mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-04-02 14:29:37 +07:00
* Ignores archived samples * Uses eslint/recommended rules * Runs prettier and eslint (including --fix) pre-commit via husky * Adds new npm scripts: 'lint', 'lint:fix' and 'prettier' * Does not lint inline js code * Fix all prettier and eslint errors * Add custom prettier rules * Apply custom prettier rules * Update readme to explain how to setup the repo * addressed comments
57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
// Copyright 2017 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// When you specify "type": "module" in the manifest background,
|
|
// you can include the service worker as an ES Module,
|
|
import { tldLocales } from './locales.js';
|
|
|
|
// Add a listener to create the initial context menu items,
|
|
// context menu items only need to be created at runtime.onInstalled
|
|
chrome.runtime.onInstalled.addListener(async () => {
|
|
for (const [tld, locale] of Object.entries(tldLocales)) {
|
|
chrome.contextMenus.create({
|
|
id: tld,
|
|
title: locale,
|
|
type: 'normal',
|
|
contexts: ['selection']
|
|
});
|
|
}
|
|
});
|
|
|
|
// Open a new search tab when the user clicks a context menu
|
|
chrome.contextMenus.onClicked.addListener((item, tab) => {
|
|
const tld = item.menuItemId;
|
|
const url = new URL(`https://google.${tld}/search`);
|
|
url.searchParams.set('q', item.selectionText);
|
|
chrome.tabs.create({ url: url.href, index: tab.index + 1 });
|
|
});
|
|
|
|
// Add or removes the locale from context menu
|
|
// when the user checks or unchecks the locale in the popup
|
|
chrome.storage.onChanged.addListener(({ enabledTlds }) => {
|
|
if (typeof enabledTlds === 'undefined') return;
|
|
|
|
const allTlds = Object.keys(tldLocales);
|
|
const currentTlds = new Set(enabledTlds.newValue);
|
|
const oldTlds = new Set(enabledTlds.oldValue ?? allTlds);
|
|
const changes = allTlds.map((tld) => ({
|
|
tld,
|
|
added: currentTlds.has(tld) && !oldTlds.has(tld),
|
|
removed: !currentTlds.has(tld) && oldTlds.has(tld)
|
|
}));
|
|
|
|
for (const { tld, added, removed } of changes) {
|
|
if (added) {
|
|
chrome.contextMenus.create({
|
|
id: tld,
|
|
title: tldLocales[tld],
|
|
type: 'normal',
|
|
contexts: ['selection']
|
|
});
|
|
} else if (removed) {
|
|
chrome.contextMenus.remove(tld);
|
|
}
|
|
}
|
|
});
|