-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
158 lines (149 loc) · 4.96 KB
/
main.js
File metadata and controls
158 lines (149 loc) · 4.96 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* globals chrome, defaultMenuStructure */
let localMenuStructure;
const createMenuItem = (item, parent) => {
// don’t create if type is group or item but isn't enable, or if type is group but doesn't have children.
if ((item.type != 'separator' && !item.enabled) || (item.type === 'group' && !item.children)) {
return;
}
// don’t create if there are children, but none of them are enabled.
// TODO: enhance check for no enabled children to check all nested levels.
if (item.children && item.children.filter(item => item.enabled === true).length === 0) {
return;
}
let menuOptions = {
id: item.id,
contexts: ['selection']
};
if (parent) {
menuOptions.parentId = parent.id;
}
if (item.type === 'separator') {
menuOptions.type = 'separator';
} else {
menuOptions.title = item.name;
}
chrome.contextMenus.create(menuOptions);
if (item.type === 'group') {
if (item.children) {
item.children.forEach((childItem) => {
createMenuItem(childItem, item);
});
}
}
};
const initializeMenus = (storageItems) => {
localMenuStructure = storageItems.menuStructure;
chrome.contextMenus.removeAll();
localMenuStructure.forEach((item) => {
createMenuItem(item, null);
});
if (storageItems.includeOptionsItem) {
chrome.contextMenus.create({
id: 'optionSeparator',
type: 'separator',
contexts: ['selection']
});
chrome.contextMenus.create({
title: 'Options',
id: 'optionsMenuItem',
contexts: ['selection']
});
}
};
function findItemById(idToFind, obj) {
let result = null;
if (!obj) {
obj = localMenuStructure;
}
if (obj instanceof Array) {
for (let item of obj) {
let arrayResult = findItemById(idToFind, item);
if (arrayResult) {
result = arrayResult;
break;
}
}
} else {
if (obj.id === idToFind) {
return obj;
}
if (obj.children) {
for (let childItem of obj.children) {
let childResult = findItemById(idToFind, childItem);
if (childResult) {
result = childResult;
break;
}
}
}
}
return result;
}
const executeSearch = (info, tab) => {
if (info.menuItemId === 'optionsMenuItem') {
chrome.runtime.openOptionsPage();
return;
}
let menuItem = findItemById(info.menuItemId);
if (menuItem && menuItem.urls) {
let selectionText = info.selectionText;
if (menuItem.encode) {
selectionText = encodeURIComponent(selectionText);
}
if (menuItem.tabtype === '(Incognito)') {
chrome.windows.create({
url: menuItem.urls.map(url => url.replace(/%s/, selectionText)),
incognito: true,
state: 'maximized'
});
} else {
menuItem.urls.forEach((url, index) => {
url = url.replace(/%s/, selectionText);
switch (menuItem.tabtype) {
case '(Standard)':
chrome.tabs.create({
url: url,
'index': tab.index + 1 + index,
active: menuItem.active,
});
break;
case '(Last Tab)':
chrome.tabs.create({
url: url,
active: menuItem.active,
});
break;
case '(Same Tab)':
if (index === 0) {
chrome.tabs.update(tab.id, {
'url': url,
active: menuItem.active,
});
} else {
chrome.tabs.create({
url: url,
'index': tab.index + index,
active: menuItem.active,
});
}
}
});
}
} else {
/* jshint ignore:start */
if (menuItem && !menuItem.urls) {
console.error('Item with id “' + info.menuItemId + '” doesn’t have a urls property.');
} else if (!menuItem) {
console.error('Couldn’t find item with id “' + info.menuItemId + '.”');
}
/* jshint ignore:end */
}
};
chrome.storage.sync.get({
'menuStructure': defaultMenuStructure,
'includeOptionsItem': true
}, initializeMenus);
chrome.storage.onChanged.addListener(function() {
chrome.storage.sync.get(initializeMenus);
});
chrome.contextMenus.onClicked.addListener(executeSearch);