-
Notifications
You must be signed in to change notification settings - Fork 10
/
background.js
352 lines (302 loc) · 11.7 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* global util */
const OPTION_AUTOCOMPLETE_KEY = "autocompleteEnabled";
const OPTION_COMMENT_STRING_KEY = "commentString";
const OPTION_CONTEXTMENU_KEY = "contextmenuEnabled";
const OPTION_ITEMS_KEY = "items";
const OPTION_MATCH_ONLY_AT_BEGINNING = "matchOnlyAtBeginning";
const OPTION_MINIMUM_CHARACTER_COUNT_KEY = "minimumCharacterCount";
const OPTION_SYNC_ITEMS = "syncItems";
const OPTION_USE_TAB_KEY = "useTabToChooseItems";
const CONTEXT_MENU_ROOT_ID = "root";
const CONTEXT_MENU_PREFERENCES_ID = "preferences";
const CONTEXT_MENU_SEPARATOR_ID = "separator";
const CONTEXT_MENU_ADD_SELECTION_ID = "add-selection";
let autocompleteEnabled;
let commentString;
let contextmenuEnabled;
let itemString;
let matchOnlyAtBeginning;
let minimumCharacterCount;
let syncItems;
let useTabToChooseItems;
browser.storage.local.get([
OPTION_AUTOCOMPLETE_KEY,
OPTION_COMMENT_STRING_KEY,
OPTION_CONTEXTMENU_KEY,
OPTION_ITEMS_KEY,
OPTION_MATCH_ONLY_AT_BEGINNING,
OPTION_MINIMUM_CHARACTER_COUNT_KEY,
OPTION_SYNC_ITEMS,
OPTION_USE_TAB_KEY,
])
.then(
(result) => {
if (result[OPTION_ITEMS_KEY] === undefined) {
browser.storage.local.set({[OPTION_ITEMS_KEY]: ""});
} else {
itemString = result[OPTION_ITEMS_KEY];
}
if (result[OPTION_CONTEXTMENU_KEY] === undefined) {
browser.storage.local.set({[OPTION_CONTEXTMENU_KEY]: true});
} else {
contextmenuEnabled = result[OPTION_CONTEXTMENU_KEY];
}
updateContextMenu();
if (result[OPTION_AUTOCOMPLETE_KEY] === undefined) {
browser.storage.local.set({[OPTION_AUTOCOMPLETE_KEY]: false});
} else {
enableDisableAutocomplete(result[OPTION_AUTOCOMPLETE_KEY]);
}
if (result[OPTION_USE_TAB_KEY] === undefined) {
browser.storage.local.set({[OPTION_USE_TAB_KEY]: false});
} else {
useTabToChooseItems = result[OPTION_USE_TAB_KEY];
}
if (result[OPTION_MATCH_ONLY_AT_BEGINNING] === undefined) {
browser.storage.local.set({[OPTION_MATCH_ONLY_AT_BEGINNING]: false});
} else {
matchOnlyAtBeginning = result[OPTION_MATCH_ONLY_AT_BEGINNING];
}
if (result[OPTION_COMMENT_STRING_KEY] === undefined) {
browser.storage.local.set({[OPTION_COMMENT_STRING_KEY]: ""});
} else {
commentString = result[OPTION_COMMENT_STRING_KEY];
}
if (result[OPTION_MINIMUM_CHARACTER_COUNT_KEY] === undefined) {
browser.storage.local.set({[OPTION_MINIMUM_CHARACTER_COUNT_KEY]: 1});
} else {
minimumCharacterCount = result[OPTION_MINIMUM_CHARACTER_COUNT_KEY];
}
if (result[OPTION_SYNC_ITEMS] === undefined) {
browser.storage.local.set({[OPTION_SYNC_ITEMS]: false});
} else {
syncItems = result[OPTION_SYNC_ITEMS];
}
}
);
browser.storage.onChanged.addListener(
(changes, areaName) => {
let initTriggered = false;
if (changes[OPTION_SYNC_ITEMS]) {
const previousValue = syncItems;
const newValue = changes[OPTION_SYNC_ITEMS].newValue;
syncItems = newValue;
if (previousValue !== newValue && syncItems) {
initTriggered = true;
initSyncItems();
}
}
if (changes[OPTION_ITEMS_KEY]) {
itemString = changes[OPTION_ITEMS_KEY].newValue;
if (!initTriggered){
maybeSyncItems(areaName, itemString);
}
}
if (changes[OPTION_CONTEXTMENU_KEY]) {
contextmenuEnabled = changes[OPTION_CONTEXTMENU_KEY].newValue;
}
updateContextMenu();
if (changes[OPTION_AUTOCOMPLETE_KEY]) {
enableDisableAutocomplete(changes[OPTION_AUTOCOMPLETE_KEY].newValue);
}
if (changes[OPTION_USE_TAB_KEY]) {
useTabToChooseItems = changes[OPTION_USE_TAB_KEY].newValue;
}
if (changes[OPTION_MATCH_ONLY_AT_BEGINNING]) {
matchOnlyAtBeginning = changes[OPTION_MATCH_ONLY_AT_BEGINNING].newValue;
}
if (changes[OPTION_COMMENT_STRING_KEY]) {
commentString = changes[OPTION_COMMENT_STRING_KEY].newValue;
}
if (changes[OPTION_MINIMUM_CHARACTER_COUNT_KEY]) {
minimumCharacterCount = changes[OPTION_MINIMUM_CHARACTER_COUNT_KEY].newValue;
}
if (autocompleteEnabled) {
sendOptionsToActiveTab();
}
}
);
function updateContextMenu() {
browser.contextMenus.removeAll().then(() => maybeFillContextMenu());
}
function maybeFillContextMenu() {
if (!contextmenuEnabled) {
return;
}
browser.contextMenus.create({
id: CONTEXT_MENU_ROOT_ID,
title: "Simple Form Fill",
contexts: ["page", "frame", "selection", "editable"],
});
browser.contextMenus.create({
id: CONTEXT_MENU_PREFERENCES_ID,
parentId: CONTEXT_MENU_ROOT_ID,
title: "Preferences",
contexts: ["page", "frame", "selection", "editable"],
});
browser.contextMenus.create({
id: CONTEXT_MENU_ADD_SELECTION_ID,
parentId: CONTEXT_MENU_ROOT_ID,
title: "Add '%s'",
contexts: ["selection"],
});
const items = itemStringToList(itemString);
if (items.length > 0) {
browser.contextMenus.create({
id: CONTEXT_MENU_SEPARATOR_ID,
parentId: CONTEXT_MENU_ROOT_ID,
type: "separator",
contexts: ["editable"],
});
for (const item of items) {
browser.contextMenus.create({
id: item,
parentId: CONTEXT_MENU_ROOT_ID,
title: item,
contexts: ["editable"],
});
}
}
}
browser.contextMenus.onClicked.addListener((info, tab) => {
switch (info.menuItemId) {
case CONTEXT_MENU_PREFERENCES_ID:
browser.runtime.openOptionsPage();
break;
case CONTEXT_MENU_ADD_SELECTION_ID:
addItem(info.selectionText);
break;
default:
chainPromises([
() => { return browser.tabs.executeScript(tab.id, {file: "browser-polyfill.js", allFrames: true}); },
() => { return browser.tabs.executeScript(tab.id, {file: "content-scripts/insert-item.js", allFrames: true}); },
() => { return commentString ? info.menuItemId.split(commentString)[0] : info.menuItemId; },
(cleanedItem) => { return browser.tabs.sendMessage(tab.id, {item: cleanedItem}); },
]);
}
});
function addItem(item) {
if (itemString) {
itemString += "\n";
itemString += item;
} else {
itemString = item;
}
browser.storage.local.set({[OPTION_ITEMS_KEY]: itemString});
}
function sendOptions(tabId, frameId) {
console.debug("Send items to tab " + tabId + " and frame " + frameId);
const options = {};
if (frameId) {
options.frameId = frameId;
}
browser.tabs.sendMessage(
tabId,
{
commentString,
itemList: itemStringToList(itemString),
useTabToChooseItems,
minimumCharacterCount,
matchOnlyAtBeginning,
},
options
);
}
function itemStringToList(itemString) {
if (!itemString) {
return [];
}
return itemString.split(/\r?\n/).filter(Boolean);
}
function sendOptionsToActiveTab() {
console.debug("Send items to active tab");
browser.tabs.query({currentWindow: true, active: true})
.then((matchingTabs) => { sendOptions(matchingTabs[0].id); });
}
function onUpdated(tabId, changeInfo) {
if (changeInfo.status == "complete") {
console.debug("New page loaded, check for inputs");
chainPromises([
() => { return browser.tabs.executeScript(tabId, {file: "browser-polyfill.js", allFrames: true}); },
() => { return browser.tabs.executeScript(tabId, {file: "content-scripts/checker.js", allFrames: true}); },
]);
}
}
function onMessage(message, sender) {
if (message.text == "refreshAutocomplete") {
if (message.requireInizialization) {
console.debug("Background got request to initialize autocompletes");
initializeAutocomplete(sender.tab.id, sender.frameId);
} else {
console.debug("Background got request to refresh autocompletes");
sendOptions(sender.tab.id, sender.frameId);
}
}
}
function initializeAutocomplete(tabId, frameId) {
console.debug("Initialize autocomplete for tab " + tabId + " and frame " + frameId);
chainPromises([
() => { return browser.tabs.executeScript(tabId, {file: "browser-polyfill.js", frameId: frameId}); },
() => { return browser.tabs.executeScript(tabId, {file: "content-scripts/jquery-3.1.1.js", frameId: frameId}); },
() => { return browser.tabs.executeScript(tabId, {file: "content-scripts/jquery-ui-1.12.1.js", frameId: frameId}); },
() => { return browser.tabs.executeScript(tabId, {file: "content-scripts/autocomplete.js", frameId: frameId}); },
() => { return browser.tabs.insertCSS(tabId, {file: "content-scripts/autocomplete.css", frameId: frameId}); },
() => { return sendOptions(tabId, frameId); },
]);
}
function enableDisableAutocomplete(enable) {
if (enable && !autocompleteEnabled) {
console.debug("Enable autocomplete");
browser.tabs.onUpdated.addListener(onUpdated);
browser.runtime.onMessage.addListener(onMessage);
browser.tabs.onActivated.addListener(sendOptionsToActiveTab);
autocompleteEnabled = true;
} else if (!enable && autocompleteEnabled) {
console.debug("Disable autocomplete");
browser.tabs.onUpdated.removeListener(onUpdated);
browser.runtime.onMessage.removeListener(onMessage);
browser.tabs.onActivated.removeListener(sendOptionsToActiveTab);
autocompleteEnabled = false;
}
}
function initSyncItems() {
Promise.all([
browser.storage.local.get(OPTION_ITEMS_KEY),
browser.storage.sync.get(OPTION_ITEMS_KEY),
])
.then(
([localResult, remoteResult]) => {
const localValue = localResult[OPTION_ITEMS_KEY];
const remoteValue = remoteResult[OPTION_ITEMS_KEY];
const newValue = util.mergeItemString(localValue, remoteValue);
if (JSON.stringify(localValue) != JSON.stringify(newValue)) {
browser.storage.local.set({[OPTION_ITEMS_KEY]: newValue});
}
if (JSON.stringify(remoteValue) != JSON.stringify(newValue)) {
browser.storage.sync.set({[OPTION_ITEMS_KEY]: newValue});
}
}
);
}
function maybeSyncItems(changedArea, itemString) {
if (!syncItems) {
return;
}
const toAreaName = changedArea === "local" ? "sync" : "local";
const toArea = browser.storage[toAreaName];
toArea.get([OPTION_ITEMS_KEY]).then(
(result) => {
const targetItemString = result[OPTION_ITEMS_KEY];
if (targetItemString !== itemString){
toArea.set({[OPTION_ITEMS_KEY]: itemString});
}
}
);
}
function chainPromises(functions) {
let promise = Promise.resolve();
for (const function_ of functions) {
promise = promise.then(function_);
}
return promise.catch((error) => { console.warn(error.message, error.stack); });
}