Skip to content

Commit 4ef525e

Browse files
committed
feat: protect Function.prototype.toString and use Reflect.apply for calling
1 parent 072f73f commit 4ef525e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

sources/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ declare const unsafeWindow: unsafeWindow
1515
const Win = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window
1616

1717
export function RunNamuLinkUserscript(BrowserWindow: typeof window, UserscriptName: string = 'NamuLink'): void {
18+
const ProtectedFunctionPrototypeToString = BrowserWindow.Function.prototype.toString
19+
1820
function GetParents(Ele: HTMLElement) {
1921
let Parents: HTMLElement[] = []
2022
while (Ele.parentElement) {
@@ -68,7 +70,7 @@ export function RunNamuLinkUserscript(BrowserWindow: typeof window, UserscriptNa
6870
BrowserWindow.Function.prototype.bind = new Proxy(BrowserWindow.Function.prototype.bind, {
6971
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
7072
apply(Target: typeof Function.prototype.bind, ThisArg: Function, Args: Parameters<typeof Function.prototype.bind>) {
71-
let StringifiedFunc = ThisArg.toString()
73+
let StringifiedFunc = Reflect.apply(ProtectedFunctionPrototypeToString, ThisArg, Args) as string
7274
if (PowerLinkGenerationPositiveRegExps.filter(PowerLinkGenerationPositiveRegExp => PowerLinkGenerationPositiveRegExp.filter(Index => Index.test(StringifiedFunc)).length >= 3).length === 1) {
7375
console.debug(`[${UserscriptName}]: Function.prototype.bind:`, ThisArg)
7476
return Reflect.apply(Target, () => {}, [])
@@ -87,7 +89,7 @@ export function RunNamuLinkUserscript(BrowserWindow: typeof window, UserscriptNa
8789

8890
BrowserWindow.setTimeout = new Proxy(BrowserWindow.setTimeout, {
8991
apply(Target: typeof setTimeout, ThisArg: undefined, Args: Parameters<typeof setTimeout>) {
90-
let StringifiedFunc = Args[0].toString()
92+
let StringifiedFunc = Reflect.apply(ProtectedFunctionPrototypeToString, Args[0], Args) as string
9193
if (PowerLinkGenerationSkeletionPositiveRegExps.filter(PowerLinkGenerationSkeletionPositiveRegExp => PowerLinkGenerationSkeletionPositiveRegExp.filter(Index => Index.test(StringifiedFunc)).length >= 1).length === 1) {
9294
console.debug(`[${UserscriptName}]: setTimeout:`, Args[0])
9395
return

0 commit comments

Comments
 (0)