Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1459b39
处理service worker没有MouseEvent的问题
CodFrm Jan 22, 2026
5003511
wip
CodFrm Jan 26, 2026
64795b4
wip
CodFrm Jan 27, 2026
dadaff4
wip
CodFrm Jan 27, 2026
c584950
重构消息机制
CodFrm Jan 28, 2026
41b28c1
修复type问题
CodFrm Jan 28, 2026
22cfe40
Merge branch 'develop/messaging-performance-boost2' into refactor/1067
CodFrm Jan 28, 2026
2e5821e
解决冲突
CodFrm Jan 28, 2026
5724b6f
单元测试
CodFrm Jan 28, 2026
62dd049
ScriptCat代碼 不使用 EventListenerObject
cyfung1031 Jan 28, 2026
9ed89ef
刪無關Debug代碼
cyfung1031 Jan 28, 2026
b67ffbd
刪未使用 getMessageFlag()
cyfung1031 Jan 28, 2026
3a5f420
注釋修訂
cyfung1031 Jan 28, 2026
a6522e3
统一写法降低维护成本
cyfung1031 Jan 28, 2026
e870ea4
ScriptCat代碼 不使用 EventListenerObject
cyfung1031 Jan 28, 2026
6d510a6
revised negotiateEventFlag
cyfung1031 Jan 28, 2026
7e9c6b3
Update common.ts
cyfung1031 Jan 28, 2026
49e8718
修复flag顺序导致的协商问题
CodFrm Jan 29, 2026
59e0e67
调整单元测试参数位置
CodFrm Jan 29, 2026
0471ca0
vitest env fix
cyfung1031 Jan 29, 2026
cf1b053
Revert: 调整单元测试参数位置
cyfung1031 Jan 29, 2026
790ce77
lint
cyfung1031 Jan 29, 2026
9ab440a
Revert: 调整单元测试参数位置
cyfung1031 Jan 29, 2026
d49a82e
調整代碼
cyfung1031 Jan 29, 2026
1250bb9
加入 readyDeferred 和 isReady
cyfung1031 Jan 29, 2026
cf9029b
fix
cyfung1031 Jan 29, 2026
e7c93ca
fix unit test
cyfung1031 Jan 29, 2026
267a24e
调整单元测试
CodFrm Jan 30, 2026
1178864
调整单元测试
CodFrm Jan 30, 2026
a9afe4f
Merge branch 'develop/messaging-performance-boost2' into refactor/1067
CodFrm Jan 30, 2026
4436817
单元测试
CodFrm Jan 30, 2026
d31baf3
单元测试
cyfung1031 Jan 30, 2026
c11d6e7
统一大小写
cyfung1031 Jan 30, 2026
11cdd34
刪無用代碼
cyfung1031 Jan 30, 2026
812625e
抽取至共通 ReadyWrap
cyfung1031 Jan 30, 2026
417d80a
ReadyWrap 釋放已使用 resolve, promise
cyfung1031 Jan 30, 2026
34241e5
删除debug日志和调整测试
CodFrm Jan 31, 2026
f0ec0db
调整日志等级
CodFrm Jan 31, 2026
2a6a049
修复测试脚本GM log使用问题
CodFrm Jan 31, 2026
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
148 changes: 84 additions & 64 deletions example/tests/early_inject_content_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,93 @@
// @run-at document-start
// ==/UserScript==

(async function () {
"use strict";

console.log("%c=== Content环境 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");

let testResults = {
passed: 0,
failed: 0,
total: 0,
};

// 测试辅助函数(支持同步和异步)
async function test(name, fn) {
testResults.total++;
try {
await fn();
testResults.passed++;
console.log(`%c✓ ${name}`, "color: green;");
return true;
} catch (error) {
testResults.failed++;
console.error(`%c✗ ${name}`, "color: red;", error);
return false;
}
// 测试辅助函数(支持同步和异步)
async function test(name, fn) {
testResults.total++;
try {
await fn();
testResults.passed++;
console.log(`%c✓ ${name}`, "color: green;");
return true;
} catch (error) {
testResults.failed++;
console.error(`%c✗ ${name}`, "color: red;", error);
return false;
}

// assert(expected, actual, message) - 比较两个值是否相等
function assert(expected, actual, message) {
if (expected !== actual) {
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
throw new Error(error);
}
}

function testSync(name, fn) {
testResults.total++;
try {
fn();
testResults.passed++;
console.log(`%c✓ ${name}`, "color: green;");
return true;
} catch (error) {
testResults.failed++;
console.error(`%c✗ ${name}`, "color: red;", error);
return false;
}
}

// assert(expected, actual, message) - 比较两个值是否相等
function assert(expected, actual, message) {
if (expected !== actual) {
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
throw new Error(error);
}
}

// assertTrue(condition, message) - 断言条件为真
function assertTrue(condition, message) {
if (!condition) {
throw new Error(message || "断言失败: 期望条件为真");
}
// assertTrue(condition, message) - 断言条件为真
function assertTrue(condition, message) {
if (!condition) {
throw new Error(message || "断言失败: 期望条件为真");
}
}

console.log("%c=== Content环境 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");

let testResults = {
passed: 0,
failed: 0,
total: 0,
};

// 同步测试

// ============ GM_addElement/GM_addStyle 测试 ============
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");

testSync("GM_addElement", () => {
const element = GM_addElement("div", {
textContent: "GM_addElement测试元素",
style: "display:none;",
id: "gm-test-element",
});
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
assert("gm-test-element", element.id, "元素ID应该正确");
assert("DIV", element.tagName, "元素标签应该是DIV");
console.log("返回元素:", element);
// 清理测试元素
element.parentNode.removeChild(element);
});

testSync("GM_addStyle", () => {
const styleElement = GM_addStyle(`
.gm-style-test {
color: #10b981 !important;
}
`);
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
console.log("返回样式元素:", styleElement);
// 清理测试样式
styleElement.parentNode.removeChild(styleElement);
});

(async function () {
"use strict";

// ============ 早期脚本环境检查 ============
console.log("\n%c--- 早期脚本环境检查 ---", "color: orange; font-weight: bold;");
Expand Down Expand Up @@ -107,37 +153,11 @@
console.log("脚本注入到:", node.tagName);
});

// ============ GM_addElement/GM_addStyle 测试 ============
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");

await test("GM_addElement", () => {
const element = GM_addElement("div", {
textContent: "GM_addElement测试元素",
style: "display:none;",
id: "gm-test-element",
});
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
assert("gm-test-element", element.id, "元素ID应该正确");
assert("DIV", element.tagName, "元素标签应该是DIV");
console.log("返回元素:", element);
});

await test("GM_addStyle", () => {
const styleElement = GM_addStyle(`
.gm-style-test {
color: #10b981 !important;
}
`);
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
console.log("返回样式元素:", styleElement);
});

// ============ GM_log 测试 ============
console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;");

await test("GM_log", () => {
GM_log("测试日志输出", { type: "test", value: 123 });
GM_log("测试日志输出", "info", { type: "test", value: 123 });
// GM_log本身不返回值,只要不抛出异常就算成功
assertTrue(true, "GM_log应该能正常输出");
});
Expand Down
146 changes: 82 additions & 64 deletions example/tests/early_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,91 @@
// @run-at document-start
// ==/UserScript==

(async function () {
"use strict";

console.log("%c=== 早期脚本 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");

let testResults = {
passed: 0,
failed: 0,
total: 0,
};

// 测试辅助函数(支持同步和异步)
async function test(name, fn) {
testResults.total++;
try {
await fn();
testResults.passed++;
console.log(`%c✓ ${name}`, "color: green;");
return true;
} catch (error) {
testResults.failed++;
console.error(`%c✗ ${name}`, "color: red;", error);
return false;
}
// 测试辅助函数(支持同步和异步)
async function test(name, fn) {
testResults.total++;
try {
await fn();
testResults.passed++;
console.log(`%c✓ ${name}`, "color: green;");
return true;
} catch (error) {
testResults.failed++;
console.error(`%c✗ ${name}`, "color: red;", error);
return false;
}

// assert(expected, actual, message) - 比较两个值是否相等
function assert(expected, actual, message) {
if (expected !== actual) {
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
throw new Error(error);
}
}

function testSync(name, fn) {
testResults.total++;
try {
fn();
testResults.passed++;
console.log(`%c✓ ${name}`, "color: green;");
return true;
} catch (error) {
testResults.failed++;
console.error(`%c✗ ${name}`, "color: red;", error);
return false;
}
}

// assert(expected, actual, message) - 比较两个值是否相等
function assert(expected, actual, message) {
if (expected !== actual) {
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
throw new Error(error);
}
}

// assertTrue(condition, message) - 断言条件为真
function assertTrue(condition, message) {
if (!condition) {
throw new Error(message || "断言失败: 期望条件为真");
}
// assertTrue(condition, message) - 断言条件为真
function assertTrue(condition, message) {
if (!condition) {
throw new Error(message || "断言失败: 期望条件为真");
}
}

console.log("%c=== 早期脚本 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");

let testResults = {
passed: 0,
failed: 0,
total: 0,
};

// ============ GM_addElement/GM_addStyle 测试 ============
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");

testSync("GM_addElement", () => {
const element = GM_addElement("div", {
textContent: "GM_addElement测试元素",
style: "display:none;",
id: "gm-test-element",
});
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
assert("gm-test-element", element.id, "元素ID应该正确");
assert("DIV", element.tagName, "元素标签应该是DIV");
console.log("返回元素:", element);
// 清理测试元素
element.parentNode.removeChild(element);
});

testSync("GM_addStyle", () => {
const styleElement = GM_addStyle(`
.gm-style-test {
color: #10b981 !important;
}
`);
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
console.log("返回样式元素:", styleElement);
// 清理测试样式
styleElement.parentNode.removeChild(styleElement);
});

(async function () {
"use strict";

// ============ 早期脚本环境检查 ============
console.log("\n%c--- 早期脚本环境检查 ---", "color: orange; font-weight: bold;");
Expand Down Expand Up @@ -135,37 +179,11 @@
}
});

// ============ GM_addElement/GM_addStyle 测试 ============
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");

await test("GM_addElement", () => {
const element = GM_addElement("div", {
textContent: "GM_addElement测试元素",
style: "display:none;",
id: "gm-test-element",
});
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
assert("gm-test-element", element.id, "元素ID应该正确");
assert("DIV", element.tagName, "元素标签应该是DIV");
console.log("返回元素:", element);
});

await test("GM_addStyle", () => {
const styleElement = GM_addStyle(`
.gm-style-test {
color: #10b981 !important;
}
`);
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
console.log("返回样式元素:", styleElement);
});

// ============ GM_log 测试 ============
console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;");

await test("GM_log", () => {
GM_log("测试日志输出", { type: "test", value: 123 });
GM_log("测试日志输出", "info", { type: "test", value: 123 });
// GM_log本身不返回值,只要不抛出异常就算成功
assertTrue(true, "GM_log应该能正常输出");
});
Expand Down
2 changes: 1 addition & 1 deletion example/tests/inject_content_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;");

await test("GM_log", () => {
GM_log("测试日志输出", { type: "test", value: 123 });
GM_log("测试日志输出", "info", { type: "test", value: 123 });
// GM_log本身不返回值,只要不抛出异常就算成功
assertTrue(true, "GM_log应该能正常输出");
});
Expand Down
Loading
Loading