-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
90 lines (82 loc) · 2.95 KB
/
background.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
// Add a listener to create the initial context menu items,
chrome.runtime.onInstalled.addListener(function() {
// Grab options from sunced Chrome storage
chrome.storage.sync.get('data', function(items) {
// if there are no options saved, do nothing
if (!items.data)
return;
// create the menu items
items.data.forEach((form, i) => {
chrome.contextMenus.create({
id: form.name,
title: form.name,
type: 'normal',
contexts: ['selection'],
});
});
});
});
// perform the search on the chosen site
chrome.contextMenus.onClicked.addListener(function(item, tab) {
var data = []; // holds the options from storage
var menuItem = {}; // selected menu item options
var parameters = ""; // stores the url encoded parameter string
chrome.storage.sync.get('data', function(items) {
data = items.data;
// get settings for clicked menu item from chrome storage
data.forEach((form, i) => {
if (form.name == item.menuItemId)
{
menuItem = form;
}
});
// build parameter string
if (menuItem.parms)
{
menuItem.parms.forEach((parm, i) => {
parameters += "&" + encodeURIComponent(parm.parm) + "=" + encodeURIComponent(parm.value);
});
}
// create and open url
let url = menuItem.url + encodeURIComponent(item.selectionText) + parameters;
chrome.tabs.create({url: url, index: tab.index + 1});
});
});
// add or remove context menu items when they change in the options
chrome.storage.onChanged.addListener(function(list, sync) {
let newlyDisabled = [];
let newlyEnabled = [];
var menu = [];
let newMenu = list.data.newValue || [];
let oldMenu = list.data.oldValue || [];
chrome.storage.sync.get('data', function(items) {
if (!items.data)
return;
// determine if menu itmes were added or removed
menu = (oldMenu.length > newMenu.length) ? oldMenu : newMenu;
for (let key of menu) {
if (newMenu.some(e => e.name == key.name) && !oldMenu.some(e => e.name == key.name)) {
newlyEnabled.push({
id: key.name,
title: key.name
});
} else if (oldMenu.some(e => e.name == key.name) && !newMenu.some(e => e.name == key.name)) {
newlyDisabled.push(key.name);
}
}
// add new menu items
for (let item of newlyEnabled) {
chrome.contextMenus.create({
id: item.id,
title: item.title,
type: 'normal',
contexts: ['selection'],
});
}
// remove old menu itmes
for (let item of newlyDisabled) {
chrome.contextMenus.remove(item);
}
});
});