-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
106 lines (95 loc) · 2.63 KB
/
background.js
File metadata and controls
106 lines (95 loc) · 2.63 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
var windowId = null;
var tabId = null;
var interval = null;
var errors = null;
chrome.tabs.onUpdated.addListener(function(id, changeInfo, tab) {
if (tabId === null) return;
if (id === tabId && changeInfo.status == 'complete') {
chrome.tabs.sendMessage(tabId, { name: 'toggle-in-page-toolbar' });
}
});
function toggleToolbar() {
chrome.tabs.getSelected(windowId, (tab) => {
tabId = tab.id;
});
}
function respondsWith200(url) {
try {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', url, false);
xmlHttp.send(null);
return xmlHttp.status === 200 || xmlHttp.status === 204;
} catch (e) {
return false;
}
}
function openWindow(targetAddress) {
var createData = {
url: targetAddress,
focused: true,
type: 'popup'
};
chrome.windows.create(
createData,
(window) => {
windowId = window.id;
toggleToolbar();
});
}
function setNA(targetAddress) {
chrome.browserAction.setBadgeText({ text: 'N/A' });
chrome.browserAction.setBadgeBackgroundColor({ color: '#dd3f3a' });
if (windowId === null) {
openWindow(targetAddress);
}
}
function setOK() {
chrome.browserAction.setBadgeText({ text: 'OK' });
chrome.browserAction.setBadgeBackgroundColor({ color: '#5cb85c' });
if(windowId !== null) {
chrome.windows.remove(windowId);
windowId = null;
}
}
function checkStatus(config) {
var unavailableAddresses = [];
for(var address of config.addresses) {
if(!respondsWith200(address)) {
unavailableAddresses.push(address);
}
}
if(unavailableAddresses.length > 0) {
errors = unavailableAddresses;
setNA(config.targetAddress);
}
else {
errors = null;
setOK();
}
}
function poll() {
clearInterval(interval);
chrome.storage.local.get({
addresses: [],
targetAddress: 'https://www.youtube.com/watch?v=wZZ7oFKsKzY',
pollingInterval: 60
},
(config) => {
if(config.addresses.length === 0) return;
checkStatus(config);
var intervalInSeconds = config.pollingInterval * 1000;
interval = setInterval(checkStatus, intervalInSeconds, config);
});
}
chrome.browserAction.onClicked.addListener(() => chrome.runtime.openOptionsPage());
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.name == 'overlay-initialized'){
console.log('UPDATE OVERLAY');
setTimeout(() => {
chrome.tabs.sendMessage(sender.tab.id, { name: 'overlay-errors', errors: errors });
}, 1000);
}
});
poll();