/
popup.js
64 lines (50 loc) · 1.86 KB
/
popup.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
// INITIALIZATION
// get the toggle element (toggles the extension functionality on/off)
let toggle = document.getElementById('toggle');
// set the value of the toggle switch to the value stored in the browser
chrome.storage.sync.get('enabled', ({enabled}) => {
toggle.checked = enabled;
});
reportStatus('opened popup');
// LISTENERS
toggle.addEventListener('change', async () => {
if(toggle.checked) { // changed to true
// set toggle value to true
chrome.storage.sync.set({enabled: true});
reportStatus('toggled on');
// get list of open tabs
let tabs = await chrome.tabs.query({currentWindow: true});
for(var i = 0; i < tabs.length; i++) {
let tab = tabs[i];
// if tab is valid site, attempt to remove counter from title
if(tab.url.includes('https://') || tab.url.includes('http://')) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
function: removeNotificationCounter
});
}
}
} else { // changed to false
// set toggle value to false
chrome.storage.sync.set({enabled: false});
reportStatus('toggled off');
}
});
// FUNCTIONS
// function to remove '(x)' from the beginning of a tab's title
function removeNotificationCounter() {
let title = document.title;
let l = title.indexOf('(')
let r = title.indexOf(')')
if (l !== -1 && r !== -1 && l < r) {
toRemove = title.slice(l, r+1)
title = title.replace(toRemove, "").replace(" ", " ").trim()
}
document.title = title;
}
// reports action and whether or not the extension is enabled or not
function reportStatus(msg) {
chrome.storage.sync.get(['enabled'], ({enabled}) => {
console.log({'status': msg, 'enabled': enabled});
});
}