-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
56 lines (47 loc) · 1.59 KB
/
service_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Load configured services and create context menu items
function loadServices() {
chrome.contextMenus.removeAll(() => {
// Create a parent context menu item
chrome.contextMenus.create({
id: 'highlightedText',
title: 'Quick Search Menu',
contexts: ['selection']
});
// Load services from storage and create submenu items
chrome.storage.sync.get('services', (data) => {
const services = data.services || {};
for (const key in services) {
chrome.contextMenus.create({
id: key,
parentId: 'highlightedText',
title: `Search on ${key}`,
contexts: ['selection']
});
}
});
});
}
// Initialize context menu items
loadServices();
// Add a click event listener for the context menu items
chrome.contextMenus.onClicked.addListener((info, tab) => {
// Get the selected text
const selectedText = info.selectionText;
// Load services from storage
chrome.storage.sync.get('services', (data) => {
const services = data.services || {};
// Check if the clicked item ID matches a service key
if (services.hasOwnProperty(info.menuItemId)) {
const serviceUrlTemplate = services[info.menuItemId];
const url = serviceUrlTemplate.replace('$TEXT', encodeURIComponent(selectedText));
// Open the URL in a new tab
chrome.tabs.create({ url });
}
});
});
// Listen for changes in storage and reload services if necessary
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync' && changes.hasOwnProperty('services')) {
loadServices();
}
});