-
Notifications
You must be signed in to change notification settings - Fork 26
/
contentscript.js
300 lines (282 loc) · 10.5 KB
/
contentscript.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
// This script is the part of Don't Track Me Google (DTMG) that does not
// directly interact with the page's context. It may run in an ISOLATED world.
// Keep the following functions in sync with main_world_script.js:
// - getReferrerPolicy
// - getRealLinkFromGoogleUrl
var dtmgLink = document.createElement('link');
dtmgLink.id = 'dont_track_me_google_link';
document.addEventListener('mousedown', handlePointerPress, true);
document.addEventListener('touchstart', handlePointerPress, true);
document.addEventListener('click', handleClick, true);
var forceNoReferrer = true;
var noping = true;
function applyPreferences() {
dtmgLink.referrerPolicy = getReferrerPolicy();
dtmgLink.disabled = noping;
}
// Ensure that the default prefs are synchronized to the dtmgLink element:
applyPreferences();
(document.head || document.documentElement).appendChild(dtmgLink);
if (typeof chrome == 'object' && chrome.storage) {
(chrome.storage.sync || chrome.storage.local).get({
forceNoReferrer: true,
// From version 4.7 until 4.11, the preference was the literal value of
// the referrer policy.
referrerPolicy: 'no-referrer',
noping: true,
}, function(items) {
if (items) {
// Migration code (to be removed in the future).
if (items.referrerPolicy === '') {
// User explicitly allowed referrers to be sent, respect that.
items.forceNoReferrer = false;
}
forceNoReferrer = items.forceNoReferrer;
noping = items.noping;
applyPreferences();
}
potentiallyAsyncInit();
});
chrome.storage.onChanged.addListener(function(changes) {
if (changes.forceNoReferrer) {
forceNoReferrer = changes.forceNoReferrer.newValue;
}
if (changes.noping) {
noping = changes.noping.newValue;
}
applyPreferences();
});
} else {
potentiallyAsyncInit();
}
function potentiallyAsyncInit() {
if (location.hostname === 'docs.google.com') {
// Google Docs have simple non-JS interfaces where the ugly links
// are hard-coded in the HTML. Remove them (#51).
// https://docs.google.com/document/d/.../mobilebasic
// https://docs.google.com/spreadsheets/d/.../htmlview
cleanLinksWhenJsIsDisabled();
}
}
function getReferrerPolicy() {
return forceNoReferrer ? 'origin' : '';
}
function updateReferrerPolicy(a) {
if (a.referrerPolicy === 'no-referrer') {
// "no-referrer" is more privacy-friendly than "origin".
return;
}
var referrerPolicy = getReferrerPolicy();
if (referrerPolicy) {
a.referrerPolicy = referrerPolicy;
}
}
function handlePointerPress(e) {
var a = e.target;
while (a && !a.href) {
a = a.parentElement;
}
if (!a) {
return;
}
var inlineMousedown = a.getAttribute('onmousedown');
// return rwt(....); // E.g Google search results.
// return google.rwt(...); // E.g. sponsored search results
// return google.arwt(this); // E.g. sponsored search results (dec 2016).
if (inlineMousedown && /\ba?rwt\(/.test(inlineMousedown)) {
a.removeAttribute('onmousedown');
// Just in case:
a.removeAttribute('ping');
// In Chrome, removing onmousedown during event dispatch does not
// prevent the inline listener from running... So we have to cancel
// event propagation just in case.
e.stopImmediatePropagation();
}
if (noping) {
a.removeAttribute('ping');
}
var realLink = getRealLinkFromGoogleUrl(a);
if (realLink) {
a.href = realLink;
// Sometimes, two fixups are needed, on old mobile user agents:
// /url?q=https://googleweblight.com/fp?u=... -> ...
realLink = getRealLinkFromGoogleUrl(a);
if (realLink) {
a.href = realLink;
}
}
updateReferrerPolicy(a);
if (e.eventPhase === Event.CAPTURING_PHASE) {
// Our event listener runs first, to sanitize the link.
// But the page may have an event handler that modifies the link again.
// We can append a listener to the bubbling phase of the (current)
// event dispatch to fix the link up again, provided that the page did
// not call stopPropagation() or stopImmediatePropagation().
var eventOptions = { capture: false, once: true };
a.addEventListener(e.type, handlePointerPress, eventOptions);
document.addEventListener(e.type, handlePointerPress, eventOptions);
}
}
// This is specifically designed for catching clicks in Gmail.
// Gmail binds a click handler to a <div> and cancels the event after opening
// a window with an ugly URL. It uses a blank window + meta refresh in Firefox,
// which is too crazy to patch. So we just make sure that the browser's default
// click handler is activated (=open link in new tab).
// The entry point for this crazy stuff is shown in my comment at
// https://github.com/Rob--W/dont-track-me-google/issues/2
function handleClick(e) {
if (e.button !== 0) {
return;
}
var a = e.target;
while (a && !a.href) {
a = a.parentElement;
}
if (!a) {
return;
}
if (a.dataset && a.dataset.url) {
var realLink = getSanitizedIntentUrl(a.dataset.url);
if (realLink) {
a.dataset.url = realLink;
}
}
if (!location.hostname.startsWith('mail.')) {
// This hack was designed for Gmail, but broke other Google sites:
// - https://github.com/Rob--W/dont-track-me-google/issues/6
// - https://github.com/Rob--W/dont-track-me-google/issues/19
// So let's disable it for every domain except Gmail.
return;
}
// TODO: Consider using a.baseURI instead of location in case Gmail ever
// starts using <base href>?
if (a.origin === location.origin) {
// Same-origin link.
// E.g. an in-page navigation at Google Docs (#...)
// or an attachment at Gmail (https://mail.google.com/mail/u/0?ui=2&...)
return;
}
if (a.protocol !== 'http:' &&
a.protocol !== 'https:' &&
a.protocol !== 'ftp:') {
// Be conservative and don't block too much. E.g. Gmail has special
// handling for mailto:-URLs, and using stopPropagation now would
// cause mailto:-links to be opened by the platform's default mailto
// handler instead of Gmail's handler (=open in new window).
return;
}
if (a.target === '_blank') {
e.stopPropagation();
updateReferrerPolicy(a);
}
}
/**
* @param {URL|HTMLHyperlinkElementUtils} a
* @returns {String} the real URL if the given link is a Google redirect URL.
*/
function getRealLinkFromGoogleUrl(a) {
if (a.protocol !== 'https:' && a.protocol !== 'http:') {
return;
}
var url;
if ((a.hostname === location.hostname || a.hostname === 'www.google.com') &&
(a.pathname === '/url' || a.pathname === '/local_url' ||
a.pathname === '/searchurl/rr.html' ||
a.pathname === '/linkredirect')) {
// Google Maps / Dito (/local_url?q=<url>)
// Mobile (/url?q=<url>)
// Google Meet's chat (/linkredirect?authuser=0&dest=<url>)
url = /[?&](?:q|url|dest)=((?:https?|ftp)[%:][^&]+)/.exec(a.search);
if (url) {
return decodeURIComponent(url[1]);
}
// Help pages, e.g. safe browsing (/url?...&q=%2Fsupport%2Fanswer...)
url = /[?&](?:q|url)=((?:%2[Ff]|\/)[^&]+)/.exec(a.search);
if (url) {
return a.origin + decodeURIComponent(url[1]);
}
// Redirect pages for Android intents (/searchurl/rr.html#...&url=...)
// rr.html only supports http(s). So restrict to http(s) only.
url = /[#&]url=(https?[:%][^&]+)/.exec(a.hash);
if (url) {
return decodeURIComponent(url[1]);
}
}
// Google Search with old mobile UA (e.g. Firefox 41).
if (a.hostname === 'googleweblight.com' && a.pathname === '/fp') {
url = /[?&]u=((?:https?|ftp)[%:][^&]+)/.exec(a.search);
if (url) {
return decodeURIComponent(url[1]);
}
}
}
/**
* @param {string} intentUrl
* @returns {string|undefined} The sanitized intent:-URL if it was an intent URL
* with embedded tracking link.
*/
function getSanitizedIntentUrl(intentUrl) {
if (!intentUrl.startsWith('intent:')) {
return;
}
// https://developer.chrome.com/multidevice/android/intents#syntax
var BROWSER_FALLBACK_URL = ';S.browser_fallback_url=';
var indexStart = intentUrl.indexOf(BROWSER_FALLBACK_URL);
if (indexStart === -1) {
return;
}
indexStart += BROWSER_FALLBACK_URL.length;
var indexEnd = intentUrl.indexOf(';', indexStart);
indexEnd = indexEnd === -1 ? intentUrl.length : indexEnd;
var url = decodeURIComponent(intentUrl.substring(indexStart, indexEnd));
var realUrl = getRealLinkFromGoogleUrl(newURL(url));
if (!realUrl) {
return;
}
return intentUrl.substring(0, indexStart) +
encodeURIComponent(realUrl) +
intentUrl.substring(indexEnd);
}
function cleanLinksWhenJsIsDisabled() {
// When JavaScript is disabled, Google sets the "href" attribute's value to
// an ugly URL. Although the link is rewritten on click, we still need to
// rewrite the link even earlier because otherwise the ugly URL is shown in
// the tooltip upon hover.
if (document.readyState == 'complete') {
cleanAllLinks();
return;
}
// When JS is disabled, the links won't change after the document finishes
// loading. Until the DOM has finished loading, use the mouseover event to
// beautify links (the DOMContentLoaded may be delayed on slow networks).
document.addEventListener('mouseover', handleMouseOver);
document.addEventListener('DOMContentLoaded', function() {
document.removeEventListener('mouseover', handleMouseOver);
cleanAllLinks();
}, {once: true});
function cleanAllLinks() {
var as = document.querySelectorAll('a[href]');
for (var i = 0; i < as.length; ++i) {
var href = getRealLinkFromGoogleUrl(as[i]);
if (href) {
as[i].href = href;
}
}
}
function handleMouseOver(e) {
var a = e.target;
var href = a.href && getRealLinkFromGoogleUrl(a);
if (href) {
a.href = href;
}
}
}
function newURL(href) {
try {
return new URL(href);
} catch (e) {
var a = document.createElement('a');
a.href = href;
return a;
}
}