Parent: [2f9679] (diff)

Download this file

background.js    305 lines (262 with data), 10.6 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
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
/*
* RecollWebext - WebExtension - Background Page
*
* A lot of code was copied from or inspired by the Save Page WE
* extension.
*
* Copyright (C) 2017 jfd@recoll.org
* Copyright (C) 2016-2017 DW-dev
*
* Distributed under the GNU General Public License version 2
* See LICENCE.txt file and http://www.gnu.org/licenses/
*/
"use strict";
/* Global variables */
var isFirefox;
var ffVersion;
var showSubmenu;
var badgeTabId;
/* Initialize on browser startup */
isFirefox = (navigator.userAgent.indexOf("Firefox") >= 0);
chrome.storage.local.set({ "environment-isfirefox": isFirefox });
if (isFirefox) {
chrome.runtime.getBrowserInfo(
function(info) {
ffVersion = info.version.substr(0,info.version.indexOf("."));
initialize();
});
} else {
initialize();
}
function initialize()
{
chrome.storage.local.get(null,
function(object)
{
var context;
var opt;
for (var key in object) {
console.log("Background init: " + key + " => " + object[key]);
}
/* Initialize or migrate options */
var opdefaults = {"options-showsubmenu": true,
"options-autosave": true,
"options-httpsalso": true,
"options-nomatch-dosave": true,
"options-conflict-dosave": false
};
for (opt in opdefaults) {
if (!(opt in object)) {
object[opt] = opdefaults[opt];
}
}
/* Update stored options */
chrome.storage.local.set(object);
/* Initialize local options */
showSubmenu = object["options-showsubmenu"];
/* Add context menu items */
context = showSubmenu ? "all" : "browser_action";
chrome.contextMenus.create(
{id: "indexnow", title: "Save this page for indexing",
contexts: [ context ], enabled: true });
chrome.contextMenus.create(
{id: "separator", type: "separator",
contexts: [ context ], enabled: true });
chrome.contextMenus.create(
{id: "sitealways", title: "Always Index This Site",
contexts: [ context ], enabled: true });
chrome.contextMenus.create(
{id: "sitenever", title: "Never Index This Site",
contexts: [ context ], enabled: true });
/* Set button and menu states */
chrome.tabs.query({lastFocusedWindow: true, active: true },
function(tabs) {
setButtonAndMenuStates(tabs[0].id,tabs[0].url);
});
/* Add listeners */
addListeners();
});
}
function addListeners()
{
/* Storage changed listener */
chrome.storage.onChanged.addListener(
function(changes,areaName)
{
chrome.storage.local.get(null,
function(object)
{
var context;
showSubmenu = object["options-showsubmenu"];
if ("options-showsubmenu" in changes)
{
context = showSubmenu ? "all" : "browser_action";
chrome.contextMenus.update("indexnow",
{contexts: [context]});
chrome.contextMenus.update("separator",
{contexts: [context]});
chrome.contextMenus.update("sitealways",
{contexts: [context]});
chrome.contextMenus.update("sitenever",
{contexts: [context]});
chrome.tabs.query({ lastFocusedWindow: true, active: true },
function(tabs)
{
setButtonAndMenuStates(tabs[0].id,tabs[0].url);
});
}
});
});
/* Browser action listener */
chrome.browserAction.onClicked.addListener(
function(tab)
{
initiateAction(tab, 0);
});
/* Context menu listener */
chrome.contextMenus.onClicked.addListener(
function(info,tab)
{
if (info.menuItemId == "indexnow") initiateAction(tab, 0,null);
else if (info.menuItemId == "sitealways") initiateAction(tab, 1,null);
else if (info.menuItemId == "sitenever") initiateAction(tab, 2,null);
});
/* Tab event listeners */
chrome.tabs.onActivated.addListener( /* tab selected */
function(activeInfo)
{
chrome.tabs.get(activeInfo.tabId,
function(tab)
{
setButtonAndMenuStates(activeInfo.tabId,tab.url);
});
});
chrome.tabs.onUpdated.addListener( /* URL updated */
function(tabId,changeInfo,tab)
{
setButtonAndMenuStates(tabId,tab.url);
});
/* Message received listener */
chrome.runtime.onMessage.addListener(
function(message,sender,sendResponse)
{
var xhr = new Object();
/* Messages from content script */
switch (message.type)
{
case "downloadFile":
{
var downloading = browser.downloads.download(
{filename: message.filename,
url: message.location, saveAs: false });
downloading.then(null, function(reason) {
console.log("Download failed: " + reason);});
}
break;
case "setSaveBadge":
setSaveBadge(message.text,message.color);
break;
}
});
}
function initiateAction(tab, menuaction, srcurl)
{
if (specialPage(tab.url)) {
alertNotify("Cannot be used with these special pages:\n" +
"about:, moz-extension:,\n" +
"https://addons.mozilla.org,\n" +
"chrome:, chrome-extension:,\n" +
"https://chrome.google.com/webstore.");
} else {
/* normal page - save operations allowed, saved page - all
operations allowed */
badgeTabId = tab.id;
chrome.tabs.sendMessage(tab.id,
{type: "performAction",
menuaction: menuaction, srcurl: srcurl},
function(response)
{
if (chrome.runtime.lastError != null ||
typeof response == "undefined") {
/* no response received - content script not loaded in
active tab */
chrome.tabs.executeScript(tab.id, {file: "content.js"},
function()
{
chrome.tabs.sendMessage(tab.id,{type: "performAction",
menuaction: menuaction,
srcurl: srcurl },
function(response)
{
if (chrome.runtime.lastError != null ||
typeof response == "undefined") {
/* no response received - content script
cannot be loaded in active tab*/
alertNotify("Cannot be used with this page.");
}
});
});
}
});
}
}
function specialPage(url)
{
return (url.substr(0,6) == "about:" ||
url.substr(0,14) == "moz-extension:" ||
url.substr(0,26) == "https://addons.mozilla.org" ||
url.substr(0,7) == "chrome:" ||
url.substr(0,17) == "chrome-extension:" ||
url.substr(0,34) == "https://chrome.google.com/webstore");
}
/* Set button and menu states function */
function setButtonAndMenuStates(tabId,url)
{
if (specialPage(url)) {
chrome.browserAction.disable(tabId);
if (isFirefox && ffVersion <= 54) {
/* Firefox 54- - icon not changed */
chrome.browserAction.setIcon({tabId: tabId,
path: "icon16-disabled.png"});
}
chrome.browserAction.setTitle(
{tabId: tabId,
title: "Recoll WE - cannot be used with this page" });
chrome.contextMenus.update("indexnow",{enabled: false });
chrome.contextMenus.update("separator", {enabled: true });
chrome.contextMenus.update("sitealways",{enabled: false });
chrome.contextMenus.update("sitenever",{enabled: false });
} else if (url.substr(0,5) == "file:") {
chrome.browserAction.enable(tabId);
if (isFirefox && ffVersion <= 54)
chrome.browserAction.setIcon({ tabId: tabId, path: "icon16.png"});
chrome.browserAction.setTitle({ tabId: tabId, title: "Recoll WE" });
chrome.contextMenus.update("indexnow",{enabled: true});
chrome.contextMenus.update("separator", {enabled: true});
chrome.contextMenus.update("sitealways",{enabled: false});
chrome.contextMenus.update("sitenever",{enabled: false});
} else {
chrome.browserAction.enable(tabId);
if (isFirefox && ffVersion <= 54)
chrome.browserAction.setIcon({ tabId: tabId, path: "icon16.png"});
chrome.browserAction.setTitle({ tabId: tabId, title: "Recoll WE" });
chrome.contextMenus.update("indexnow",{enabled: true});
chrome.contextMenus.update("separator", {enabled: true});
chrome.contextMenus.update("sitealways",{enabled: true});
chrome.contextMenus.update("sitenever",{enabled: true});
}
}
function setSaveBadge(text, color)
{
/*console.log("setSaveBadge: text [" + text + "] color " + color);*/
chrome.browserAction.setBadgeText({tabId: badgeTabId, text: text});
chrome.browserAction.setBadgeBackgroundColor({tabId: badgeTabId,
color: color});
}
function alertNotify(message)
{
chrome.notifications.create(
"alert",
{type: "basic", iconUrl: "icon32.png", title: "Recoll-WE",
message: "" + message });
}