Skip to content

Commit ee4a8b2

Browse files
cyfung1031CodFrm
andauthored
♻️ GMApiRequest 代码调整,GM_log 代码修正,@connect判断修正 (#849)
* `GMApiRequest` 代码调整,`GM_log` 代码修正,`@connect`判断修正 * 加入 isConnectMatched 的单元测试 * Update src/app/service/content/gm_api.ts * 域名不区分大小写 --------- Co-authored-by: wangyizhi <[email protected]> Co-authored-by: 王一之 <[email protected]>
1 parent 080a859 commit ee4a8b2

File tree

6 files changed

+226
-108
lines changed

6 files changed

+226
-108
lines changed

src/app/service/content/gm_api.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,7 @@ export default class GMApi extends GM_Base {
456456
if (typeof message !== "string") {
457457
message = JSON.stringify(message);
458458
}
459-
const requestParams: any[] = [message, level];
460-
if (labels.length > 0) {
461-
requestParams.push(labels);
462-
}
463-
this.sendMessage("GM_log", requestParams);
459+
this.sendMessage("GM_log", [message, level, labels]);
464460
}
465461

466462
@GMContext.API()
@@ -758,7 +754,7 @@ export default class GMApi extends GM_Base {
758754
this.sendMessage("CAT_fileStorage", ["config"]);
759755
return;
760756
}
761-
const sendDetails: { [key: string]: string } = {
757+
const sendDetails: CATType.CATFileStorageDetails = {
762758
baseDir: details.baseDir || "",
763759
path: details.path || "",
764760
filename: details.filename,
@@ -1056,7 +1052,7 @@ export default class GMApi extends GM_Base {
10561052
timeout: details.timeout,
10571053
cookie: details.cookie,
10581054
anonymous: details.anonymous,
1059-
},
1055+
} as GMTypes.DownloadDetails,
10601056
]).then((con) => {
10611057
connect = con;
10621058
connect.onMessage((data) => {
@@ -1257,7 +1253,7 @@ export default class GMApi extends GM_Base {
12571253
onclose() {},
12581254
};
12591255

1260-
this.sendMessage("GM_openInTab", [url, option]).then((id) => {
1256+
this.sendMessage("GM_openInTab", [url, option as GMTypes.SWOpenTabOptions]).then((id) => {
12611257
if (!this.EE) return;
12621258
if (id) {
12631259
tabid = id;
@@ -1338,7 +1334,7 @@ export default class GMApi extends GM_Base {
13381334
}
13391335

13401336
@GMContext.API({})
1341-
GM_setClipboard(data: string, info?: string | { type?: string; mimetype?: string }, cb?: () => void) {
1337+
GM_setClipboard(data: string, info?: GMTypes.GMClipboardInfo, cb?: () => void) {
13421338
if (this.isInvalidContext()) return;
13431339
this.sendMessage("GM_setClipboard", [data, info])
13441340
.then(() => {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, it, expect } from "vitest";
2+
import { type IGetSender } from "@Packages/message/server";
3+
import { type ExtMessageSender } from "@Packages/message/types";
4+
import { isConnectMatched } from "./gm_api";
5+
6+
// 小工具:建立假的 IGetSender
7+
const makeSender = (url?: string): IGetSender => ({
8+
getSender: () => (url ? { url } : {}),
9+
getType: () => 0,
10+
isType: (_type: any) => false,
11+
getExtMessageSender: () => null as unknown as ExtMessageSender,
12+
getConnect: () => undefined,
13+
});
14+
15+
describe("isConnectMatched", () => {
16+
it("回传 false 当 metadataConnect 为 undefined 或空阵列", () => {
17+
const req = new URL("https://api.example.com/v1");
18+
expect(isConnectMatched(undefined, req, makeSender("https://app.example.com"))).toBe(false);
19+
expect(isConnectMatched([], req, makeSender("https://app.example.com"))).toBe(false);
20+
});
21+
22+
it('遇到 "*" 应回传 true', () => {
23+
const req = new URL("https://anything.example.com/path");
24+
expect(isConnectMatched(["*"], req, makeSender())).toBe(true);
25+
});
26+
27+
it("尾缀网域比对成功时回传 true(example.com 比对 api.example.com)", () => {
28+
const req = new URL("https://api.example.com/users");
29+
expect(isConnectMatched(["example.com"], req, makeSender())).toBe(true);
30+
expect(isConnectMatched(["foo.com", "bar.net", "example.com"], req, makeSender())).toBe(true);
31+
expect(isConnectMatched(["foo.com", "bar.net", "api.example.com"], req, makeSender())).toBe(true);
32+
expect(isConnectMatched(["foo.com", "bar.net", "apiexample.com"], req, makeSender())).toBe(false);
33+
});
34+
35+
it("尾缀网域比对成功时回传 true(myapple.com vs apple.com)", () => {
36+
const req = new URL("https://myapple.com/users");
37+
expect(isConnectMatched(["myapple.com"], req, makeSender())).toBe(true);
38+
expect(isConnectMatched(["apple.com"], req, makeSender())).toBe(false);
39+
});
40+
41+
it('metadata 包含 "self" 且 sender.url 与 reqURL 主机相同时回传 true', () => {
42+
const req = new URL("https://app.example.com/dashboard");
43+
const sender = makeSender("https://app.example.com/some-page");
44+
expect(isConnectMatched(["self"], req, sender)).toBe(true);
45+
});
46+
47+
it('metadata 包含 "self" 但 sender.url 与 reqURL 主机不同时回传 false(若无其他规则命中)', () => {
48+
const req = new URL("https://api.example.com/resource");
49+
const sender = makeSender("https://news.example.com/article");
50+
expect(isConnectMatched(["self"], req, sender)).toBe(false);
51+
});
52+
53+
it('当 sender.getSender() 回传没有 url 或无效 URL 时,"self" 不应报错且回传 false(若无其他规则命中)', () => {
54+
const req = new URL("https://example.com/path");
55+
56+
// 无 url
57+
const senderNoUrl = makeSender();
58+
expect(isConnectMatched(["self"], req, senderNoUrl)).toBe(false);
59+
60+
// 无效 URL(try/catch 会吞掉错误)
61+
const senderBadUrl = makeSender("not a valid url");
62+
expect(isConnectMatched(["self"], req, senderBadUrl)).toBe(false);
63+
});
64+
65+
it('当 "self" 不符合但尾缀规则符合时仍应回传 true(走到后续条件)', () => {
66+
const req = new URL("https://api.example.com/data");
67+
const sender = makeSender("https://other.site.com/");
68+
expect(isConnectMatched(["self", "example.com"], req, sender)).toBe(true);
69+
});
70+
71+
it("完全不匹配时回传 false", () => {
72+
const req = new URL("https://api.foo.com");
73+
const sender = makeSender("https://bar.com");
74+
expect(isConnectMatched(["baz.com", "qux.net"], req, sender)).toBe(false);
75+
});
76+
77+
it("域名不区分大小写", () => {
78+
const req = new URL("https://API.Example.COM/Path");
79+
expect(isConnectMatched(["example.com"], req, makeSender())).toBe(true);
80+
expect(isConnectMatched(["EXAMPLE.COM"], req, makeSender())).toBe(true);
81+
expect(isConnectMatched(["Api.Example.com"], req, makeSender())).toBe(true);
82+
});
83+
});

0 commit comments

Comments
 (0)