This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
/
background.js
283 lines (257 loc) · 6.77 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"use strict";
/**
* background.js - persistent state and 'all tabs and sites' effects
*
* the webExtenion is only started up the user turns the pref on in bootstrap.js
*/
// constants used by particular effects
const WORDS =
`
puppet
one
zero
congo
rewind
71
fsociety
encrypt
decrypt
control
illusion
dark
army
evil
robot
disintegration
hack
society
white
rose
revolution
subroutine
backdoor
undo
society
corporation
economy
mask
system
truth
debt
cryptocurrency
kernel
panic
privacy
money
data
fuck
`.trim().split(/\s+/);
const XHEADERSITES = [
"https://www.red-wheelbarrow.com/forkids/*",
"https://www.whatismybrowser.com/detect/*",
"https://red-wheelbarrow-stage.apps.nbcuni.com/forkids/*"
];
const XHEADERNAME = 'x-1057';
const XHEADERVALUE = 'true'
/**
* Affect page views for all urls.
*
* In particular, the first time any word from wordArray
* is seen during a session, apply special styling to it.
*/
class PersistentPageModificationEffect {
/**
* wordArray: array of words to apply effect to
* CSS: string. Style for .donotdelete classname
*
* sets up:
* this.
* wordSet: Set() of words to 'affect'
* CSS
*/
constructor(wordArray, CSS) {
if (!CSS) CSS = `
.donotdelete {
transform: scaleY(-1);
/* scaleY does not work for inline elements */
display: inline-block;
}
.donotdelete-revert {
transform: scaleY(1);
transition: transform 1s ease-in;
}
/* after revert, revert the tooltip */
.donotdelete-revert .donotdelete-tooltip {
transform: scaleY(1) translateY(-50%);
transition: transform 1s ease-in;
}
.donotdelete-tooltip {
visibility: hidden !important;
display: inline-block;
position: absolute;
min-height: 50px;
max-width: 200px;
min-width: 200px;
padding: 5px;
text-align: center;
border-radius: 3px;
border: 1px solid #bebdbd;
box-shadow: var(--standard-box-shadow);
z-index: 50 !important;
/* Neutralizing styles */
font-style: normal !important;
transform: scaleY(-1) translateY(50%);
color: black !important;
text-shadow: none !important;
line-height: 16px !important;
font-family: sans-serif !important;
font-size: 12px !important;
font-weight: normal !important;
background-color: #ffffff !important;
letter-spacing: 0 !important;
text-transform: none !important;
}
.donotdelete-tooltip a {
color: blue !important;
text-decoration: underline !important;
font-weight: normal !important;
}
/* Neutralizes case where <a> have after pseudoelements like ">>" */
.donotdelete-tooltip a::after {
display: none;
}
/*
* Ensures hover effect is always on top; needed when two or more
* match words with hover effects occur directly next to one another
* in a given text node
*/
.donotdelete:hover {
z-index: 999 !important;
}
/* Show the tooltip when hovering */
.donotdelete:hover .donotdelete-tooltip {
visibility: visible !important;
}
/* Vertical centering */
[data-tooltip-position] {
top: 50%;
/*
* transform: translateY(-50%);
* (if we weren't already transforming this element)
*/
}
[data-tooltip-position="right"] {
left: 100%;
margin-left: 0;
}
[data-tooltip-position="left"] {
right: 100%;
margin-right: 0;
}`;
this.wordSet = new Set(wordArray);
this.insertCSSOnAllTabs().then(() => this.addListeners());
this.portFromCS = null;
this.APPLICABLE_PROTOCOLS = ["http:", "https:", "file:"];
this.CSS = {
code: CSS
};
}
addListeners() {
browser.runtime.onConnect.addListener((port) => this.connected(port));
}
/**
* msg: one of
* getList => returns this.wordSet (list of words)
* wordUsed => remove a word from this.wordSet
*/
connected(p) {
this.portFromCS = p;
this.portFromCS.postMessage({type: "backgroundConnected"});
this.portFromCS.onMessage.addListener((m) => {
switch (m.type) {
case "getList":
this.portFromCS.postMessage({ type: "getList", data: this.wordSet });
break;
case "wordUsed":
// remove word from the list
this.wordSet.delete(m.word);
break;
default:
throw new Error(`Message type not recognized: ${m.type}`);
}
});
}
/**
* Ensure that the CSS modification happens for all open and future tabs
*/
async insertCSSOnAllTabs() {
// When first loaded, add CSS for open tabs.
var gettingAllTabs = browser.tabs.query({});
gettingAllTabs.then(async (tabs) => {
for (let tab of tabs) {
if (this.protocolIsApplicable(tab.url)) {
await browser.tabs.insertCSS(tab.id, this.CSS);
}
}
});
// Each time a tab is updated, add CSS for that tab.
browser.tabs.onUpdated.addListener(async (id, changeInfo, tab) => {
if (this.protocolIsApplicable(tab.url) && tab.status === "complete") {
await browser.tabs.insertCSS(id, this.CSS);
}
});
}
/*
* Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.
*/
protocolIsApplicable(url) {
var anchor = document.createElement('a');
anchor.href = url;
return this.APPLICABLE_PROTOCOLS.includes(anchor.protocol);
}
}
/**
* For certain domains, add a special header, using blocking requestHeaders API
*
* https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onBeforeSendHeaders
*/
class AddHeaderForSpecialPage {
/**
* matchingUrls: array of url matches
* headerName: string single x-header to add to request
* value: value for the header value
*/
constructor(matchingUrls, headerName, headerValue) {
this.matchingUrls = matchingUrls;
this.headerName = headerName;
this.headerValue = headerValue;
this.registerHeaderListener();
}
listener(details) {
details.requestHeaders.push({name: this.headerName, value: this.headerValue});
return {requestHeaders: details.requestHeaders};
}
registerHeaderListener() {
browser.webRequest.onBeforeSendHeaders.addListener(
this.listener.bind(this),
{urls: this.matchingUrls},
["blocking", "requestHeaders"] // blocking = wait before sending
)
}
}
/**
* Once per addon startup, creates the effects
*/
async function runOnce() {
new PersistentPageModificationEffect(
WORDS
);
new AddHeaderForSpecialPage(
XHEADERSITES,
XHEADERNAME,
XHEADERVALUE
);
// when deciding whether to do the effect, check if allowed?
}
// actually start, once per run.
runOnce();