Skip to content

Commit e1cb4a4

Browse files
committed
build: make tinyShield exportable
1 parent c4eb897 commit e1cb4a4

File tree

4 files changed

+103
-86
lines changed

4 files changed

+103
-86
lines changed

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
{
22
"name": "@filteringdev/tinyshield",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"description": "",
55
"type": "module",
66
"scripts": {
7-
"build": "esbuild sources/src/index.ts --bundle --minify-whitespace --minify-syntax --define:global=window --inject:./sources/esbuild.inject.ts --banner:js=\"$(cat ./sources/banner.txt)\" --target=es2024,chrome119,safari17,firefox121 --outfile=./dist/tinyShield.user.js",
7+
"build:interface": "esbuild sources/interface.ts --bundle --format=esm --splitting --sourcemap --target=es2024 --external:/node_modules --outdir=dist && tsc sources/interface.ts --outDir dist/types --declaration --emitDeclarationOnly",
8+
"build:userscript": "esbuild sources/src/index.ts --bundle --minify-whitespace --minify-syntax --define:global=window --inject:./sources/esbuild.inject.ts --banner:js=\"$(cat ./sources/banner.txt)\" --target=es2024,chrome119,safari17,firefox121 --outfile=./dist/tinyShield.user.js",
9+
"build": "npm run build:interface && npm run build:userscript",
810
"debug": "esbuild sources/src/index.ts --bundle --define:global=window --inject:./sources/esbuild.inject.ts --banner:js=\"$(cat ./sources/banner.txt)\" --target=es2024,chrome119,safari17,firefox121 --outfile=./dist/tinyShield-debug.user.js",
911
"lint": "tsc --noEmit && eslint sources/**/*.ts"
1012
},
11-
"keywords": [],
13+
"keywords": [
14+
"Ad-Shield"
15+
],
1216
"files": [
13-
"dist"
17+
"dist/**/*"
1418
],
19+
"exports": {
20+
".": {
21+
"import": "./dist/interface.js",
22+
"types": "./dist/types/interface.d.ts"
23+
}
24+
},
1525
"repository": {
1626
"type": "git",
1727
"url": "git+https://github.com/FilteringDev/tinyShield.git"

sources/banner.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// @downloadURL https://cdn.jsdelivr.net/npm/@filteringdev/tinyshield@latest/dist/tinyShield.user.js
99
// @license MIT
1010
//
11-
// @version 2.0.6
11+
// @version 2.0.7
1212
// @author PiQuark6046 and contributors
1313
//
1414
// @match *://ygosu.com/*

sources/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { RunTinyShieldUserscript } from './src/index.js'

sources/src/index.ts

Lines changed: 87 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,99 +6,105 @@ const Win = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window
66

77
import { CheckDepthInASWeakMap } from './as-weakmap.js'
88

9-
const OriginalArrayToString = Win.Array.prototype.toString
109
export const OriginalRegExpTest = Win.RegExp.prototype.test
1110

12-
const ProtectedFunctionStrings = ['toString', 'get', 'set']
11+
export function RunTinyShieldUserscript(BrowserWindow: typeof window, UserscriptName: string = 'tinyShield'): void {
12+
const OriginalArrayToString = BrowserWindow.Array.prototype.toString
13+
const OriginalRegExpTest = BrowserWindow.RegExp.prototype.test
1314

14-
Win.Function.prototype.toString = new Proxy(Win.Function.prototype.toString, {
15-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
16-
apply(Target: () => string, ThisArg: Function, Args: null) {
17-
if (ProtectedFunctionStrings.includes(ThisArg.name)) {
18-
return `function ${ThisArg.name}() { [native code] }`
19-
} else {
20-
return Reflect.apply(Target, ThisArg, Args)
21-
}
22-
}
23-
})
15+
const ProtectedFunctionStrings = ['toString', 'get', 'set']
2416

25-
const ASInitPositiveRegExps: RegExp[][] = [[
26-
/[a-zA-Z0-9]+ *=> *{ *const *[a-zA-Z0-9]+ *= *[a-zA-Z0-9]+ *; *if/,
27-
/===? *[a-zA-Z0-9]+ *\[ *[a-zA-Z0-9]+\( *[0-9a-z]+ *\) *\] *\) *return *[a-zA-Z0-9]+ *\( *{ *('|")?inventoryId('|")? *:/,
28-
/{ *('|")?inventoryId('|")? *: *this *\[[a-zA-Z0-9]+ *\( *[0-9a-z]+ *\) *\] *, *\.\.\. *[a-zA-Z0-9]+ *\[ *[a-zA-Z0-9]+ *\( *[0-9a-z]+ * *\) *\] *} *\)/
29-
]]
30-
Win.Map.prototype.get = new Proxy(Win.Map.prototype.get, {
31-
apply(Target: (key: unknown) => unknown, ThisArg: Map<unknown, unknown>, Args: [unknown]) {
32-
if (Args.length > 0 && typeof Args[0] !== 'function') {
33-
return Reflect.apply(Target, ThisArg, Args)
17+
BrowserWindow.Function.prototype.toString = new Proxy(BrowserWindow.Function.prototype.toString, {
18+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
19+
apply(Target: () => string, ThisArg: Function, Args: null) {
20+
if (ProtectedFunctionStrings.includes(ThisArg.name)) {
21+
return `function ${ThisArg.name}() { [native code] }`
22+
} else {
23+
return Reflect.apply(Target, ThisArg, Args)
24+
}
3425
}
26+
})
3527

36-
let ArgText = OriginalArrayToString.call(Args) as string
37-
if (ASInitPositiveRegExps.filter(ASInitPositiveRegExp => ASInitPositiveRegExp.filter(Index => OriginalRegExpTest.call(Index, ArgText) as boolean).length >= 2).length === 1) {
38-
console.debug('[tinyShield]: Map.prototype.get:', ThisArg, Args)
39-
throw new Error()
40-
}
28+
const ASInitPositiveRegExps: RegExp[][] = [[
29+
/[a-zA-Z0-9]+ *=> *{ *const *[a-zA-Z0-9]+ *= *[a-zA-Z0-9]+ *; *if/,
30+
/===? *[a-zA-Z0-9]+ *\[ *[a-zA-Z0-9]+\( *[0-9a-z]+ *\) *\] *\) *return *[a-zA-Z0-9]+ *\( *{ *('|")?inventoryId('|")? *:/,
31+
/{ *('|")?inventoryId('|")? *: *this *\[[a-zA-Z0-9]+ *\( *[0-9a-z]+ *\) *\] *, *\.\.\. *[a-zA-Z0-9]+ *\[ *[a-zA-Z0-9]+ *\( *[0-9a-z]+ * *\) *\] *} *\)/
32+
]]
33+
BrowserWindow.Map.prototype.get = new Proxy(BrowserWindow.Map.prototype.get, {
34+
apply(Target: (key: unknown) => unknown, ThisArg: Map<unknown, unknown>, Args: [unknown]) {
35+
if (Args.length > 0 && typeof Args[0] !== 'function') {
36+
return Reflect.apply(Target, ThisArg, Args)
37+
}
4138

42-
return Reflect.apply(Target, ThisArg, Args)
43-
}
44-
})
39+
let ArgText = OriginalArrayToString.call(Args) as string
40+
if (ASInitPositiveRegExps.filter(ASInitPositiveRegExp => ASInitPositiveRegExp.filter(Index => OriginalRegExpTest.call(Index, ArgText) as boolean).length >= 2).length === 1) {
41+
console.debug(`[${UserscriptName}]: Map.prototype.get:`, ThisArg, Args)
42+
throw new Error()
43+
}
4544

46-
const ASReinsertedAdvInvenPositiveRegExps: RegExp[][] = [[
47-
/inventory_id,[a-zA-Z0-9-]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+/,
48-
/inventory_id,[a-zA-Z0-9-]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+/,
49-
/inventory_id,[a-zA-Z0-9-]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+/
50-
], [
51-
/[a-z0-9A-Z]+\.setAttribute\( *('|")onload('|") *, *('|")! *async *function\( *\) *\{ *let */,
52-
/confirm\( *[A-Za-z0-9]+ *\) *\) *{ *const *[A-Za-z0-9]+ *= *new *[A-Za-z0-9]+\.URL\(('|")https:\/\/report\.error-report\.com\//,
53-
/\.forEach *\( *\( *[A-Za-z0-9]+ *=> *[A-Za-z0-9]+\.remove *\( *\) *\) *\) *\) *, *[0-9a-f]+ *\) *; *const *[A-Za-z0-9]+ *= *await *\( *await *fetch *\(/
54-
]]
55-
Win.Map.prototype.set = new Proxy(Win.Map.prototype.set, {
56-
apply(Target: (key: unknown, value: unknown) => Map<unknown, unknown>, ThisArg: Map<unknown, unknown>, Args: [unknown, unknown]) {
57-
let ArgText = ''
58-
try {
59-
ArgText = OriginalArrayToString.call(Args) as string
60-
} catch {
61-
console.warn('[tinyShield]: Map.prototype.set:', ThisArg, Args)
62-
}
63-
if (ASReinsertedAdvInvenPositiveRegExps.filter(ASReinsertedAdvInvenPositiveRegExp => ASReinsertedAdvInvenPositiveRegExp.filter(Index => OriginalRegExpTest.call(Index, ArgText) as boolean).length >= 3).length === 1) {
64-
console.debug('[tinyShield]: Map.prototype.set:', ThisArg, Args)
65-
throw new Error()
45+
return Reflect.apply(Target, ThisArg, Args)
6646
}
67-
return Reflect.apply(Target, ThisArg, Args)
68-
}
69-
})
47+
})
7048

71-
Win.WeakMap.prototype.set = new Proxy(Win.WeakMap.prototype.set, {
72-
apply(Target: (key: object, value: unknown) => WeakMap<object, unknown>, ThisArg: WeakMap<object, unknown>, Args: [object, unknown]) {
73-
if (CheckDepthInASWeakMap(Args)) {
74-
console.debug('[tinyShield]: WeakMap.prototype.set:', ThisArg, Args)
75-
throw new Error()
49+
const ASReinsertedAdvInvenPositiveRegExps: RegExp[][] = [[
50+
/inventory_id,[a-zA-Z0-9-]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+/,
51+
/inventory_id,[a-zA-Z0-9-]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+/,
52+
/inventory_id,[a-zA-Z0-9-]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+/
53+
], [
54+
/[a-z0-9A-Z]+\.setAttribute\( *('|")onload('|") *, *('|")! *async *function\( *\) *\{ *let */,
55+
/confirm\( *[A-Za-z0-9]+ *\) *\) *{ *const *[A-Za-z0-9]+ *= *new *[A-Za-z0-9]+\.URL\(('|")https:\/\/report\.error-report\.com\//,
56+
/\.forEach *\( *\( *[A-Za-z0-9]+ *=> *[A-Za-z0-9]+\.remove *\( *\) *\) *\) *\) *, *[0-9a-f]+ *\) *; *const *[A-Za-z0-9]+ *= *await *\( *await *fetch *\(/
57+
]]
58+
BrowserWindow.Map.prototype.set = new Proxy(BrowserWindow.Map.prototype.set, {
59+
apply(Target: (key: unknown, value: unknown) => Map<unknown, unknown>, ThisArg: Map<unknown, unknown>, Args: [unknown, unknown]) {
60+
let ArgText = ''
61+
try {
62+
ArgText = OriginalArrayToString.call(Args) as string
63+
} catch {
64+
console.warn(`[${UserscriptName}]: Map.prototype.set:`, ThisArg, Args)
65+
}
66+
if (ASReinsertedAdvInvenPositiveRegExps.filter(ASReinsertedAdvInvenPositiveRegExp => ASReinsertedAdvInvenPositiveRegExp.filter(Index => OriginalRegExpTest.call(Index, ArgText) as boolean).length >= 3).length === 1) {
67+
console.debug(`[${UserscriptName}]: Map.prototype.set:`, ThisArg, Args)
68+
throw new Error()
69+
}
70+
return Reflect.apply(Target, ThisArg, Args)
7671
}
72+
})
7773

78-
return Reflect.apply(Target, ThisArg, Args)
79-
}
80-
})
74+
BrowserWindow.WeakMap.prototype.set = new Proxy(BrowserWindow.WeakMap.prototype.set, {
75+
apply(Target: (key: object, value: unknown) => WeakMap<object, unknown>, ThisArg: WeakMap<object, unknown>, Args: [object, unknown]) {
76+
if (CheckDepthInASWeakMap(Args)) {
77+
console.debug(`[${UserscriptName}]: WeakMap.prototype.set:`, ThisArg, Args)
78+
throw new Error()
79+
}
8180

82-
let ASTimerRegExps: RegExp[][] = [[
83-
/async *\( *\) *=> *{ *const *[A-Za-z0-9]+ *= *[A-Za-z0-9]+ *; *await *[A-Za-z0-9]+ *\( *\)/,
84-
/; *await *[A-Za-z0-9]+ *\( *\) *, *[A-Za-z0-9]+ *\( *! *1 *, *new *Error *\( *[A-Za-z0-9]+ *\( *[0-9a-f]+ *\) *\) *\) *}/,
85-
/ *\) *\) *\) *}/
86-
]]
87-
Win.setTimeout = new Proxy(Win.setTimeout, {
88-
apply(Target: typeof Win.setTimeout, ThisArg: undefined, Args: Parameters<typeof setTimeout>) {
89-
if (ASTimerRegExps.filter(ASTimerRegExp => ASTimerRegExp.filter(Index => Index.test(Args[0].toString())).length >= 3).length === 1) {
90-
console.debug('[tinyShield]: setTimeout:', Args)
91-
return
81+
return Reflect.apply(Target, ThisArg, Args)
82+
}
83+
})
84+
85+
let ASTimerRegExps: RegExp[][] = [[
86+
/async *\( *\) *=> *{ *const *[A-Za-z0-9]+ *= *[A-Za-z0-9]+ *; *await *[A-Za-z0-9]+ *\( *\)/,
87+
/; *await *[A-Za-z0-9]+ *\( *\) *, *[A-Za-z0-9]+ *\( *! *1 *, *new *Error *\( *[A-Za-z0-9]+ *\( *[0-9a-f]+ *\) *\) *\) *}/,
88+
/ *\) *\) *\) *}/
89+
]]
90+
BrowserWindow.setTimeout = new Proxy(BrowserWindow.setTimeout, {
91+
apply(Target: typeof BrowserWindow.setTimeout, ThisArg: undefined, Args: Parameters<typeof setTimeout>) {
92+
if (ASTimerRegExps.filter(ASTimerRegExp => ASTimerRegExp.filter(Index => Index.test(Args[0].toString())).length >= 3).length === 1) {
93+
console.debug(`[${UserscriptName}]: setTimeout:`, Args)
94+
return
95+
}
96+
return Reflect.apply(Target, ThisArg, Args)
9297
}
93-
return Reflect.apply(Target, ThisArg, Args)
94-
}
95-
})
96-
Win.setInterval = new Proxy(Win.setInterval, {
97-
apply(Target: typeof Win.setInterval, ThisArg: undefined, Args: Parameters<typeof setInterval>) {
98-
if (ASTimerRegExps.filter(ASTimerRegExp => ASTimerRegExp.filter(Index => Index.test(Args[0].toString())).length >= 3).length === 1) {
99-
console.debug('[tinyShield]: setInterval:', Args)
100-
return
98+
})
99+
BrowserWindow.setInterval = new Proxy(BrowserWindow.setInterval, {
100+
apply(Target: typeof BrowserWindow.setInterval, ThisArg: undefined, Args: Parameters<typeof setInterval>) {
101+
if (ASTimerRegExps.filter(ASTimerRegExp => ASTimerRegExp.filter(Index => Index.test(Args[0].toString())).length >= 3).length === 1) {
102+
console.debug(`[${UserscriptName}]: setInterval:`, Args)
103+
return
104+
}
105+
return Reflect.apply(Target, ThisArg, Args)
101106
}
102-
return Reflect.apply(Target, ThisArg, Args)
103-
}
104-
})
107+
})
108+
}
109+
110+
RunTinyShieldUserscript(Win)

0 commit comments

Comments
 (0)