-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
93 lines (73 loc) · 2.67 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
chrome.runtime.onInstalled.addListener(function (details) {
// variables initialization
var settings = {
popup_width: 340,
popup_height: 40,
CSS_COLORS_COUNT: 10,
CSSprefix1: "chrome-extension-FindManyStrings",
CSSprefix2: "chrome-extension-FindManyStrings-style-",
CSSprefix3: "CE-FMS-",
isInstant: true,
isSaveKws: true,
delim: ' ',
latest_keywords: [],
isItemAddKw: true,
isItemRemoveKw: true
}
chrome.contextMenus.create({
title: 'Remove Keyword',
id: 'removeKw',
contexts: ['selection'],
});
chrome.contextMenus.create({
title: 'Add Keyword',
id: 'addKw',
contexts: ['selection'],
});
chrome.storage.local.set({'settings': settings});
});
chrome.tabs.onUpdated.addListener(function (tabId, info) {
if (info.status === 'complete') {
var tabkey = get_tabkey(tabId);
var tabinfo = {};
tabinfo.id = tabId;
tabinfo.style_nbr = 0;
tabinfo.isNewPage = true;
tabinfo.keywords = [];
chrome.storage.local.set({[tabkey]: tabinfo});
}
});
chrome.contextMenus.onClicked.addListener(function getword(info, tab) {
var tabkey = get_tabkey(tab.id);
var kw = info.selectionText.toLowerCase().split(' ')[0];
if (info.menuItemId === "removeKw") {
chrome.storage.local.get(["settings", tabkey], function (result) {
settings = result.settings;
tabinfo = result[tabkey];
var index = tabinfo.keywords.indexOf(kw);
if (index !== -1) {
length = tabinfo.keywords.length
while (length--) {
if (kw.indexOf(tabinfo.keywords[length]) != -1 && length != index) {
return;
}
}
tabinfo.keywords.splice(index, 1);
}
settings.latest_keywords = tabinfo.keywords;
hl_clear([kw], settings, tabinfo);
chrome.storage.local.set({[tabkey]: tabinfo, "settings": settings}, function () {
});
});
} else if (info.menuItemId === "addKw") {
chrome.storage.local.get(["settings", tabkey], function (result) {
settings = result.settings;
tabinfo = result[tabkey];
tabinfo.keywords.push(kw);
settings.latest_keywords = tabinfo.keywords;
hl_search([kw], settings, tabinfo);
chrome.storage.local.set({[tabkey]: tabinfo, "settings": settings}, function () {
});
});
}
});