Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions api-samples/contextMenus/global_context_search/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,46 @@
// found in the LICENSE file.

// When you specify "type": "module" in the manifest background,
// you can include the service worker as an ES Module,
// 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']
});
chrome.contextMenus.create(
{
id: tld,
title: `Search Google ${locale} for "%s"`,
type: 'normal',
contexts: ['selection']
},
() => {
if (chrome.runtime.lastError) {
console.warn(
`Failed to create menu item for "${tld}":`,
chrome.runtime.lastError.message
);
}
}
);
}
});

// Open a new search tab when the user clicks a context menu
// Open a new search tab when the user clicks a context menu item.
// tab can be undefined in some contexts (e.g. chrome://extensions),
// so we only set index if tab is available.
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 });
chrome.tabs.create({
url: url.href,
...(tab && { index: tab.index + 1 })
});
});

// Add or removes the locale from context menu
// Add or remove 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;
Expand All @@ -43,14 +58,31 @@ chrome.storage.onChanged.addListener(({ enabledTlds }) => {

for (const { tld, added, removed } of changes) {
if (added) {
chrome.contextMenus.create({
id: tld,
title: tldLocales[tld],
type: 'normal',
contexts: ['selection']
});
chrome.contextMenus.create(
{
id: tld,
title: tldLocales[tld],
type: 'normal',
contexts: ['selection']
},
() => {
if (chrome.runtime.lastError) {
console.warn(
`Failed to create menu item for "${tld}":`,
chrome.runtime.lastError.message
);
}
}
);
} else if (removed) {
chrome.contextMenus.remove(tld);
chrome.contextMenus.remove(tld, () => {
if (chrome.runtime.lastError) {
console.warn(
`Failed to remove menu item for "${tld}":`,
chrome.runtime.lastError.message
);
}
});
}
}
});
8 changes: 4 additions & 4 deletions api-samples/contextMenus/global_context_search/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "Global Google Search",
"description": "Uses the context menu to search a different country's Google",
"version": "1.1",
"version": "1.2",
"description": "Right-click selected text to search on a regional Google site.",
"manifest_version": 3,
"permissions": ["contextMenus", "storage"],
"background": {
"service_worker": "background.js",
"type": "module"
},
"permissions": ["contextMenus", "tabs", "storage"],
"action": {
"default_popup": "popup.html"
},
Expand All @@ -16,4 +16,4 @@
"48": "globalGoogle48.png",
"128": "globalGoogle128.png"
}
}
}
14 changes: 3 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"@eslint/js": "9.19.0",
"chrome-types": "0.1.335",
"eslint": "9.19.0",
"eslint": "^9.19.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-jest": "28.11.0",
"eslint-plugin-prettier": "5.4.1",
Expand All @@ -39,5 +39,7 @@
"**/*.{md,html,json}": [
"npx prettier --write"
]
}
},
"main": "eslint.config.js",
"type": "commonjs"
}