forked from Cimbali/CleanLinks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanlink.js
447 lines (364 loc) · 13 KB
/
cleanlink.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* ***** BEGIN LICENSE BLOCK *****
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/
*
* The Original Code is CleanLinks Mozilla Extension.
*
* The Initial Developer of the Original Code is
* Copyright (C)2012 Diego Casorran <[email protected]>
* All Rights Reserved.
*
* ***** END LICENSE BLOCK ***** */
const _ = browser.i18n.getMessage
const attr_cleaned_count = 'data-cleanedlinks';
const attr_cleaned_link = 'data-cleanedlink';
const str_cleanlink_touch = "\n\n- " + _("browser_touch");
const title = browser.runtime.getManifest().name;
const version = browser.runtime.getManifest().version;
const homepage = browser.runtime.getManifest().homepage_url;
const copyright = browser.runtime.getManifest().author;
const icon_default = '';
const icon_disabled = '-';
const icon_fire = '~';
const icon_green = '!';
const same_tab = 0;
const new_tab = 1;
const new_window = 2;
var prefValues = {
enabled : true,
skipwhen : new RegExp('/ServiceLogin|imgres\\?|searchbyimage\\?|watch%3Fv|auth\\?client_id|signup|bing\\.com/widget|'
+ 'oauth|openid\\.ns|\\.mcstatic\\.com|sVidLoc|[Ll]ogout|submit\\?url=|magnet:'),
remove : /(?:ref|aff)\\w*|utm_\\w+|(?:merchant|programme|media)ID/,
skipdoms : ['accounts.google.com', 'docs.google.com', 'translate.google.com',
'login.live.com', 'plus.google.com', 'twitter.com',
'static.ak.facebook.com', 'www.linkedin.com', 'www.virustotal.com',
'account.live.com', 'admin.brightcove.com', 'www.mywot.com',
'webcache.googleusercontent.com', 'web.archive.org', 'accounts.youtube.com',
'signin.ebay.com'],
highlight : true, // highlight cleaned links
hlstyle : 'background:rgba(252,252,0,0.6); color: #000', // style for highlighted cleaned links
evdm : true, // Event Delegation Mode: whether we clean on click
// (vs. preventively/recursively cleaning all links)
progltr : true, // http-on-examine-response: clean links on Location: redirect headers?
httpomr : true, // http-on-modify-request: skip redirects
cbc : true, // Context menus to clean links
gotarget : false, // whether we respect target attributes on links that are being cleaned
repdelay : 3, // delay before we call recursiveCleanLinksInDoc again (to handle ajax links)
textcl : false, // search for & clean links in selected text
ignhttp : false, // ignore non-http(s?) links
cltrack : true, // whether we track the link cleaning
switchToTab : true, // Should be a copy of the browser preference: switch to a new tab when we open a link?
notifications: true, // Send notifications when tracking links?
debug : false
}
function log()
{
if (prefValues.debug) console.log.call(arguments)
}
function serializeOptions()
{
return Object.keys(prefValues).reduce((serializedVals, param) =>
{
if (prefValues[param] instanceof RegExp)
serializedVals[param] = prefValues[param].source;
else if (Array.isArray(prefValues[param]))
serializedVals[param] = prefValues[param].join(',');
else
serializedVals[param] = prefValues[param];
return serializedVals;
}, {});
}
function loadOptions()
{
// return the promise so it can be chained
return browser.storage.local.get('configuration').then(data =>
{
if ('configuration' in data) {
for (var param in data.configuration) {
if (typeof prefValues[param] != 'object')
prefValues[param] = data.configuration[param];
else if (prefValues[param] instanceof RegExp)
prefValues[param] = new RegExp(data.configuration[param]);
else if (Array.isArray(prefValues[param]))
prefValues[param] = data.configuration[param].split(',').map(s => s.trim()).filter(s => s.length > 0);
}
}
});
}
function highlightLink(node, remove)
{
// parse and apply ;-separated list of key:val style properties
('' + prefValues.hlstyle).split(';').forEach(function (r)
{
let [prop, val] = r.split(':').map(String.trim);
node.style.setProperty(prop, remove ? '' : val, 'important');
});
}
function cleanLink(link, base)
{
if (!link || link.startsWith("view-source:") || (prefValues.skipwhen && prefValues.skipwhen.test(link)))
{
log('not cleaning', link, ': empty, source, or matches skipwhen');
return link;
}
if (typeof base == 'undefined')
{
if (/^https?:/.test(link))
base = link;
else if (window)
{
base = new URL(window.location);
base.hash = '';
}
}
if (typeof base === 'string')
base = new URL(base);
let linkURL;
if (prefValues.skipdoms)
{
try
{
linkURL = new URL(link, 'href' in base ? base.href : base);
if (prefValues.skipdoms.indexOf(linkURL.host) !== -1)
{
log('not cleaning', link, ': host in skipdoms');
return link;
}
}
catch (e) {}
}
if (prefValues.ignhttp && !(/^https?:/.test(typeof linkURL != 'undefined' ? linkURL.href : link)))
{
log('not cleaning', link, ': ignoring non-http(s) links');
return link;
}
if (/\.google\.[a-z.]+\/search\?(?:.+&)?q=http/i.test(link)
|| /^https?:\/\/www\.amazon\.[\w.]+\/.*\/voting\/cast\//.test(link)
)
{
log('not cleaning', link, ': google search/amazon vote')
return link;
}
let s = 0,
origLink = link,
isYahooLink = /\.yahoo.com$/.test(base.host);
link = link.replace(/^javascript:.+(["'])(https?(?:\:|%3a).+?)\1.*/gi, (all, quote, quotedLink) => (++s, quotedLink));
if (/\b((?:aHR0|d3d3)[A-Z0-9+=\/]+)/gi.test(link))
{
try
{
let r = RegExp.$1;
if (isYahooLink)
r = r.replace(/\/RS.*$/, '');
let decoded = decodeURIComponent(atob(r));
if (decoded)
link = '=' + decoded;
}
catch (e)
{
log('Invalid base64 data in link', link, ':' , r, '-- error is', e);
}
}
else
{
switch (base.host)
{
case 'www.tripadvisor.com':
if (link.indexOf('-a_urlKey') !== -1)
link = '=' + decodeURIComponent(link.replace(/_+([a-f\d]{2})/gi, '%$1')
.replace(/_|%5f/ig, '')).split('-aurl.').pop().split('-aurlKey').shift();
break;
default:
switch (linkURL && linkURL.host || (link.match(/^\w+:\/\/([^/]+)/) || []).pop())
{
case 'redirect.disqus.com':
if (link.indexOf('/url?url=') !== -1)
link = '=' + link.match(/url\?url=([^&]+)/).pop().split(/%3a[\w-]+$/i).shift();
break;
}
}
}
let lmt = 4;
while (--lmt && (/(?:.\b|3D)([a-z]{2,}(?:\:|%3a)(?:\/|%2f){2}.+)$/i.test(link) || /(?:[?=]|[^\/]\/)(www\..+)$/i.test(link)))
{
let pos;
link = RegExp.$1;
if ((pos = link.indexOf('&')) !== -1)
link = link.substr(0, pos);
link = decodeURIComponent(link);
console.log('decoded URI Component =', link)
if ((pos = link.indexOf('html&')) !== -1 || (pos = link.indexOf('html%')) !== -1)
link = link.substr(0, pos + 4);
else if ((pos = link.indexOf('/&')) !== -1) // || (pos = link.indexOf('/%')) !== -1)
link = link.substr(0, pos);
if (link.indexOf('://') == -1)
link = 'http://' + link;
if (link.indexOf('/', link.indexOf(':') + 2) == -1)
link += '/';
++s;
}
link = link.replace(/^h[\w*]+(ps?):/i, 'htt$1:');
if (origLink != link)
{
try
{
new URL(link);
}
catch (e)
{
log('not cleaning', origLink, ': yielded invalid link :', link)
link = origLink;
}
}
if (isYahooLink)
link = link.replace(/\/R[KS]=\d.*$/, '');
prefValues.remove.lastIndex = 0;
if (s || prefValues.remove.test(link))
{
let pos, ht = null;
if ((pos = link.indexOf('#')) !== -1)
ht = link.substr(pos), link = link.substr(0, pos);
link = link.replace(/&/g, '&').replace(prefValues.remove, '').replace(/[?&]$/, '')
+ (ht && /^[\w\/#!-]+$/.test(ht) ? ht : (this.cleanOnClick ? '' : '#'));
}
log('cleaning', origLink, ':', link)
return link;
}
function cleanLinksInDoc(doc)
{
if (typeof doc == 'undefined')
doc = document;
if (typeof doc == 'undefined')
return;
let links = doc.getElementsByTagName('a'),
nCleanedLinks = 0;
for (let l = 0, link = links[l]; l < links.length; link = links[++l])
{
let href = link.href,
cleanedHref = cleanLink(href, link.baseURI);
if (href != cleanedHref)
{
++nCleanedLinks;
if (!(link.hasAttribute(attr_cleaned_link)))
{
link.setAttribute(attr_cleaned_link, link.href);
let t = link.hasAttribute('title') ? link.getAttribute('title') : '';
link.setAttribute('title', (t + str_cleanlink_touch).replace(/^\s+/, ''));
}
link.setAttribute('href', cleanedHref);
// alternately: {text-decoration: underline dotted #9f9f8e !important;}
link.style.setProperty('border-bottom', '1px dotted #9f9f8e', 'important');
if (prefValues.highlight)
highlightLink(link);
}
// TODO: OK-looking links (fine URL etc.): remove events. E.g. Google replaces
// @onmousedown https://foo/ with https://google.com/url?...;url=https://foo/
}
browser.runtime.sendMessage({ 'cleaned': nCleanedLinks });
return nCleanedLinks;
}
function undoCleanLinksInDoc(doc)
{
if (typeof doc == 'undefined')
doc = document;
if (typeof doc == 'undefined')
return;
let links = doc.getElementsByTagName('a'),
c = links.length;
for (let l = 0, link = links[l]; l < links.length; link = links[++l])
{
if (link.hasAttribute(attr_cleaned_link))
{
link.setAttribute('href', link.getAttribute(attr_cleaned_link));
link.setAttribute('title', link.getAttribute('title').replace(str_cleanlink_touch.replace(/^\s+/, ''), '').replace(/\s+$/, ''));
link.style.setProperty('border-bottom', '0px', 'important');
// remove highlight styling
if (prefValues.highlight)
highlightLink(link, true);
}
}
//doc.body.setAttribute(attr_cleaned_count, 0);
browser.runtime.sendMessage({ 'cleaned': 0 });
return 0;
}
function textFindLink(node)
{
let pos, selection = node.ownerDocument && node.ownerDocument.defaultView.getSelection();
// if selection has a node with data, and we can get the offset in that data
if (selection && selection.isCollapsed && selection.focusNode && selection.focusNode.data && (pos = selection.focusOffset))
{
// unsanitized content of selection
let content = selection.focusNode.data.substr(--pos);
// sanitize selection: remove 0-space tags, replace other tags with spaces
let text = node.innerHTML.replace(/<\/?wbr>/ig, '').replace(/<[^>]+?>/g, ' ');
// recover position of selection in sanitized text
pos = text.indexOf(content) + 1;
if (pos === 0)
{
text = node.textContent;
pos = text.indexOf(content) + 1;
}
// tools to modify boundaries of selection, until a reasonable url
let boundaryChars = ' "\'<>\n\r\t()[]|',
protectedBoundaryChars = boundaryChars.replace(/(.)/g, '\\$1'),
trimEndRegex = RegExp("[" + protectedBoundaryChars + "]+$");
// move start of selection backwards until start of data or a boundary character
while (pos && boundaryChars.indexOf(text[pos]) === -1)
--pos;
text = (pos && text.substr(++pos) || text)
text = text.match(/^\s*(?:\w+:\/\/|www\.)[^\s">]{4,}/)
if (text)
{
text = text.shift().trim().replace(trimEndRegex, '');
if (text.indexOf('://') === -1)
text = 'http://' + text;
}
return text;
}
return undefined;
}
function cleanRedirectHeaders(details)
{
if (30 != parseInt(details.statusCode / 10) || 304 == details.statusCode)
return {};
var loc = details.responseHeaders.find(element => element.name.toLowerCase() == 'location')
if (!loc || !loc.value)
return {};
var dest = loc.value, cleanDest = cleanLink(dest, details.url);
/* NB. XUL code seemed to mark redirected requests, due to infinite redirections on *.cox.net,
* see #13 & 8c280b7. However it is not clear whether this is necessary nor how to do this in webexts.
*
* NB2. XUL code also protected against "The page isn't redirecting properly" errors with the following:
if (cleanDest != details.url)
return {}
*/
if (cleanDest == dest)
return {};
handleMessage({ url: cleanDest, orig: dest });
return {redirectUrl: cleanDest};
}
function onRequest(details)
{
var dest = details.url, curLink = details.originUrl, cleanDest = cleanLink(dest, curLink)
if (!cleanDest)
return {};
/* NB. XUL code allowed requests when destination is self, to protect against infinite loops (see 42106fd).
else if (cleanDest == curLink)
return {};
*/
/* NB? XUL code prevented iframe redirections back to top-level document (see 182e58e):
* allow if top level frame, or loading to a different domain than the one in the top frame
else if (details.documentUrl === null || details.documentUrl === details.originUrl
|| new URL(details.documentUrl).host != new URL(cleanDest).host)
return (cleanDest == dest ? {} : {redirectUrl: cleanDest});
// Otherwise just cancel
else
return {cancel: true}
*/
if (cleanDest == dest)
return {};
handleMessage({ url: cleanDest, orig: dest });
return {redirectUrl: cleanDest};
}