-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
77 lines (63 loc) · 2.14 KB
/
background.js
File metadata and controls
77 lines (63 loc) · 2.14 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
// if (/^https:\/\/www\.google/.test(highlighted_tab.url)) { }
let blockList = []
let block = false
const BLOCK_STATE = 'BLOCK_MODE'
const UNBLOCK_STATE = 'UNBLOCK_MODE'
chrome.storage.local.get(null, function (items) {
for (let keys in items) {
blockList.push(items[keys])
}
})
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (var key in changes) {
var storageChange = changes[key]
blockList.push(storageChange.newValue)
blockList = blockList.filter(ele => ele != storageChange.oldValue)
}
})
// Compares the URL of a tab to see if it's included in the blockList.
function compareURL(tabURL) {
for (let i = 0; i < blockList.length; i++) {
if (tabURL.includes(blockList[i]))
return true;
}
return false;
}
// Adds a listener which gets called when a tab is updated (loaded, refreshed, etc)
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
/*
chrome.tabs.get(tabId, function (tab) {
console.log(tab.url)
})
*/
if (block) {
if (compareURL(tab.url)) {
chrome.tabs.executeScript(null, { file: './block.js' })
}
}
})
let timer; // Keeps track of the Date() of when the timer will complete
let timerAlert; // callback function to alert the user
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.msg === 'GET_TIME') {
sendResponse({ time: timer })
}
})
chrome.runtime.onConnect.addListener(function (port) {
console.assert(port.name === 'createTimer')
port.onMessage.addListener(function (msg) {
if (msg.message === 'START_TIME') {
block = !block
if (block)
port.postMessage({ state: BLOCK_STATE })
if (!block)
port.postMessage({ state: UNBLOCK_STATE })
} else if (msg.when != null) {
timer = new Date(msg.when)
timerAlert = setTimeout(function () {
alert('Time\'s up!')
}, timer - Date.now()
)
}
})
})