Skip to content

Commit ccb9aab

Browse files
committed
fix: enhance Promise.then handling to detect and hide specific elements in PowerLink
1 parent 6837980 commit ccb9aab

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

userscript/source/index.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,35 @@ declare const __OCR_WORKER_CODE__: string
2424
export async function RunNamuLinkUserscript(BrowserWindow: typeof window, UserscriptName: string = 'NamuLink'): Promise<void> {
2525
const OriginalReflectApply = BrowserWindow.Reflect.apply
2626

27-
let PL2AfterLoadInitTimerPatterns: RegExp[][] = [[
28-
/\( *\) *=> *{ *var *_0x[0-9a-z]+ *= *a0_0x[0-9a-f]+ *; *this\[ *_0x[a-z0-9]+\( *0x[0-9a-f]+ *\) *\]\(\); *}/,
29-
/\( *\) *=> *{ *var *_0x[0-9a-z]+ *= *a0_0x[0-9a-f]+ *; *this\[ *_0x[a-z0-9]+\( *0x[0-9a-f]+ *\) *\]\(\); *}/
30-
], [
31-
/\( *\) *=> *{ *var _0x[a-z0-9]+ *= *_0x[a-z0-9]+ *; *if *\( *this\[ *_0x[a-z0-9]+ *\( *0x[0-9a-f]+ *\) *\] *\) *return *clearTimeout/,
32-
/\( *0x[0-9a-f]+ *\) *\] *\) *, *void *\( *this\[ *_0x[a-z0-9]+\( *0x[0-9a-f]+ *\) *\] *= *void *\([x0-9a-f*+-]+ *\) *\) *; *this\[_0x[a-z0-9]+\( *0x[0-9a-f]+ *\) *\] *\(\) *;/
27+
const PL2PromiseThenRegexs: RegExp[][] = [[
28+
/function *[A-Za-z0-9]+ *\([A-Za-z0-9]+ * *\) *{ *function *[A-Za-z0-9]+ *\( *[a-zA-Z]+ *, *[A-Za-z]+ *\) *{ *return *[A-Za-z0-9]+ *\( */,
29+
/{ *return *[A-Za-z0-9]+ *\( *[a-zA-Z]+ *- *0x[a-f0-9]+ *, *[a-zA-Z]+ *\) *; *\} *[A-Za-z0-9]+ *\( *[A-Za-z0-9]+ *, *[A-Za-z0-9]+ *, *[A-Za-z0-9]+/,
30+
/\( *[A-Za-z0-9]+ *, *[A-Za-z0-9]+ *, *[A-Za-z0-9]+ *, *[A-Za-z0-9]+ *, *[A-Za-z0-9]+ *, *[A-Za-z0-9]+ *\( *0x[a-f0-9]+ *, *0x[a-f0-9]+ *\) *, *[A-Za-z0-9]+ *\) *;/
3331
]]
3432

35-
BrowserWindow.setTimeout = new Proxy(BrowserWindow.setTimeout, {
36-
apply(Target: typeof setTimeout, ThisArg: undefined, Args: Parameters<typeof setTimeout>) {
37-
let StringifiedFunc = String(Args[0])
38-
if (PL2AfterLoadInitTimerPatterns.filter(PowerLinkGenerationSkeletionPositiveRegExp => PowerLinkGenerationSkeletionPositiveRegExp.filter(Index => Index.test(StringifiedFunc)).length >= 1).length === 1) {
39-
console.debug(`[${UserscriptName}]: setTimeout called for PowerLink Skeleton:`, Args[0])
40-
return OriginalReflectApply(Target, ThisArg, [() => {}, 0])
33+
BrowserWindow.Promise.prototype.then = new Proxy(BrowserWindow.Promise.prototype.then, {
34+
apply(Target: typeof Promise.prototype.then, ThisArg: Promise<unknown>, Args: Parameters<typeof Promise.prototype.then>) {
35+
if (typeof Args[0] !== 'function' || typeof Args[1] !== 'function') {
36+
return OriginalReflectApply(Target, ThisArg, Args)
37+
}
38+
const Stringified: [string, string] = [String(Args[0]), String(Args[1])]
39+
if (Stringified.every(Str => PL2PromiseThenRegexs.filter(Regexs => Regexs.filter(Regex => Regex.test(Str)).length === Regexs.length).length === 1)) {
40+
console.debug(`[${UserscriptName}] Detected PL2 Promise.then`, Stringified, Args)
41+
setTimeout(() => {
42+
let Targeted = [...document.querySelectorAll('#app div[class] div[class] ~ div[class]')].filter(Ele => Ele instanceof HTMLElement)
43+
Targeted = Targeted.filter(Ele => parseFloat(getComputedStyle(Ele).getPropertyValue('margin-bottom')) >= 12.5)
44+
Targeted = Targeted.filter(Ele => Ele.innerText.trim().length === 0)
45+
Targeted = Targeted.filter(Ele => [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement).some(Child => {
46+
const Height = Child.getBoundingClientRect().height
47+
return Height > 0 && Height <= 5
48+
}))
49+
console.debug(`[${UserscriptName}] Detected PL2 Promise.then Targeted`, Targeted)
50+
Targeted.forEach(Ele => {
51+
Ele.style.setProperty('display', 'none', 'important')
52+
})
53+
}, 250)
54+
return
4155
}
42-
4356
return OriginalReflectApply(Target, ThisArg, Args)
4457
}
4558
})

0 commit comments

Comments
 (0)