mirror of
https://github.com/naiyQAQ/fanqie-assistant.git
synced 2026-10-21 01:54:48 +00:00
fix: 修复因请求拦截没有 resolve 导致的页面部分内容卡死不渲染
This commit is contained in:
1 parent
539073564b
commit
cbb2006e47
1 file changed
+63
-5
+63
-5
@@ -9,6 +9,12 @@ const blackList = [
|
||||
'hm.baidu.com'
|
||||
]
|
||||
|
||||
const BLOCKED_BODY = JSON.stringify({
|
||||
e: 0,
|
||||
sc: 10,
|
||||
tc: 10
|
||||
})
|
||||
|
||||
function checkBlack(url: string): boolean {
|
||||
return blackList.some(black => url.includes(black))
|
||||
}
|
||||
@@ -28,9 +34,10 @@ unsafeWindow.fetch = function fetch(input: RequestInfo | URL, init?: RequestInit
|
||||
|
||||
if (checkBlack(url)) {
|
||||
console.log('blocked request: ' + url)
|
||||
return new Promise(() => {
|
||||
return new Response('{}', { status: 403 })
|
||||
})
|
||||
return Promise.resolve(new Response(BLOCKED_BODY, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}))
|
||||
}
|
||||
|
||||
return originalFetch(input, init)
|
||||
@@ -39,13 +46,64 @@ unsafeWindow.fetch = function fetch(input: RequestInfo | URL, init?: RequestInit
|
||||
const originalXMLHttpRequest = unsafeWindow.XMLHttpRequest
|
||||
|
||||
unsafeWindow.XMLHttpRequest = class XMLHttpRequest extends originalXMLHttpRequest {
|
||||
private _blockedUrl?: string
|
||||
|
||||
open(method: string, url: string, async: boolean = true, user?: string | null, password?: string | null): void {
|
||||
if (checkBlack(url)) {
|
||||
console.log('blocked request: ' + url)
|
||||
this.abort()
|
||||
} else {
|
||||
this._blockedUrl = url
|
||||
return
|
||||
}
|
||||
this._blockedUrl = undefined
|
||||
super.open(method, url, async, user, password)
|
||||
}
|
||||
|
||||
setRequestHeader(name: string, value: string): void {
|
||||
if (this._blockedUrl !== undefined) return
|
||||
super.setRequestHeader(name, value)
|
||||
}
|
||||
|
||||
send(body?: Document | XMLHttpRequestBodyInit | null): void {
|
||||
if (this._blockedUrl === undefined) {
|
||||
super.send(body)
|
||||
return
|
||||
}
|
||||
const url = this._blockedUrl
|
||||
const shadow = (prop: string, value: unknown) =>
|
||||
Object.defineProperty(this, prop, { configurable: true, get: () => value })
|
||||
|
||||
setTimeout(() => {
|
||||
shadow('readyState', 4)
|
||||
shadow('status', 200)
|
||||
shadow('statusText', 'OK')
|
||||
shadow('responseURL', url)
|
||||
shadow('responseText', this.responseType === '' || this.responseType === 'text' ? BLOCKED_BODY : '')
|
||||
shadow('response', this.responseType === 'json' ? {
|
||||
"e": 0,
|
||||
"sc": 10,
|
||||
"tc": 10
|
||||
} : BLOCKED_BODY)
|
||||
this.dispatchEvent(new Event('readystatechange'))
|
||||
this.dispatchEvent(new ProgressEvent('load'))
|
||||
this.dispatchEvent(new ProgressEvent('loadend'))
|
||||
}, 0)
|
||||
}
|
||||
|
||||
abort(): void {
|
||||
if (this._blockedUrl !== undefined) return
|
||||
super.abort()
|
||||
}
|
||||
|
||||
getAllResponseHeaders(): string {
|
||||
if (this._blockedUrl !== undefined) return 'content-type: application/json\r\n'
|
||||
return super.getAllResponseHeaders()
|
||||
}
|
||||
|
||||
getResponseHeader(name: string): string | null {
|
||||
if (this._blockedUrl !== undefined) {
|
||||
return name.toLowerCase() === 'content-type' ? 'application/json' : null
|
||||
}
|
||||
return super.getResponseHeader(name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user