-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscriptlets.js
More file actions
345 lines (331 loc) · 11.7 KB
/
scriptlets.js
File metadata and controls
345 lines (331 loc) · 11.7 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
338
339
340
341
342
343
344
345
/*
Scriptlets - A personal collection of uBlock Origin scriptlets.
Copyright (C) 2022-present tastytypist
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
The following section is used to document custom types used to represent
various dependencies-related objects.
*/
/**
* The object returned by `safeSelf()`.
* @typedef {Object} safe
* @property {function(Object, string, Object): Object} Object_defineProperty - Protected `Object.defineProperty`.
* @property {function(string, Object, Object=)} addEventListener - Protected `addEventListener`.
* @property {function(string, function=): Object} JSON_parse - Protected `JSON.parse`.
* @property {function(Object, function=, string=): string} JSON_stringify - Protected `JSON.stringify`.
* @property {function(string, string=): RegExp} patternToRegex - Convert string into `RegExp` object.
* @property {function(Array, Number): Object.<string, string>} getExtraArgs - Get extra arguments from filters.
*/
/*
The following section is used to document the external dependencies used
as helper functions for the scriptlets in this document.
*/
/**
* Provide protected JavaScript methods for scriptlets.
* @external safeSelf
* @example
* const safe = safeSelf();
* safe.uboLog("Hello, world!");
* @returns {safe} An object with protected methods as its properties.
* @author Raymond Hill <rhill@raymondhill.net>
* @license GPL-3.0-or-later
*/
/**
* Run the specified function at the specified loading state of the document.
* @external runAt
* @example
* function foo() {
* console.log("Hello world!");
* }
* runAt(foo, "loading");
* @param {function} func - A function to be run at the specified document
* loading state.
* @param {string} when - A string representing a valid value of the
* `Document.readyState` property.
* @author Raymond Hill <rhill@raymondhill.net>
* @license GPL-3.0-or-later
*/
/**
* Get the value of the specified cookie name.
* @external getCookieFn
* @example
* let theme = getCookieFn("theme");
* setTheme(theme);
* @param {string} name - A string representing the cookie name to be fetched.
* @author Raymond Hill <rhill@raymondhill.net>
* @license GPL-3.0-or-later
*/
/*
The following section is used to implement ready-to-use scriptlets for
uBlock Origin.
*/
/**
* Redirects opened URL by replacing its hostname with the specified hostname.
* @example
* www.reddit.com##+js(rh, old.reddit.com)
* @description
* The scriptlet also accepts an optional token `exclude`, followed by a valid
* regex representing hrefs we want to exclude from redirection.
* @param {string} hostname - A valid string representation of a hostname we
* want to be redirected to.
* */
/// redirect-hostname.js
/// alias rh.js
/// world isolated
/// dependency safe-self.fn
function redirectHostname(hostname = "") {
if (hostname === "") {
return;
}
const safe = safeSelf();
const extraArgs = safe.getExtraArgs(Array.from(arguments), 1);
if (extraArgs.exclude) {
const reExclude = safe.patternToRegex(extraArgs.exclude);
if (reExclude.test(window.location)) {
return;
}
}
let targetOrigin = "https://" + hostname;
if (URL.canParse(targetOrigin) === false) {
return;
}
window.location.replace(
targetOrigin
+ window.location.pathname
+ window.location.search
+ window.location.hash
);
}
/**
* Sets the specified attribute-value pair on the specified node at the
* specified document loading state.
* @example
* github.com##+js(sa, html, data-color-mode, dark)
* @param {string} selector - A valid CSS selector of the targeted DOM node.
* @param {string} attribute - The name of the attribute being set.
* @param {string} [value] - The value of the attribute being set.
* @param {string} [when] - A valid value of the `Document.readyState` property.
*/
/// set-attribute.js
/// alias sa.js
/// world isolated
/// dependency run-at.fn
function setAttribute(selector = "", attribute = "", value = "", when = "complete") {
if (selector === "" || attribute === "") {
return;
}
function setAttr() {
const nodes = document.querySelectorAll(selector);
try {
nodes.forEach((node) => {
node.setAttribute(attribute, value);
});
} catch (error) {
console.error(error);
}
}
function debounce(func, delay) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
const callback = (_, observer) => {
observer.disconnect();
setAttr();
observer.observe(document.documentElement, {
subtree: true,
childList: true,
attributeFilter: [attribute]
});
};
const debouncedCallback = debounce(callback, 20);
const observer = new MutationObserver(debouncedCallback);
runAt(() => {
setAttr();
observer.observe(document.documentElement, {
subtree: true,
childList: true,
attributeFilter: [attribute]
});
}, when);
}
/**
* Prevents and spoofs the response of a fetch call mathing the specified options.
* @example
* theathletic.com##+js(sf, api.theathletic.com/graphql body:PostEvent, '{"data":{"postEvent":true}}')
* @param {string} optionsToMatch - A string of space-separated fetch options
* to be matched.
* @param {string} responseString - A string used to spoof the response call.
*/
/// spoof-fetch.js
/// alias sf.js
/// dependency safe-self.fn
function spoofFetch(optionsToMatch = "", responseString = "") {
if (optionsToMatch === "" || responseString === "") {
return;
}
const safe = safeSelf();
const needles = [];
const conditions = optionsToMatch.split(" ");
conditions.forEach((condition) => {
const setting = condition.split(":", 2);
let key, needle;
if (setting.length === 2) {
key = setting[0];
needle = safe.patternToRegex(setting[1]);
} else {
key = "url";
needle = safe.patternToRegex(setting[0]);
}
needles.push({ key, needle });
});
self.fetch = new Proxy(self.fetch, {
apply(target, thisArg, args) {
let options;
if (args[0] instanceof Request) {
options = args[0];
} else {
options = Object.assign({ url: args[0] }, args[1]);
}
try {
const settings = new Map();
for (const option in options) {
let value = options[option];
if (typeof value !== "string") {
try {
value = safe.JSON_stringify(value);
} catch (error) {
console.error(error);
continue;
}
}
settings.set(option, value);
}
for (const { key, needle } of needles) {
if (settings.has(key) === false || needle.test(settings.get(key)) === false) {
return Reflect.apply(target, thisArg, args);
}
}
} catch (error) {
console.error(error);
}
let responseType = "";
if (options.mode === undefined || options.mode === "cors") {
try {
const destinationURL = new URL(options.url);
if (destinationURL.origin !== document.location.origin) {
responseType = "cors";
} else {
responseType = "basic";
}
} catch (error) {
console.error(error);
}
}
return Promise.resolve(responseString).then((responseBody) => {
const spoofedResponse = new Response(responseBody, {
statusText: "OK",
headers: {
"Content-Length": responseBody.length.toString()
}
});
safe.Object_defineProperty(spoofedResponse, "url", {
value: options.url
});
if (responseType !== "") {
safe.Object_defineProperty(spoofedResponse, "type", {
value: responseType
});
}
return spoofedResponse;
});
}
});
}
/**
* Automatically claims bonus channel points on Twitch.
* @example
* twitch.tv##+js(twitch-claim-bonus)
*/
/// twitch-claim-bonus.js
/// dependency safe-self.fn
/// dependency get-cookie.fn
function twitchClaimBonus() {
if (/^\/videos\//.test(document.location.pathname)) {
return;
}
if (getCookieFn("login") === undefined) {
return;
}
const safe = safeSelf();
try {
safe.Object_defineProperty(document, "visibilityState", {
value: "visible"
});
safe.Object_defineProperty(document, "hidden", {
value: false
});
const args = ["visibilitychange", (e) => e.stopImmediatePropagation(), true];
safe.addEventListener.apply(document, args);
} catch (error) {
console.error(error);
}
console.info("Checking for button container...");
function leadingDebounce(func, delay) {
let timer;
return (...args) => {
if (timer === undefined) {
func.apply(this, args);
}
clearTimeout(timer);
timer = setTimeout(() => {
timer = undefined;
}, delay);
};
}
const logAttempt = (success) => {
if (success) {
console.info("Bonus point claim succeed!");
} else {
console.info("Bonus point button isn't found!");
}
};
function checkButton(element) {
let debouncedLog = leadingDebounce(logAttempt, 10000);
const callback = () => {
try {
document.getElementsByClassName("claimable-bonus__icon")[0].click();
debouncedLog = leadingDebounce(logAttempt, 10000);
debouncedLog(true);
} catch (error) {
debouncedLog(false);
}
};
const observer = new MutationObserver(callback);
observer.observe(element, { subtree: true, childList: true });
}
const callback = (_, observer) => {
const elements = document.getElementsByClassName("chat-input__buttons-container");
if (elements.length > 0) {
console.info(`Button container found! Initiating auto-claim...`);
observer.disconnect();
checkButton(elements[0]);
}
};
const observer = new MutationObserver(callback);
observer.observe(document, { subtree: true, childList: true });
}