generated from hchiam/learning-firefox-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain.js
81 lines (68 loc) · 2.26 KB
/
brain.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
clearStorage();
getHosts();
function getHosts() {
const hosts = new Set(); // use Set to keep unique values
getHostFromCurrentTab(hosts);
const children = Array.from(document.head.children);
addSrcAndHrefToHosts(children, hosts);
const scripts = Array.from(document.scripts);
addSrcAndHrefToHosts(scripts, hosts);
addToStorage("hosts", Array.from(hosts));
// console.log(hosts);
}
function getHostFromCurrentTab(hosts) {
const tabHost = String(window.location);
addHostToHosts(tabHost, hosts);
}
function addSrcAndHrefToHosts(elements, hosts) {
for (let i in elements) {
const src = elements[i].src;
addHostToHosts(src, hosts);
const href = elements[i].href;
addHostToHosts(href, hosts);
const baseURI = elements[i].baseURI;
addHostToHosts(baseURI, hosts);
}
}
function addHostToHosts(host, hosts) {
const noHost = !host || host == undefined;
if (noHost) return;
// keep part between "//" and 1st "/", and remove the final "/":
host = host.split("/")[2].replace(/\/$/, "");
const invalidHost = !host || !host.includes(".");
const repeatHost = hosts.has(host);
if (invalidHost || repeatHost) return;
hosts.add(host);
const hostMinusWWW = host.startsWith("www.")
? host.replace(/^www\./g, "")
: "";
if (hostMinusWWW !== "") {
hosts.add(hostMinusWWW);
}
const hostPartDotPart = host.match(/\.([^.]+\..+)$/);
if (hostPartDotPart !== null) {
hosts.add(hostPartDotPart[1]);
}
}
function addToStorage(key, data) {
chrome.storage.local.set({ [key]: data }, () => {
// console.log('addToStorage', key, data);
});
}
function updateStorage(key, data) {
chrome.storage.local.get(key, (result) => {
chrome.storage.local.remove(key);
addToStorage(key, data);
// console.log('updateStorage', key, data);
});
}
function clearStorage() {
chrome.storage.local.clear();
}
// to get messages from popup.js, use this instead of chrome.storage.local.get:
chrome.runtime.onMessage.addListener((results) => {
const openNewTabs = results.openNewTabs;
if (openNewTabs !== true) return; // just in case
getHosts(); // get hosts now since page has already reloaded after message received
// (let popup.js open tabs to the hosts)
}); // chrome.runtime.onMessage.addListener does not accept onError parameter