Skip to content

Commit bfa2712

Browse files
committed
改良测试用环境
1 parent 3d3be2c commit bfa2712

File tree

4 files changed

+68
-36
lines changed

4 files changed

+68
-36
lines changed

src/app/service/content/create_context.ts

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,11 @@ import { v4 as uuidv4 } from "uuid";
33
import type { Message } from "@Packages/message/types";
44
import EventEmitter from "eventemitter3";
55
import { GMContextApiGet } from "./gm_context";
6-
import { createGMBase } from "./gm_api";
6+
import { createGMBase, type GMApiType } from "./gm_api";
7+
8+
function _createContext(options:{[key:string]:any}, scriptGrants:Set<string> | string[]){
9+
const context = createGMBase(options);
710

8-
// 构建沙盒上下文
9-
export function createContext(
10-
scriptRes: ScriptRunResource,
11-
GMInfo: any,
12-
envPrefix: string,
13-
message: Message,
14-
scriptGrants: Set<string>
15-
) {
16-
// 按照GMApi构建
17-
const valueChangeListener = new Map<number, { name: string; listener: GMTypes.ValueChangeListener }>();
18-
const EE: EventEmitter = new EventEmitter();
19-
const context = createGMBase({
20-
prefix: envPrefix,
21-
message,
22-
scriptRes,
23-
valueChangeListener,
24-
EE,
25-
runFlag: uuidv4(),
26-
eventId: 10000,
27-
GM: { info: GMInfo },
28-
GM_info: GMInfo,
29-
window: {
30-
onurlchange: null,
31-
},
32-
grantSet: new Set(),
33-
});
3411
const grantedAPIs: { [key: string]: any } = {};
3512
const __methodInject__ = (grant: string): boolean => {
3613
const grantSet: Set<string> = context.grantSet;
@@ -64,6 +41,59 @@ export function createContext(
6441
g = g[part] || (g[part] = (grantedAPIs[s] || {}));
6542
}
6643
}
67-
context.unsafeWindow = window;
44+
return <GMApiType>context;
45+
}
46+
47+
// 构建沙盒上下文
48+
export function createContext(
49+
scriptRes: ScriptRunResource,
50+
GMInfo: any,
51+
envPrefix: string,
52+
message: Message,
53+
scriptGrants: Set<string> | string[]
54+
) {
55+
// 按照GMApi构建
56+
const valueChangeListener = new Map<number, { name: string; listener: GMTypes.ValueChangeListener }>();
57+
const EE: EventEmitter = new EventEmitter();
58+
const context = _createContext({
59+
prefix: envPrefix,
60+
message,
61+
scriptRes,
62+
valueChangeListener,
63+
EE,
64+
runFlag: uuidv4(),
65+
eventId: 10000,
66+
GM: { info: GMInfo },
67+
GM_info: GMInfo,
68+
window: {
69+
onurlchange: null,
70+
},
71+
grantSet: new Set(),
72+
unsafeWindow: window
73+
}, scriptGrants);
74+
return context;
75+
}
76+
77+
// 构建测试用沙盒上下文
78+
export function createTestContext(
79+
prefix: string,
80+
message: Message,
81+
scriptRes: ScriptRunResource,
82+
scriptGrants: Set<string> | string[]
83+
) {
84+
85+
const valueChangeListener = new Map<number, { name: string; listener: GMTypes.ValueChangeListener }>();
86+
const EE: EventEmitter = new EventEmitter();
87+
const context = _createContext({
88+
prefix,
89+
message,
90+
scriptRes,
91+
valueChangeListener,
92+
EE,
93+
notificationTagMap: new Map(),
94+
eventId: 0,
95+
grantSet: new Set(),
96+
}, scriptGrants);
97+
6898
return context;
6999
}

src/app/service/content/gm_api.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class GM_Base implements IGM_Base {
108108
}
109109
}
110110

111-
// GMApi 定义 外部用API函数。不使用@protected
112-
export default class GMApi extends GM_Base {
111+
// GMApi 定义 外部用API函数。不使用@protected。定義API用。不導出
112+
class GMApi extends GM_Base {
113113
/**
114114
* <tag, notificationId>
115115
*/
@@ -1157,3 +1157,5 @@ export const { createGMBase } = GM_Base;
11571157

11581158
// 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出
11591159
const { _GM_getValue, _GM_cookie, _GM_setValue, _GM_xmlhttpRequest } = GMApi;
1160+
1161+
export type GMApiType = InstanceType<typeof GMApi>;

tests/runtime/gm_api.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Script, ScriptDAO, type ScriptRunResource } from "@App/app/repo/scripts";
2-
import GMApi from "@App/app/service/content/gm_api";
2+
import { createTestContext } from "@App/app/service/content/create_context";
33
import { initTestEnv, initTestGMApi } from "@Tests/utils";
44
import { randomUUID } from "crypto";
55
import { newMockXhr } from "mock-xmlhttprequest";
@@ -33,9 +33,9 @@ beforeAll(async () => {
3333
});
3434

3535
describe("GM xmlHttpRequest", () => {
36-
const gmApi = new GMApi("serviceWorker", msg, <ScriptRunResource>{
36+
const gmApi = createTestContext("serviceWorker", msg, <ScriptRunResource>{
3737
uuid: script.uuid
38-
});
38+
}, ["GM_xmlhttpRequest"]);
3939
const mockXhr = newMockXhr();
4040
mockXhr.onSend = async (request) => {
4141
switch (request.url) {

tests/utils.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { randomUUID } from "crypto";
44
import { newMockXhr } from "mock-xmlhttprequest";
55
import type { Script, ScriptRunResource } from "@App/app/repo/scripts";
66
import { ScriptDAO } from "@App/app/repo/scripts";
7-
import GMApi from "@App/app/service/content/gm_api";
7+
import { createTestContext } from "@App/app/service/content/create_context";
88

99
initTestEnv();
1010

@@ -29,9 +29,9 @@ describe("测试GMApi环境", async () => {
2929
checktime: 0,
3030
};
3131
await new ScriptDAO().save(script);
32-
const gmApi = new GMApi("serviceWorker", msg, <ScriptRunResource>{
32+
const gmApi = createTestContext("serviceWorker", msg, <ScriptRunResource>{
3333
uuid: script.uuid
34-
});
34+
}, ["GM_xmlhttpRequest"]);
3535
const mockXhr = newMockXhr();
3636
mockXhr.onSend = async (request) => {
3737
return request.respond(200, {}, "example");

0 commit comments

Comments
 (0)