feat: 增强 Plus Checkout 内容脚本,支持多次注入同一页面的功能测试

This commit is contained in:
QLHazyCoder
2026-04-26 16:28:24 +08:00
parent 344158e465
commit 818791f395
3 changed files with 120 additions and 7 deletions
+34
View File
@@ -1,9 +1,43 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const source = fs.readFileSync('content/plus-checkout.js', 'utf8');
test('plus checkout content script can be injected repeatedly on the same page', () => {
const attrs = new Map();
const context = {
console: { log() {}, warn() {}, error() {}, info() {} },
location: { href: 'https://chatgpt.com/' },
window: {},
document: {
documentElement: {
getAttribute(name) {
return attrs.get(name) || null;
},
setAttribute(name, value) {
attrs.set(name, String(value));
},
},
},
chrome: {
runtime: {
onMessage: {
addListener() {},
},
},
},
};
context.window = context;
vm.createContext(context);
vm.runInContext(source, context);
vm.runInContext(source, context);
assert.equal(context.__MULTIPAGE_PLUS_CHECKOUT_READY__, true);
});
function extractFunction(name) {
const plainStart = source.indexOf(`function ${name}(`);
const asyncStart = source.indexOf(`async function ${name}(`);