Skip to content

Commit fabd2e9

Browse files
cyfung1031CodFrm
andauthored
♻️ 重构 GM_xmlhttpRequest 及相关代码 (#901)
* 重构 `GM_xmlhttpRequest` 及相关代码 * 刪無用代碼 * mock修正 * 重构 `GM_donwload` * GM_download: 跟随TM,改使用 fetch * `GM_download` 代码修正 * `GM_download` 代码调整 * `GM_xmlhttpRequest` 修正 `context`;`GM_download` 新增 `context`, `user`, `password` * 代码调整,避免参考无法GC回收 * 支持 `binary: true` * 添加单元测试 * 定义成常量赋予含义 * 不用可以直接删 * 没地方引用dealXhrResponse了,可以直接删 * 修正response空值问题 * 新增 GM xhr API 黑名单判断 (与TM一致) * 单元测试更改成 concurrent * 单元测试代码调整 * FF兼容调整 * FF兼容调整 * 代码清理一下 * 整理单元测试mock * 整理代码结构 * 删除byPass逻辑,避免饶过的可能 * 通过单元测试 * 改善了reqID关联的代码设计 * typescript 要 return undefined * 修正 lastNwReqTriggerTime * 修正 * 中文注释 中文注释 * 删旧代码 * 抽出成 generateUniqueMarkerID * 中文注释 * 修正错误处理 * 异常处理: nwReqExecute 过时处理 * 时间值条件修改 * 勉强无幸福,只好退回 * 整理结构 * 测试脚本 * 整理 * 整理定义 * 整理fetch xhr * 整理 * 删除不使用的方法 --------- Co-authored-by: 王一之 <[email protected]>
1 parent 7b14f35 commit fabd2e9

36 files changed

+5033
-1083
lines changed

example/tests/gm_xhr_test.js

Lines changed: 1292 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,8 @@
1+
import EventEmitter from "eventemitter3";
2+
13
export default class WebRequest {
24
sendHeader?: (details: chrome.webRequest.OnSendHeadersDetails) => chrome.webRequest.BlockingResponse | void;
35

4-
mockXhr(xhr: any): any {
5-
return () => {
6-
const ret = new xhr();
7-
const header: chrome.webRequest.HttpHeader[] = [];
8-
ret.setRequestHeader = (k: string, v: string) => {
9-
header.push({
10-
name: k,
11-
value: v,
12-
});
13-
};
14-
const oldSend = ret.send.bind(ret);
15-
ret.send = (data: any) => {
16-
header.push({
17-
name: "cookie",
18-
value: "website=example.com",
19-
});
20-
const resp = this.sendHeader?.({
21-
method: ret.method,
22-
url: ret.url,
23-
requestHeaders: header,
24-
initiator: chrome.runtime.getURL(""),
25-
} as chrome.webRequest.OnSendHeadersDetails) as chrome.webRequest.BlockingResponse;
26-
resp.requestHeaders?.forEach((h) => {
27-
ret._authorRequestHeaders!.addHeader(h.name, h.value);
28-
});
29-
oldSend(data);
30-
};
31-
return ret;
32-
};
33-
}
34-
356
onBeforeSendHeaders = {
367
addListener: (callback: any) => {
378
this.sendHeader = callback;
@@ -49,4 +20,27 @@ export default class WebRequest {
4920
// TODO
5021
},
5122
};
23+
24+
onBeforeRequest = {
25+
counter: 0,
26+
EE: new EventEmitter<string, any>(),
27+
addListener: function (callback: (...args: any[]) => any) {
28+
this.EE.addListener("onBeforeRequest", (params) => {
29+
callback(params);
30+
});
31+
// TODO
32+
},
33+
};
34+
35+
onBeforeRedirect = {
36+
addListener: () => {
37+
// TODO
38+
},
39+
};
40+
41+
onErrorOccurred = {
42+
addListener: () => {
43+
// TODO
44+
},
45+
};
5246
}

src/app/service/content/create_context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { type ScriptRunResource } from "@App/app/repo/scripts";
22
import { v4 as uuidv4 } from "uuid";
33
import type { Message } from "@Packages/message/types";
44
import EventEmitter from "eventemitter3";
5-
import { GMContextApiGet } from "./gm_context";
6-
import { createGMBase } from "./gm_api";
7-
import { protect } from "./gm_context";
5+
import { GMContextApiGet } from "./gm_api/gm_context";
6+
import { protect } from "./gm_api/gm_context";
87
import { isEarlyStartScript } from "./utils";
98
import { ListenerManager } from "./listener_manager";
9+
import { createGMBase } from "./gm_api/gm_api";
1010

1111
// 构建沙盒上下文
1212
export const createContext = (

src/app/service/content/exec_script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { compileScript } from "./utils";
66
import type { Message } from "@Packages/message/types";
77
import type { ScriptLoadInfo } from "../service_worker/types";
88
import type { ValueUpdateDataEncoded } from "./types";
9-
import { evaluateGMInfo } from "./gm_info";
10-
import { type IGM_Base } from "./gm_api";
9+
import { evaluateGMInfo } from "./gm_api/gm_info";
10+
import type { IGM_Base } from "./gm_api/gm_api";
1111

1212
// 执行脚本,控制脚本执行与停止
1313
export default class ExecScript {

src/app/service/content/gm_api.test.ts renamed to src/app/service/content/gm_api/gm_api.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import ExecScript from "./exec_script";
3-
import type { ScriptLoadInfo } from "../service_worker/types";
4-
import type { GMInfoEnv, ScriptFunc } from "./types";
5-
import { compileScript, compileScriptCode } from "./utils";
2+
import ExecScript from "../exec_script";
3+
import type { ScriptLoadInfo } from "@App/app/service/service_worker/types";
4+
import type { GMInfoEnv, ScriptFunc } from "../types";
5+
import { compileScript, compileScriptCode } from "../utils";
66
import type { Message } from "@Packages/message/types";
77
import { encodeMessage } from "@App/pkg/utils/message_value";
88
import { v4 as uuidv4 } from "uuid";

0 commit comments

Comments
 (0)