forked from TheBallOfAeolus/gnu_terry_pratchett
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground-script.js
161 lines (146 loc) · 3.97 KB
/
background-script.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var currentTab;
var pages = [];
var clacks = [];
var current_clacks;
var has_clacks;
var pos = 0;
var loop = 0;
function updateIcon() {
browser.browserAction.setIcon({
path: has_clacks ? {
"16": "data/Clacks16/END.png",
"32": "data/Clacks32/END.png",
"64": "data/Clacks64/END.png"
} : {
"16": "data/Clacks16/BLANK.png",
"32": "data/Clacks32/BLANK.png",
"64": "data/Clacks64/BLANK.png"
},
tabId: currentTab.id
});
browser.browserAction.setTitle({
// Screen readers can see the title
title: has_clacks ? current_clacks : "Nothing in the overhead" ,
tabId: currentTab.id
});
}
function checkTime() {
if (has_clacks == true) {
loop = loop + 1;
if (loop > 4) { loop = 0; }
if (loop < 1) {
browser.browserAction.setIcon({
path: {
"16": "data/Clacks16/BLANK.png",
"32": "data/Clacks32/BLANK.png",
"64": "data/Clacks64/BLANK.png"
},
tabId: currentTab.id
});
pos = pos + 1;
} else {
if (current_clacks.length-1 < pos) { pos = 0; };
if (current_clacks.charAt(pos) == " ") {
browser.browserAction.setIcon({
path: {
"16": "data/Clacks16/SPACE.png",
"32": "data/Clacks32/SPACE.png",
"64": "data/Clacks64/SPACE.png"
},
tabId: currentTab.id
});
} else {
if (current_clacks.charAt(pos) == "+") {
browser.browserAction.setIcon({
path: {
"16": "data/Clacks16/END.png",
"32": "data/Clacks32/END.png",
"64": "data/Clacks64/END.png"
},
tabId: currentTab.id
});
} else {
try {
browser.browserAction.setIcon({
path: {
"16": "data/Clacks16/"+current_clacks.charAt(pos).toUpperCase()+".png",
"32": "data/Clacks32/"+current_clacks.charAt(pos).toUpperCase()+".png",
"64": "data/Clacks64/"+current_clacks.charAt(pos).toUpperCase()+".png"
},
tabId: currentTab.id
});
} catch (ex4) {
browser.browserAction.setIcon({
path: {
"16": "data/Clacks16/SPACE.png",
"32": "data/Clacks32/SPACE.png",
"64": "data/Clacks64/SPACE.png"
},
tabId: currentTab.id
});
}
}
}
}
}
}
function updateActiveTab(tabs) {
function updateTab(tabs) {
if (tabs[0]) {
currentTab = tabs[0];
var l = pages.indexOf(currentTab.url);
if (l == -1) {
current_clacks = "Nothing in the overhead";
has_clacks = false;
} else {
current_clacks = clacks[l];
has_clacks = true;
}
updateIcon();
}
}
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
gettingActiveTab.then(updateTab);
}
function setGNU(e) {
for (var header of e.responseHeaders) {
if (header.name.toLowerCase() == "x-clacks-overhead") {
if (pages.indexOf(e.url) == -1) {
pages.push(e.url);
clacks.push("+"+header.value);
}
}
}
return {responseHeaders: e.responseHeaders};
}
// Listen for onHeaderReceived for the target page.
// Set "blocking" and "responseHeaders".
browser.webRequest.onHeadersReceived.addListener(
setGNU,
{urls: ["<all_urls>"]},
["responseHeaders"]
);
function openPage() {
browser.tabs.create({
url: "http://www.gnuterrypratchett.com/"
});
}
function notify(message) {
if (pages.indexOf(message.url) == -1) {
pages.push(message.url);
clacks.push("+"+message.value);
}
updateActiveTab();
}
browser.runtime.onMessage.addListener(notify);
browser.browserAction.onClicked.addListener(openPage);
// listen to tab URL changes
browser.tabs.onUpdated.addListener(updateActiveTab);
// listen to tab switching
browser.tabs.onActivated.addListener(updateActiveTab);
// listen for window switching
browser.windows.onFocusChanged.addListener(updateActiveTab);
browser.alarms.onAlarm.addListener(checkTime);
browser.alarms.create('checkTime', {periodInMinutes: 0.02});
// update when the extension loads initially
updateActiveTab();