-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
74 lines (61 loc) · 1.93 KB
/
Copy pathbackground.js
File metadata and controls
74 lines (61 loc) · 1.93 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
(function() {
'use strict';
const request_url = 'https://github.com/opensearch.xml?n=';
const cycle_time = 15000;
const request_timeout = 3000;
const states = {
active: {
color: '#080',
badge_text: '\u2714',
title: 'It seems that everything is working fine!',
},
inactive: {
color: '#888',
badge_text: ' ',
title: 'Inactive',
},
interrupt: {
color: '#f80',
badge_text: '\u2716',
title: 'Last request failed. No need to do anything, we will keep trying.',
},
};
var is_active = true;
var request_to_github = function() {
if (!is_active) {
return;
}
var n = parseInt(Math.random() * 1e16);
var url = request_url + n;
var xhttp = new XMLHttpRequest();
xhttp.timeout = request_timeout;
xhttp.onreadystatechange = function() {
if (!is_active || this.readyState != 4) {
return;
}
set_state(this.status == 200 ? 'active' : 'interrupt');
};
xhttp.open('GET', url, true);
xhttp.send();
};
var toggle_activity = function() {
is_active = !is_active;
set_state(is_active ? 'active' : 'inactive');
if (is_active) {
request_to_github();
}
};
var set_state = function(state_name) {
var state = states[state_name];
chrome.browserAction.setBadgeText({text: state.badge_text});
chrome.browserAction.setBadgeBackgroundColor({color: state.color});
chrome.browserAction.setTitle({title: 'Github Keep-Alive\n' + state.title});
};
var init = function() {
set_state('active');
chrome.browserAction.onClicked.addListener(toggle_activity);
setInterval(request_to_github, cycle_time);
request_to_github();
};
init();
})();