forked from EFForg/privacybadger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlutils.js
More file actions
337 lines (289 loc) · 10.1 KB
/
Copy pathhtmlutils.js
File metadata and controls
337 lines (289 loc) · 10.1 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* This file is part of Privacy Badger <https://privacybadger.org/>
* Copyright (C) 2014 Electronic Frontier Foundation
*
* Privacy Badger is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Privacy Badger is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-env browser, jquery */
import { isIPv4, isIPv6, getBaseDomain } from "../lib/basedomain.js";
import constants from "./constants.js";
const i18n = chrome.i18n;
function escape_html(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
let htmlUtils = {
// default Tooltipster config
TOOLTIPSTER_DEFAULTS: {
delay: 100,
// allow per-instance option overriding
functionInit: function (instance, helper) {
let dataOptions = helper.origin.dataset.tooltipster;
if (dataOptions) {
try {
dataOptions = JSON.parse(dataOptions);
} catch (e) {
console.error(e);
}
for (let name in dataOptions) {
instance.option(name, dataOptions[name]);
}
}
},
},
/**
* Gets localized description for given domain and action.
*
* @param {String} action the action to get description for
* @param {String} fqdn the domain to get description for
* @param {?Array} [blockedFpScripts]
*
* @returns {String} the description
*/
getActionDescription: (function () {
const messages = {
block: i18n.getMessage('badger_status_block', "XXX"),
cookieblock: i18n.getMessage('badger_status_cookieblock', "XXX"),
blockedScripts: i18n.getMessage('badger_status_blocked_scripts', "XXX"),
noaction: i18n.getMessage('badger_status_noaction', "XXX"),
allow: i18n.getMessage('badger_status_allow', "XXX"),
dntTooltip: i18n.getMessage('dnt_tooltip')
};
return function (action, fqdn, blockedFpScripts) {
if (action.startsWith('user')) {
action = action.slice(5);
}
if (action == constants.DNT) {
return messages.dntTooltip;
}
if (blockedFpScripts) {
return messages.blockedScripts.replace("XXX", fqdn);
}
const rv_action = messages[action];
if (!rv_action) {
return fqdn;
}
return rv_action.replace("XXX", fqdn);
};
}()),
/**
* Gets HTML for domain action toggle switch.
*
* @param {String} fqdn the domain to get toggle for
* @param {String} action the current action of given domain
*
* @returns {String} the HTML for toggle switch
*/
getToggleHtml: (function () {
function is_checked(input_action, action) {
if (action == constants.NO_TRACKING || action == constants.DNT) {
action = constants.ALLOW;
}
return (input_action === action ? 'checked' : '');
}
let tooltips = {
block: i18n.getMessage('domain_slider_block_tooltip'),
cookieblock: i18n.getMessage('domain_slider_cookieblock_tooltip'),
allow: i18n.getMessage('domain_slider_allow_tooltip')
};
return function (fqdn, action) {
let id = fqdn.replace(/\./g, '-');
return `
<div class="switch-container ${action}">
<div class="switch-toggle switch-3 switch-candy">
<input id="block-${id}" name="${fqdn}" value="${constants.BLOCK}" type="radio" aria-label="${tooltips.block}" ${is_checked(constants.BLOCK, action)}>
<label title="${tooltips.block}" class="tooltip" for="block-${id}"></label>
<input id="cookieblock-${id}" name="${fqdn}" value="${constants.COOKIEBLOCK}" type="radio" aria-label="${tooltips.cookieblock}" ${is_checked(constants.COOKIEBLOCK, action)}>
<label title="${tooltips.cookieblock}" class="tooltip" for="cookieblock-${id}"></label>
<input id="allow-${id}" name="${fqdn}" value="${constants.ALLOW}" type="radio" aria-label="${tooltips.allow}" ${is_checked(constants.ALLOW, action)}>
<label title="${tooltips.allow}" class="tooltip" for="allow-${id}"></label>
<a></a>
</div>
</div>
`.trim();
};
}()),
/**
* Returns the HTML for the EFF's Do Not Track policy compliance declaration icon.
* @returns {String}
*/
getDntIconHtml: (function () {
let dnt_icon_url = chrome.runtime.getURL('/icons/dnt-16.png');
return function () {
return `
<div class="dnt-compliant">
<a target=_blank href="https://privacybadger.org/#-I-am-an-online-advertising-tracking-company.--How-do-I-stop-Privacy-Badger-from-blocking-me"><img src="${dnt_icon_url}"></a>
</div>
`.trim();
};
}()),
/**
* Generates HTML for given FQDN.
*
* @param {String} fqdn the FQDN to get HTML for
* @param {String} action the action for given FQDN
* @param {Boolean} [show_breakage_warning]
* @param {Boolean} [show_breakage_note]
* @param {?Array} [blockedFpScripts]
*
* @returns {String} the slider HTML for the FQDN
*/
// TODO origin --> domain/FQDN
getOriginHtml: (function () {
const breakage_warning_tooltip = i18n.getMessage('breakage_warning_tooltip'),
undo_arrow_tooltip = i18n.getMessage('feed_the_badger_title');
return function (fqdn, action, show_breakage_warning, show_breakage_note, blockedFpScripts) {
action = escape_html(action);
fqdn = escape_html(fqdn);
// Get classes for main div.
let classes = ['clicker'];
// show warning when manually blocking a domain
// that would have been cookieblocked otherwise
if (show_breakage_warning) {
classes.push('show-breakage-warning');
}
if (show_breakage_note) {
classes.push('breakage-note');
}
// manually-set sliders get an undo arrow
if (action.startsWith('user')) {
classes.push('userset');
action = action.slice(5);
}
// show the DNT icon for DNT-compliant domains
let dnt_html = '';
if (action == constants.DNT) {
dnt_html = htmlUtils.getDntIconHtml();
}
let shield_icon = '';
if (blockedFpScripts) {
shield_icon = '<span class="ui-icon ui-icon-shield"></span>';
}
// construct HTML for domain
let domain_tooltip = htmlUtils.getActionDescription(action, fqdn, blockedFpScripts);
return `
<div class="${classes.join(' ')}" data-origin="${fqdn}">
<div class="origin" role="heading" aria-level="4">
<span class="ui-icon ui-icon-alert tooltip breakage-warning" title="${breakage_warning_tooltip}"></span>
<span class="origin-inner tooltip" title="${domain_tooltip}">${fqdn}${dnt_html}${shield_icon}</span>
</div>
${htmlUtils.getToggleHtml(fqdn, action, blockedFpScripts)}
<a href="" class="honeybadgerPowered tooltip" title="${undo_arrow_tooltip}"></a>
<a href="" class="removeOrigin">✖</a>
</div>
`.trim();
};
}()),
/**
* Toggles undo arrows and breakage warnings in domain slider rows.
* TODO rename/refactor with updateOrigin()
*
* @param {jQuery} $clicker
* @param {Boolean} userset whether to show a revert control arrow
* @param {Boolean} show_breakage_warning whether to show a breakage warning
*/
toggleBlockedStatus: function ($clicker, userset, show_breakage_warning) {
$clicker.removeClass([
"userset",
"show-breakage-warning",
].join(" "));
// toggles revert control arrow via CSS
if (userset) {
$clicker.addClass("userset");
}
// show warning when manually blocking a domain
// that would have been cookieblocked otherwise
if (show_breakage_warning) {
$clicker.addClass("show-breakage-warning");
}
},
/**
* Compare two domains, reversing them to start comparing the least
* significant parts (TLD) first.
*
* @param {Array} domains The domains to sort.
* @returns {Array} Sorted domains.
*/
sortDomains: (domains) => {
domains = domains || [];
// optimization: cache makeSortable output by walking the array once
// to extract the actual values used for sorting into a temporary array
return domains.map((domain, i) => {
return {
index: i,
value: htmlUtils.makeSortable(domain)
};
// sort the temporary array
}).sort((a, b) => {
if (a.value > b.value) {
return 1;
}
if (a.value < b.value) {
return -1;
}
return 0;
// walk the temporary array to achieve the right order
}).map(item => domains[item.index]);
},
/**
* Reverse order of domain items to have the least exact (TLD) first.
*
* @param {String} domain The domain to shuffle
* @returns {String} The 'reversed' domain
*/
makeSortable: (domain) => {
let base = getBaseDomain(domain),
base_minus_tld = base,
dot_index = base.indexOf('.'),
rest_of_it_reversed = '';
if (domain.length > base.length) {
rest_of_it_reversed = domain
.slice(0, domain.length - base.length - 1)
.split('.').reverse().join('.');
}
if (dot_index > -1 && !isIPv4(domain) && !isIPv6(domain)) {
base_minus_tld = base.slice(0, dot_index);
}
return (base_minus_tld + '.' + rest_of_it_reversed);
},
/**
* Checks whether an element is at least partially visible
* within its scrollable container.
*
* @param {Element} elm
* @param {Element} container
*
* @returns {Boolean}
*/
isScrolledIntoView: (elm, container) => {
let ctop = container.scrollTop,
cbot = ctop + container.clientHeight,
etop = elm.offsetTop,
ebot = etop + elm.clientHeight;
// completely in view
if (etop >= ctop && ebot <= cbot) {
return true;
}
// partially in view
if ((etop < ctop && ebot > ctop) || (ebot > cbot && etop < cbot)) {
return true;
}
return false;
},
};
htmlUtils.escape = escape_html;
export default htmlUtils;