Skip to content

改良测试用环境 #521

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
86 changes: 58 additions & 28 deletions src/app/service/content/create_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,11 @@ import { v4 as uuidv4 } from "uuid";
import type { Message } from "@Packages/message/types";
import EventEmitter from "eventemitter3";
import { GMContextApiGet } from "./gm_context";
import { createGMBase } from "./gm_api";
import { createGMBase, type GMApiType } from "./gm_api";

function _createContext(options:{[key:string]:any}, scriptGrants:Set<string> | string[]){
const context = createGMBase(options);

// 构建沙盒上下文
export function createContext(
scriptRes: ScriptRunResource,
GMInfo: any,
envPrefix: string,
message: Message,
scriptGrants: Set<string>
) {
// 按照GMApi构建
const valueChangeListener = new Map<number, { name: string; listener: GMTypes.ValueChangeListener }>();
const EE: EventEmitter = new EventEmitter();
const context = createGMBase({
prefix: envPrefix,
message,
scriptRes,
valueChangeListener,
EE,
runFlag: uuidv4(),
eventId: 10000,
GM: { info: GMInfo },
GM_info: GMInfo,
window: {
onurlchange: null,
},
grantSet: new Set(),
});
const grantedAPIs: { [key: string]: any } = {};
const __methodInject__ = (grant: string): boolean => {
const grantSet: Set<string> = context.grantSet;
Expand Down Expand Up @@ -64,6 +41,59 @@ export function createContext(
g = g[part] || (g[part] = (grantedAPIs[s] || {}));
}
}
context.unsafeWindow = window;
return <GMApiType>context;
}

// 构建沙盒上下文
export function createContext(
scriptRes: ScriptRunResource,
GMInfo: any,
envPrefix: string,
message: Message,
scriptGrants: Set<string> | string[]
) {
// 按照GMApi构建
const valueChangeListener = new Map<number, { name: string; listener: GMTypes.ValueChangeListener }>();
const EE: EventEmitter = new EventEmitter();
const context = _createContext({
prefix: envPrefix,
message,
scriptRes,
valueChangeListener,
EE,
runFlag: uuidv4(),
eventId: 10000,
GM: { info: GMInfo },
GM_info: GMInfo,
window: {
onurlchange: null,
},
grantSet: new Set(),
unsafeWindow: window
}, scriptGrants);
return context;
}

// 构建测试用沙盒上下文
export function createTestContext(
prefix: string,
message: Message,
scriptRes: ScriptRunResource,
scriptGrants: Set<string> | string[]
) {

const valueChangeListener = new Map<number, { name: string; listener: GMTypes.ValueChangeListener }>();
const EE: EventEmitter = new EventEmitter();
const context = _createContext({
prefix,
message,
scriptRes,
valueChangeListener,
EE,
notificationTagMap: new Map(),
eventId: 0,
grantSet: new Set(),
}, scriptGrants);

return context;
}
6 changes: 4 additions & 2 deletions src/app/service/content/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class GM_Base implements IGM_Base {
}
}

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

// 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出
const { _GM_getValue, _GM_cookie, _GM_setValue, _GM_xmlhttpRequest } = GMApi;

export type GMApiType = InstanceType<typeof GMApi>;
6 changes: 3 additions & 3 deletions tests/runtime/gm_api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Script, ScriptDAO, type ScriptRunResource } from "@App/app/repo/scripts";
import GMApi from "@App/app/service/content/gm_api";
import { createTestContext } from "@App/app/service/content/create_context";
import { initTestEnv, initTestGMApi } from "@Tests/utils";
import { randomUUID } from "crypto";
import { newMockXhr } from "mock-xmlhttprequest";
Expand Down Expand Up @@ -33,9 +33,9 @@ beforeAll(async () => {
});

describe("GM xmlHttpRequest", () => {
const gmApi = new GMApi("serviceWorker", msg, <ScriptRunResource>{
const gmApi = createTestContext("serviceWorker", msg, <ScriptRunResource>{
uuid: script.uuid
});
}, ["GM_xmlhttpRequest"]);
const mockXhr = newMockXhr();
mockXhr.onSend = async (request) => {
switch (request.url) {
Expand Down
6 changes: 3 additions & 3 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { randomUUID } from "crypto";
import { newMockXhr } from "mock-xmlhttprequest";
import type { Script, ScriptRunResource } from "@App/app/repo/scripts";
import { ScriptDAO } from "@App/app/repo/scripts";
import GMApi from "@App/app/service/content/gm_api";
import { createTestContext } from "@App/app/service/content/create_context";

initTestEnv();

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