-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocker.js
More file actions
executable file
·304 lines (239 loc) · 12.5 KB
/
Copy pathblocker.js
File metadata and controls
executable file
·304 lines (239 loc) · 12.5 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
/*
Copyright (C) 2026 IsHacker
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Some non-working attempts to block tracking through iframes (e.g Youtube, ogs.google.com)
/*const csp_tag = 'default-src \'unsafe-inline\' * data:; script-src \'unsafe-inline\' https://*.ytimg.com https://*.google.com https://*.googleusercontent.com https://*.googleapis.com https://*.youtube.com https://*.youtube-nocookie.com https://*.googlevideo.com https://*.gstatic.com; connect-src \'unsafe-inline\' https://*.ytimg.com https://img.youtube.com https://*.googlevideo.com https://www.google.com https://*.gstatic.com https://*.googleusercontent.com;';
console.log("Using CSP: "+csp_tag);
function addCsptoIframes() {
var a_iframes = document.querySelectorAll("iframe");
for (var i = 0; i < a_iframes.length; i++) {a_iframes[i].csp = csp_tag;}
}
const iframe_observer = new MutationObserver(addCsptoIframes);
iframe_observer.observe(document, { childList: true, subtree: true });
*/
/*function sandboxIframes() {
var a_iframes = document.querySelectorAll("iframe");
for (var i = 0; i < a_iframes.length; i++) {a_iframes[i].sandbox = "allow-same-origin";}
}
const iframe_observer = new MutationObserver(sandboxIframes);
iframe_observer.observe(document, { childList: true, subtree: true });
*/
// Intercept and block tracking urls by hooking into XHR and fetch methods
// XHR
(function() {
let _open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
_open.call(this, method, url, async=async, user=user, password=password)
}
let _send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data) {
let _onload = this.onprogress;
this.onload = function() {
console.log("Allowed: "+this.responseURL);
if (_onload != null) {
_onload.call(this)
}
}
// Since we can't know the request URL for xhr.send(), we distinguish tracking requests by their request body
// If it starts with "[" or "[[", it is a tracking request
// Legit POST requests won't start with those brackets, e.g POST requests for image uploads for Google Lens Search will start with binary data
if (data != null && data.startsWith("[")) {
console.log("Blocked tracking request!");
}
else {
_send.call(this, data);
}
}
})();
// fetch
const { fetch: originalFetch } = window;
window.fetch = async (...args) => {
let [resource, config ] = args;
if (resource.includes('/log?') || resource.includes('_204')) {
console.log("Blocked: "+resource);
resource = "";
return true;
}
else {
console.log("Allowed: "+resource);
}
const response = await originalFetch(resource, config);
return response;
}
// Block gen_204 beacons
function blockTrackingBeacons() {
console.log("Blocked tracking beacon!");
return true;
}
Object.defineProperty(window.Navigator.prototype, 'sendBeacon', {
get: function() {
return blockTrackingBeacons;
},
set: function(ignored) { }
});
if (defglog == 1) {
Object.defineProperty(window.google, 'log', {
get: function() {
return blockTrackingBeacons;
},
set: function(ignored) { }
});
Object.defineProperty(window.google, 'logUrl', {
get: function() {
return blockTrackingBeacons;
},
set: function(ignored) { }
});
Object.defineProperty(window.google, 'ml', {
get: function() {
return blockTrackingBeacons;
},
set: function(ignored) { }
});
}
// Alternative implementation taken from "Don't track me google" userscript
/*(function() {
var navProto = window.Navigator.prototype;
var navProtoSendBeacon = navProto.sendBeacon;
if (!navProtoSendBeacon) {
return;
}
var sendBeacon = Function.prototype.apply.bind(navProtoSendBeacon);
navProto.sendBeacon = function(url, data) {
console.log("Blocked tracking beacon!");
return true;
};
})();
*/
// AI overviews
// Sometimes the AI overview is part of the received html document, and isn't generated by JS. This makes it impossible to "block" them, so we just hide them
// Taken from https://github.com/zbarnz/Google_AI_Overviews_Blocker
// AI Overview text patterns for different languages
const AI_OVERVIEW_PATTERNS = [
/übersicht mit ki/i, // de
/ai overview/i, // en
/prezentare generală generată de ai/i, // ro
/AI による概要/, // ja
/Обзор от ИИ/, // ru
/AI 摘要/, // zh-TW
/AI-overzicht/i, // nl
/Vista creada con IA/i, // es
/Přehled od AI/i, // cz
];
// CSS selectors for AI overview containers
const AI_OVERVIEW_SELECTORS = {
SEARCH_RESULT_SELECTOR: "div#rso > div > div",
ABOVE_SEARCH_RESULTS_SELECTOR: "div#rcnt > div",
};
// Main DOM selectors
const DOM_SELECTORS = {
MAIN_BODY: "div#rcnt",
HEADER_TABS: "div#hdtb-sc > div",
MAIN_ELEMENT: '[role="main"]',
PEOPLE_ALSO_ASK: "div.related-question-pair",
TABS_LIST: '[role="list"]',
};
// CSS values
const CSS_VALUES = {
HIDDEN: "none",
HEADER_PADDING: "12px",
MAIN_MARGIN: "24px",
};
// Tab patterns
const TAB_PATTERNS = {
AI_MODE: /^AI Mode$/i,
};
// Hide AI overview
(function () {
'use strict';
const hideAIO = () => {
const aiBlocks = document.querySelectorAll('div[jscontroller="EYwa3d"]');
aiBlocks.forEach(el => {
el.style.display = 'none';
el.style.visibility = 'hidden';
});
const aiButton = document.querySelectorAll('button[jscontroller="jNZDL"]');
aiButton.forEach(elb => {
elb.style.display = 'none';
elb.style.visibility = 'hidden';
});
const extraEl = document.querySelector('#Odp5De > div:nth-child(1) > div');
if (extraEl) {
extraEl.style.display = 'none';
extraEl.style.visibility = 'hidden';
}
const extraEl2 = document.querySelector('#m-x-content > div');
if (extraEl2) {
extraEl2.style.display = 'none';
extraEl2.style.visibility = 'hidden';
}
};
hideAIO();
const observer = new MutationObserver(hideAIO);
observer.observe(document, { childList: true, subtree: true });
})();
const getAiOverview = (mainBody) => {
// Find all headings that match AI overview patterns
const aiTexts = [...mainBody?.querySelectorAll("h1, h2")].filter((e) =>
AI_OVERVIEW_PATTERNS.some((pattern) => pattern.test(e.innerText))
);
// For each matching heading, check both possible div containers
for (const aiText of aiTexts) {
// Check AI overview as a search result
const aiOverviewAsResult = aiText?.closest(AI_OVERVIEW_SELECTORS.SEARCH_RESULT_SELECTOR);
if (aiOverviewAsResult) return aiOverviewAsResult;
// Check AI overview above search results
const aiOverviewAbove = aiText?.closest(AI_OVERVIEW_SELECTORS.ABOVE_SEARCH_RESULTS_SELECTOR);
if (aiOverviewAbove) return aiOverviewAbove;
}
return null;
};
const n_observer = new MutationObserver(() => {
// each time there's a mutation in the document see if there's an ai overview to hide
const mainBody = document.querySelector(DOM_SELECTORS.MAIN_BODY);
// Commented out because it's not working
// const aiOverview = getAiOverview(mainBody);
/* // Hide AI overview
if (aiOverview) aiOverview.style.display = CSS_VALUES.HIDDEN;
*/
// Restore padding after header tabs
const headerTabs = document.querySelector(DOM_SELECTORS.HEADER_TABS);
if (headerTabs) headerTabs.style.paddingBottom = CSS_VALUES.HEADER_PADDING;
// For debugging
// console.log([...mainBody?.querySelectorAll('h1, h2')].map(e => { return { text: e.innerText, obj: e }}));
const mainElement = document.querySelector(DOM_SELECTORS.MAIN_ELEMENT);
if (mainElement) {
mainElement.style.marginTop = CSS_VALUES.MAIN_MARGIN;
}
// Remove entries in "People also ask" section if it contains "AI overview"
const peopleAlsoAskAiOverviews = [
...document.querySelectorAll(DOM_SELECTORS.PEOPLE_ALSO_ASK),
].filter((el) => AI_OVERVIEW_PATTERNS.some((pattern) => pattern.test(el.innerHTML)));
peopleAlsoAskAiOverviews.forEach((el) => {
el.parentElement.parentElement.style.display = CSS_VALUES.HIDDEN;
});
// Hide AI Mode tab
const tabsList = document.querySelector(DOM_SELECTORS.TABS_LIST).children;
const aiModeTab = tabsList[0];
const text = aiModeTab.innerText.trim();
if (TAB_PATTERNS.AI_MODE.test(text)) {
aiModeTab.style.display = CSS_VALUES.HIDDEN;
}
});
n_observer.observe(document, {
childList: true,
subtree: true,
});
// Disable AMP
// Taken from AdGuard Disable AMP userscript
!function(){"use strict";var t=/(amp\/|amp-|\.amp)/,e=["#amp-mobile-version-switcher > a",'head > link[rel="canonical"]'],r=function(t){new MutationObserver(t).observe(document,{childList:!0,subtree:!0})},n=function(){var t=e.reduce((function(t,e){return t||document.querySelector(e)}),null);if(!t)return null;if(!function(t){try{return new URL(t),!0}catch(t){return!1}}(t.href))return null;var r=new URL(t.href);return r.searchParams.has("amp")&&r.searchParams.delete("amp"),r.href},a=function(t){var e=t.querySelector('[aria-label="AMP logo"], [aria-label="Logo AMP"]');e&&(e.style.display="none")};function o(t){return function(t){if(Array.isArray(t))return i(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return i(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}var c,u,l,s="https://",f=["ping","data-ved","data-amp-cur","data-amp-title","data-amp","data-amp-vgi","jsaction"],d="google.";u=(c=document.location).href,l=c.origin,u.includes("https://yandex.ru/turbo")||u.includes("turbopages.org")?function(){var t=n();if(t)document.location.href=t;else{var e=document.querySelector('script[data-name="post-message"][data-message]');if(e&&t){var r=e.getAttribute("data-message"),a=JSON.parse(r);a&&a.originalUrl&&(document.location.href=a.originalUrl)}}}():(l.includes(d)&&u.includes("/search")&&r((function(){var t=document.querySelectorAll("a[data-ved]:has(> div[class] > span + svg)"),e=[];t.forEach((function(t){var r=t.previousElementSibling;r&&r.hasAttribute("data-ved")&&e.push(r)}));var r=document.querySelectorAll("a[data-amp]"),n=document.querySelectorAll("a[data-amp-vgi]");[].concat(o(r),o(n),o(t),e).forEach((function(t){f.forEach((function(e){t.removeAttribute(e)}))}))})),l.includes(d)||function(){if(document.querySelector('script[src^="https://cdn.ampproject.org/"]')){var t=n();t&&(document.location.href=t)}}(),l.includes("news.google.")?r((function(){window.self===window.top&&document.querySelectorAll("article > a[jslog]").forEach((function(e){var r=function(e){var r,n=e.substring(e.indexOf(":")+1,e.indexOf("; track:click,vis"));try{r=JSON.parse(atob(n))}catch(t){}if(!r)return null;var a=r.filter((function(t){return"string"==typeof t&&(t.startsWith("http")||t.startsWith("https"))}));return a.length<2?null:a.find((function(e){return!t.test(e)}))||null}(e.getAttribute("jslog"));r&&function(t,e){t.setAttribute("href",e),t.addEventListener("click",(function(t){t.preventDefault(),t.stopPropagation(),document.location.href=e}),!0),a(t)}(e,r)}))})):r((function(){document.querySelectorAll("a[data-amp-cdn]").forEach((function(t){var e=t.href;e.includes("cdn.ampproject.org")&&(e=s+e.substr(e.indexOf("cdn.ampproject.org/wp/s/")+24)),e.substr(8).startsWith("amp.")&&(e=s+e.substr(12)),(e=e.replace("?amp&","?&"))!==t.href&&(t.setAttribute("href",e),t.addEventListener("click",(function(t){t.preventDefault(),t.stopPropagation(),document.location.href=e}),!0),a(t))}))})))}();