-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
200 lines (160 loc) · 6.18 KB
/
Copy pathbackground.js
File metadata and controls
200 lines (160 loc) · 6.18 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
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
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
var settings = {
popup_width: 400,
popup_height: 100,
CSS_COLORS_COUNT: 20,
CSSprefix1: "chrome-extension-FindManyStrings",
CSSprefix2: "chrome-extension-FindManyStrings-style-",
CSSprefix3: "CE-FMS-",
delim: ',',
isAlwaysSearch: true,
isOn: true,
isCasesensitive: false,
isInstant: true,
isNewlineNewColor: true,
isSaveKws: true,
isWholeWord: false,
latest_keywords: [],
enableAddKw: true,
enableRemoveKw: true
}
chrome.contextMenus.create({
title: 'Add Keyword',
id: 'addKw',
contexts: ['selection'],
});
chrome.contextMenus.create({
title: 'Remove Keyword',
id: 'removeKw',
contexts: ['selection'],
});
chrome.storage.local.set({ 'settings': settings });
} else {
chrome.storage.local.get(['settings'], function (result) {
var settings = result.settings;
handle_addKw_change(settings.enableAddKw);
handle_removeKw_change(settings.enableRemoveKw);
handle_popupSize_change(settings.popup_height, settings.popup_width);
});
}
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
if (changeInfo.status === 'complete' || changeInfo.title || changeInfo.url) {
chrome.storage.local.get(['settings'], function (result) {
var settings = result.settings;
var tabkey = get_tabkey(tabId);
console.log("[" + tabkey + "] onUpdated " + changeInfo.status);
var tabinfo = {};
tabinfo.id = tabId;
tabinfo.keywords = settings.isSaveKws ? settings.latest_keywords : [];
chrome.storage.local.set({ [tabkey]: tabinfo });
});
}
});
chrome.contextMenus.onClicked.addListener(function getword(info, tab) {
var tabId = tab.id;
var tabkey = get_tabkey(tabId);
var kw = info.selectionText.toLowerCase().split(' ')[0];
if (info.menuItemId === "removeKw") {
chrome.storage.local.get(["settings", tabkey], function (result) {
var settings = result.settings;
var tabinfo = result[tabkey];
var kws = tabinfo.keywords;
var pos = -1;
for (var i = 0, len = kws.length; i < len; ++i) {
if (kws[i].kwStr === kw) {
pos = i;
break;
}
}
if (pos < 0) return;
chrome.tabs.sendMessage(tabId, {
action: "_hl_clear",
removedKws: [kws[pos]],
});
tabinfo.keywords.splice(pos, 1);
settings.latest_keywords = tabinfo.keywords;
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];
if (settings.isNewlineNewColor) {
addedKw = { kwGrp: 0, kwStr: kw };
tabinfo.keywords.unshift(addedKw);
} else {
addedKw = { kwGrp: (tabinfo.keywords.length % 20), kwStr: kw };
tabinfo.keywords.push(addedKw);
}
chrome.tabs.sendMessage(tabId, {
action: "_hl_search",
addedKws: [addedKw],
});
settings.latest_keywords = tabinfo.keywords;
chrome.storage.local.set({ [tabkey]: tabinfo, "settings": settings }, function () {
});
});
}
});
function handle_addKw_change(enableIt) {
chrome.storage.local.get(['settings'], function (result) {
if (enableIt) {
chrome.contextMenus.create({
title: 'Add Keyword',
id: 'addKw',
contexts: ['selection'],
});
result.settings.enableAddKw = true;
} else {
chrome.contextMenus.remove("addKw");
result.settings.enableAddKw = false;
}
chrome.storage.local.set({ 'settings': result.settings });
});
}
function handle_removeKw_change(enableIt) {
chrome.storage.local.get(['settings'], function (result) {
if (enableIt) {
chrome.contextMenus.create({
title: 'Remove Keyword',
id: 'removeKw',
contexts: ['selection'],
});
result.settings.enableRemoveKw = true;
} else {
chrome.contextMenus.remove("removeKw");
result.settings.enableRemoveKw = false;
}
chrome.storage.local.set({ 'settings': result.settings });
});
}
function handle_popupSize_change(newHeight, newWidth) {
chrome.storage.local.get(['settings'], function (result) {
is_changed = false;
if (newHeight) {
result.settings.popup_height = newHeight;
is_changed = true;
}
if (newWidth) {
result.settings.popup_width = newWidth;
is_changed = true;
}
if (is_changed) {
chrome.storage.local.set({ 'settings': result.settings });
}
});
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action == "getTabId") {
sendResponse({ tabId: sender.tab.id });
} else if (request.action == "handle_addKw_change") {
handle_addKw_change(request.enableIt);
} else if (request.action == "handle_removeKw_change") {
handle_removeKw_change(request.enableIt);
} else if (request.action == "handle_popupSize_change") {
handle_popupSize_change(request.newHeight, request.newWidth);
}
});