-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathbackground.js
298 lines (272 loc) · 8.49 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
function isEmpty(obj) {
if (obj) return Object.keys(obj).length === 0;
return true;
}
let preventInstance = {};
function startJustRead(tab) {
if (tab) {
executeScripts(tab.id);
} else {
chrome.tabs.query({ currentWindow: true, active: true }, (tabArray) => {
if (tabArray.length) executeScripts(tabArray[0].id);
});
}
}
function executeScripts(tabId) {
if (preventInstance[tabId]) return;
preventInstance[tabId] = true;
setTimeout(() => delete preventInstance[tabId], 10000);
// Add a badge to signify the extension is in use
chrome.action.setBadgeBackgroundColor({ color: [242, 38, 19, 230] });
chrome.action.setBadgeText({ text: "on" });
// Check if we need to add the site to JR's autorun list
chrome.storage.sync.get("alwaysAddAR", function (result) {
if (result && result["alwaysAddAR"]) {
addSiteToAutorunList(null, tab);
}
});
// Load our external scripts, then our content script
chrome.scripting
.executeScript({
target: { tabId: tabId, allFrames: false },
files: [
"/external-libraries/readability/readability.js",
"/external-libraries/datGUI/dat.gui.min.js",
"/external-libraries/DOMPurify/purify.min.js",
"/external-libraries/Rangy/rangy.min.js",
"/external-libraries/Rangy/rangy-classapplier.min.js",
"/external-libraries/Rangy/rangy-highlighter.min.js",
"/external-libraries/Rangy/rangy-textrange.min.js",
],
})
.then(() => {
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
files: ["content_script.js"],
});
setTimeout(function () {
chrome.action.setBadgeText({ text: "" });
chrome.storage.sync.set({ useText: false });
chrome.storage.sync.set({ runOnLoad: false });
}, 1000);
});
}
function startSelectText() {
chrome.storage.sync.set({ useText: true });
startJustRead();
}
function createPageCM() {
// Create a right click menu option
pageCMId = chrome.contextMenus.create(
{
title: "View this page using Just Read",
id: "pageCM",
contexts: ["page"],
},
chrome.runtime.lastError
);
}
function createLinkCM() {
// Create an entry to allow user to open a given link using Just read
linkCMId = chrome.contextMenus.create(
{
title: "View the linked page using Just Read",
id: "linkCM",
contexts: ["link"],
},
chrome.runtime.lastError
);
}
function createAutorunCM() {
// Create an entry to allow user to open a given link using Just read
autorunCMId = chrome.contextMenus.create(
{
title: "Add this site to Just Read's auto-run list",
id: "autorunCM",
contexts: ["page"],
},
chrome.runtime.lastError
);
}
function addSiteToAutorunList(info, tab) {
chrome.storage.sync.get("auto-enable-site-list", function (result) {
let url = new URL((info != null && info.pageUrl) || tab.url);
let entry;
if (url.pathname !== "/" && url.pathname !== "") {
entry = url.hostname + "/.+";
} else {
entry = url.hostname;
}
let currentDomains = result["auto-enable-site-list"];
if (!isEmpty(currentDomains)) {
if (!currentDomains.includes(entry)) {
chrome.storage.sync.set(
{
"auto-enable-site-list": [...currentDomains, entry],
},
function () {
if (currentDomains.indexOf(url.hostname)) {
console.log(
"Just Read auto-run entry added.\n\nWarning: An auto-run entry with the same hostname has already been added. Be careful to not add two duplicates."
);
} else {
console.log("Just Read auto-run entry added.");
}
}
);
} else {
console.error(
"Entry already exists inside of Just Read's auto-run list. Not adding new entry."
);
}
} else {
chrome.storage.sync.set({ "auto-enable-site-list": [entry] });
}
});
}
let pageCMId = (linkCMId = autorunCMId = undefined);
function updateCMs() {
chrome.storage.sync.get(
["enable-pageCM", "enable-linkCM", "enable-autorunCM"],
function (result) {
let size = 0;
for (let key in result) {
size++;
if (key === "enable-pageCM") {
if (result[key]) {
if (typeof pageCMId == "undefined") createPageCM();
} else {
if (typeof pageCMId != "undefined") {
pageCMId = undefined;
}
}
} else if (key === "enable-linkCM") {
if (result[key]) {
if (typeof linkCMId == "undefined") createLinkCM();
} else {
if (typeof linkCMId != "undefined") {
linkCMId = undefined;
}
}
} else if (key === "enable-autorunCM") {
if (result[key]) {
if (typeof autorunCMId == "undefined") createAutorunCM();
} else {
if (typeof autorunCMId != "undefined") {
autorunCMId = undefined;
}
}
}
}
if (size === 0) {
createPageCM();
createLinkCM();
createAutorunCM();
}
}
);
}
// Listen for the extension's click
chrome.action.onClicked.addListener(startJustRead);
// Listen for the keyboard shortcut
chrome.commands.onCommand.addListener(function (command) {
if (command == "open-just-read") startJustRead();
if (command == "select-text") startSelectText();
});
// Listen for messages
let lastClosed = Date.now();
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request === "Open options") {
chrome.runtime.openOptionsPage();
} else if (request.updateCMs === "true") {
updateCMs();
} else if (request.closeTab === "true") {
chrome.tabs.query(
{
active: true,
lastFocusedWindow: true,
},
function (tabs) {
const tab = tabs[0];
setTimeout(function () {
chrome.tabs.remove(tab.id);
}, 100);
}
);
} else if (request.lastClosed) {
lastClosed = request.lastClosed;
}
// For JRP
else if (request.jrSecret) {
chrome.storage.sync.set({ jrSecret: request.jrSecret });
} else if (request.resetJRLastChecked) {
chrome.storage.sync.set({ jrLastChecked: "" });
} else if (request.tabOpenedJR) {
const tabURL = request.tabOpenedJR.href.split("?")[0];
for (const tabId in preventInstance) {
chrome.tabs.get(parseInt(tabId), (tab) => {
if (tab.url.split("?")[0] === tabURL) {
setTimeout(() => delete preventInstance[tabId], 1000);
}
});
}
}
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === "selectContentCM") {
startSelectText();
} else if (info.menuItemId === "pageCM") {
startJustRead();
} else if (info.menuItemId === "linkCM") {
chrome.tabs.create({ url: info.linkUrl, active: false }, function (newTab) {
chrome.storage.sync.set({ runOnLoad: true });
startJustRead(newTab);
});
} else if (info.menuItemId === "autorunCM") {
addSiteToAutorunList();
}
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (preventInstance[tabId]) return;
const change = Date.now() - lastClosed;
if (changeInfo.status === "complete" && change > 300) {
// Auto enable on sites specified
chrome.storage.sync.get("auto-enable-site-list", function (siteListObj) {
let siteList;
if (siteListObj) {
siteList = siteListObj["auto-enable-site-list"];
const url = tab.url;
if (typeof siteList !== "undefined") {
for (let i = 0; i < siteList.length; i++) {
// Allows the format `text.npr.org>5000` to autorun JR after 5 seconds on text.npr.org
const entry = siteList[i];
const splitEntry = entry.split(">");
const entryRegex = splitEntry[0];
const urlRegex = new RegExp(entryRegex, "i");
if (url.match(urlRegex)) {
chrome.storage.sync.set({ runOnLoad: true });
startJustRead(tab);
return;
}
}
}
// Check if jr=on is set, autorun if so
if (new URL(url).searchParams.get("jr") === "on") {
startJustRead(tab);
}
}
});
}
});
// Add our context menus
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create(
{
title: "Select content to read",
contexts: ["action"],
id: "selectContentCM",
},
chrome.runtime.lastError
);
updateCMs();
});