Skip to content

Commit

Permalink
fix: #83 #84
Browse files Browse the repository at this point in the history
  • Loading branch information
msojocs committed Sep 23, 2023
1 parent 2a9f765 commit ecbeada
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
1 change: 0 additions & 1 deletion extensions/area_unlimit/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ console.log("[hook]: hook.js", fileName)
const ext = chrome.extension
const URLS = {
md5: chrome.extension.getURL(`utils/md5.js`),
load: chrome.extension.getURL(`hook/load.js`),
index: chrome.extension.getURL(`hook/index.js`),
search: chrome.extension.getURL(`hook/search.js`),
player: chrome.extension.getURL(`hook/player.js`),
Expand Down
2 changes: 1 addition & 1 deletion extensions/area_unlimit/hook/biliapp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

(() => {
console.log("[hook]: index.js");
console.log("[hook]: biliapp.js");
const HTTP_INDEX = {
get(url) {
return new Promise((resolve, reject) => {
Expand Down
76 changes: 75 additions & 1 deletion extensions/area_unlimit/hook/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,53 @@ const URL_HOOK = {
}
}
}
const URL_HOOK_FETCH = {
/**
* 搜索
* @param {{urlInfo: [string, string], config: RequestInit, res: Response }} data 原请求结果
* @returns {Promise<Response>}
*/
"https://api.bilibili.com/x/web-interface/search/type": async (data) => {
// console.log('===搜索 HOOK: ', req)
const params = UTILS._params2obj(data.urlInfo[1])
if (params.search_type === 'media_bangumi') {
// 搜索番剧
const searchResult = await data.res.json()
console.log('预期结果:', searchResult)
searchResult.data.result = searchResult.data.result || []
const api = new BiliBiliApi()
const serverList = JSON.parse(localStorage.serverList || "{}")
for (let area in serverList) {
const server = serverList[area] || ""
if (server.length === 0) continue

api.setServer(server)
try {
// const buvid3 = await cookieStore.get('buvid3') || {}
function sleep(d) {
for (var t = Date.now(); Date.now() - t <= d;) {
}
}

sleep(500); //当前方法暂停0.5秒
// const result = await api.searchBangumi2(req._params, area, buvid3.value || '')
const result = await api.searchBangumi(params, area)
// console.log('searchResult:', result)
result.forEach(s => {
s.title = `[${area}]${s.title}`
})
searchResult.data.result.push(...result)
data.res.data = searchResult
} catch (err) {

console.error('搜索异常:', err)
}
}
}
return data.res
},

}

/*请求响应修改器1.0*/
window.getHookXMLHttpRequest = (win) => {
Expand Down Expand Up @@ -873,6 +920,31 @@ window.getHookXMLHttpRequest = (win) => {

}

const originalFetch = window.fetch
if (fetch.toString().includes('[native code]')) {
window.fetch = async (url, config) => {
console.log('fetch:', url, config)
const res = await originalFetch(url, config)
// const u = new URL(url.startsWith('//') ? `https:${url}` : url)
// console.log('u.pathname:', u.pathname)
console.log('res:', res)
const [path, params] = url.split(/\?/);
if (URL_HOOK_FETCH[path]) {
// debugger
try {
return await URL_HOOK_FETCH[path]({
urlInfo: [path, params],
config,
res
})
}catch (e) {
console.error(e)
}
}
return res
}
}

function _deCode(params) {
return params.split("&").map((a) => {
const [key, value] = a.split("=");
Expand All @@ -882,7 +954,9 @@ function _deCode(params) {
}

console.log('替换XMLHttpRequest')
window.XMLHttpRequest = getHookXMLHttpRequest(window);
if (!location.href.includes('live.bilibili')) {
window.XMLHttpRequest = getHookXMLHttpRequest(window);
}

function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) {
Expand Down
8 changes: 8 additions & 0 deletions extensions/area_unlimit/hook/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
const fileName = url.pathname.substring(1).split('.')[0]
console.log("[hook]: load.js", fileName)

if (!location.href.includes('live.bilibili')) {
if (parent?.getHookXMLHttpRequest)
window.getHookXMLHttpRequest = parent?.getHookXMLHttpRequest
if (window.getHookXMLHttpRequest && undefined === window.XMLHttpRequest.isHooked) {
console.log('[hook]: load replace XMLHttpRequest')
window.XMLHttpRequest = window.getHookXMLHttpRequest(window)
}
}
const getData = (name) => {
return new Promise((resolve, reject) => {
document.addEventListener('ROAMING_sendURL', async function (e) {
Expand Down
2 changes: 1 addition & 1 deletion extensions/area_unlimit/hook/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try {
console.log('[hook]: search.js', window.getHookXMLHttpRequest)
console.log('[hook]: search.js', window.XMLHttpRequest)
window.hex_md5 = parent?.hex_md5
if (parent?.getHookXMLHttpRequest)
window.getHookXMLHttpRequest = parent?.getHookXMLHttpRequest
Expand Down
1 change: 0 additions & 1 deletion extensions/area_unlimit/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"web_accessible_resources": [
{
"resources": [
"hook/load.js",
"hook/hook.js",
"hook/player.js",
"hook/index.js",
Expand Down

0 comments on commit ecbeada

Please sign in to comment.