-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
90 lines (76 loc) · 2.7 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
import {getActiveTabURL} from "./utils.js"
const setBadgeIcon = items => {
// items will be an object of {TICKER: [Addresses]}
// console.log(`Object.keys(items): ${Object.keys(items)}`);
// console.log(`Object.keys(items).length: ${Object.keys(items).length}`);
if (items){
let total_length = 0;
for (let i = 0; i < Object.keys(items).length; i++) {
let ticker = Object.keys(items)[i];
total_length = total_length + items[ticker].length;
}
if (total_length > 0 && total_length < 100) {
chrome.action.setBadgeText({"text": `${total_length}`});
} else if (total_length > 0 && total_length >= 100) {
chrome.action.setBadgeText({"text": `99+`});
} else { // < 0 or some other type?
chrome.action.setBadgeText({"text": ""});
}
} else { // is null
chrome.action.setBadgeText({"text": ""});
}
// do some more stress testing before deleting below
// Loop through the tickers to find total number of addrs found
// if (Object.keys(items) && items) {
// for (let i = 0; i < Object.keys(items).length; i++) {
// let ticker = Object.keys(items)[i];
// total_length = total_length + items[ticker].length;
// }
// }
// if (total_length > 0 && total_length < 100) {
// chrome.action.setBadgeText({"text": `${total_length}`});
// } else if (total_length > 0 && total_length >= 100) {
// chrome.action.setBadgeText({"text": `99+`});
// } else {
// chrome.action.setBadgeText({"text": ""});
// }
};
//determine if we should send a message to perform address checking
const runOnSite = async () => {
let current_url = await getActiveTabURL();
console.log(`current_url: ${current_url}`);
let dnr_sites = await chrome.storage.sync.get(['doNotRun']);
dnr_sites = dnr_sites.doNotRun;
console.log(`dnr_sites: ${dnr_sites}`);
for (let i = 0; i < dnr_sites.length; i++){
if (current_url.includes(dnr_sites[i])){
console.log(`${current_url} includes ${dnr_sites[i]}`);
return false;
}
}
return true;
};
// Below are the listeners for when the tab is updated or activated
chrome.tabs.onUpdated.addListener(async (tabId, tab) => {
if (await runOnSite()) {
chrome.tabs.sendMessage(tabId, {
type: "NEW",
}, setBadgeIcon);
} else {
setBadgeIcon();
}
});
chrome.tabs.onActivated.addListener(async activeInfo => {
if (await runOnSite()) {
chrome.tabs.sendMessage(activeInfo.tabId, {
type: "SWITCHED",
}, setBadgeIcon);
} else {
setBadgeIcon();
}
});
// When we are installed, set the theme to light until clicked
chrome.runtime.onInstalled.addListener(details => {
chrome.storage.sync.set({["theme"]: "light"});
chrome.storage.sync.set({["doNotRun"]: ['etherscan.io']});
});