Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mock Win.String.prototype.indexOf for navigation #101

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions NamuLink.user.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "namulink",
"version": "3.15.2",
"version": "3.16.0",
"description": "",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion sources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// @downloadURL https://cdn.jsdelivr.net/gh/List-KR/NamuLink@latest/NamuLink.user.js
// @license MIT
//
// @version 3.15.2
// @version 3.16.0
// @author PiQuark6046 and contributors
//
// @match https://namu.wiki/*
Expand Down
51 changes: 2 additions & 49 deletions sources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,12 @@ const Win = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window

const NamuWikiUnloadedAdEvent = new Event('namuwikiunloadedadvert')
const NamuWikiLoadedAdEvent = new Event('namuwikiloadedadvert')
const NagivationEvent = new Event('namuwikinavigation')

const SubString = ['substring', 'substr']
for (const SubStringFunction of SubString) {
Win.String.prototype[SubStringFunction] = new Proxy(Win.String.prototype[SubStringFunction], {
apply(Target, ThisArg, Args) {
if (typeof ThisArg === 'string' && /^[a-zA-Z0-9_]+--?[a-zA-Z0-9_]+(-[a-zA-Z0-9_]+-[a-zA-Z0-9_]+){0,}$/.test(ThisArg)) {
console.debug(`[NamuLink:index]: String.prototype.${SubStringFunction}:`, ThisArg)
Win.dispatchEvent(NamuWikiUnloadedAdEvent)
}
if (typeof ThisArg === 'string' && ThisArg === 'headAttrs') {
Win.dispatchEvent(NamuWikiUnloadedAdEvent)
}
return Reflect.apply(Target, ThisArg, Args)
}
})
}

Win.TextDecoder.prototype.decode = new Proxy(Win.TextDecoder.prototype.decode, {
Win.String.prototype.indexOf = new Proxy(Win.String.prototype.indexOf, {
apply(Target, ThisArg, Args) {
const Result = Reflect.apply(Target, ThisArg, Args)
if (typeof Result === 'string' && /^\[+.{0,10}#.{10,50}\/\/\/.{0,20}==/.test(Result)) {
console.debug('[NamuLink:index]: TextDecoder.prototype.decode', Result)
if (typeof Args[0] === 'string' && Args[0] === 'key') {
Win.dispatchEvent(NamuWikiUnloadedAdEvent)
return ''
}
return Result
}
})

const Timer = ['setTimeout', 'setInterval']
for (const TimerFunction of Timer) {
Win[TimerFunction] = new Proxy(Win[TimerFunction], {
apply(Target, ThisArg, Args) {
if (typeof Args[0] === 'function' && typeof Args[1] === 'number'
// eslint-disable-next-line @typescript-eslint/ban-types
&& (/return {0,}new {0,}Promise.+\.apply {0,}\(.+function.+next.+throw.+void/.test((Args[0] as Function).toString())
// eslint-disable-next-line @typescript-eslint/ban-types
|| /if {0,}\(('|")[a-zA-Z0-9_]+('|") {0,}===? {0,}.+return.+else/.test((Args[0] as Function).toString()))) {
console.debug(`[NamuLink:index]: ${TimerFunction}:`, Args)
Win.dispatchEvent(NamuWikiUnloadedAdEvent)
return
}
return Reflect.apply(Target, ThisArg, Args)
}
})
}

Win.Array.prototype.join = new Proxy(Win.Array.prototype.join, {
apply(Target, ThisArg, Args) {
const Result = Reflect.apply(Target, ThisArg, Args)
if ((Result as string).startsWith('noscript[data-n-head="]')) {
Win.dispatchEvent(NagivationEvent)
}
return Result
}
Expand Down