-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
61 lines (53 loc) · 1.9 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
let tabsTimer = {};
const audio = new Audio('assests/wompwomp.mp3');
chrome.runtime.onInstalled.addListener(() => {
tabsTimer = {};
chrome.storage.sync.set({ color: '#3aa757' }, () => {
console.log('The color is what.');
});
});
chrome.tabs.onCreated.addListener((currTab) => {
chrome.tabs.query({ currentWindow: true, active: false }, (tabs) => {
tabs.forEach((tabObj) => {
if (!tabsTimer[tabObj.id]) {
chrome.alarms.create(`timer${tabObj.id}`, { delayInMinutes: 1 });
tabsTimer[tabObj.id] = `timer${tabObj.id}`;
// window.alert(tabsTimer[tabObj.id]);
} else {
// chrome.alarms.clear(`timer${tabObj.id}`);
// chrome.alarms.create(`timer${tabObj.id}`, { delayInMinutes: 1 });
}
});
});
});
// onHighlighted but not onCreated to make a change to alarm
chrome.tabs.onHighlighted.addListener((highlighted) => {
if (tabsTimer[highlighted.tabIds]) {
chrome.alarms.clear(`timer${highlighted.tabIds}`);
delete tabsTimer[highlighted.tabIds];
}
chrome.tabs.query({ currentWindow: true, active: false }, (tabs) => {
// window.alert(`this current${highlighted.tabIds}`);
tabs.forEach((tabObj) => {
if (!tabsTimer[tabObj.id]) {
chrome.alarms.create(`timer${tabObj.id}`, { delayInMinutes: 1 });
tabsTimer[tabObj.id] = `timer${tabObj.id}`;
} else {
// chrome.alarms.clear(`timer${tabObj.id}`);
// chrome.alarms.create(`timer${tabObj.id}`, { delayInMinutes: 1 });
}
});
});
});
chrome.alarms.onAlarm.addListener((alarmDone) => {
// window.alert(`alarmDone name: ${alarmDone.name}`);
Object.entries(tabsTimer).forEach((tabEntry) => {
if (tabEntry[1] === alarmDone.name) {
// window.alert(`tab id: ${typeof tabEntry[0]}`);
chrome.tabs.remove(parseInt(tabEntry[0], 10));
delete tabsTimer[tabEntry[0]];
audio.currentTime = 0;
audio.play();
}
});
});