Merge branch 'dev' of https://github.com/QLHazyCoder/codex-oauth-automation-extension into dev
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
|
||||
const source = fs.readFileSync('content/signup-page.js', 'utf8');
|
||||
|
||||
function extractFunction(name) {
|
||||
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||
const start = markers
|
||||
.map((marker) => source.indexOf(marker))
|
||||
.find((index) => index >= 0);
|
||||
if (start < 0) {
|
||||
throw new Error(`missing function ${name}`);
|
||||
}
|
||||
|
||||
let parenDepth = 0;
|
||||
let signatureEnded = false;
|
||||
let braceStart = -1;
|
||||
for (let i = start; i < source.length; i += 1) {
|
||||
const ch = source[i];
|
||||
if (ch === '(') {
|
||||
parenDepth += 1;
|
||||
} else if (ch === ')') {
|
||||
parenDepth -= 1;
|
||||
if (parenDepth === 0) {
|
||||
signatureEnded = true;
|
||||
}
|
||||
} else if (ch === '{' && signatureEnded) {
|
||||
braceStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (braceStart < 0) {
|
||||
throw new Error(`missing body for function ${name}`);
|
||||
}
|
||||
|
||||
let depth = 0;
|
||||
let end = braceStart;
|
||||
for (; end < source.length; end += 1) {
|
||||
const ch = source[end];
|
||||
if (ch === '{') depth += 1;
|
||||
if (ch === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) {
|
||||
end += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
const bundle = [
|
||||
extractFunction('getPageTextSnapshot'),
|
||||
extractFunction('findChatgptSkipButton'),
|
||||
extractFunction('waitForChatgptSkipButton'),
|
||||
extractFunction('isChatgptOnboardingPage'),
|
||||
extractFunction('isChatgptUrl'),
|
||||
extractFunction('hasVisibleElementMatchingSelector'),
|
||||
extractFunction('isChatgptAuthenticatedHomePage'),
|
||||
extractFunction('waitForChatgptPostSignupState'),
|
||||
extractFunction('skipChatgptOnboarding'),
|
||||
].join('\n');
|
||||
|
||||
const api = new Function(`
|
||||
const CHATGPT_ONBOARDING_TEXT_PATTERN = /welcome\\s+to\\s+chatgpt|what\\s+should\\s+chatgpt\\s+call\\s+you|how\\s+do\\s+you\\s+want\\s+chatgpt\\s+to\\s+respond|tell\\s+chatgpt\\s+what\\s+traits|personal(?:ize|ise)\\s+your\\s+experience|介绍一下你自己|ChatGPT 应该如何称呼你|你希望 ChatGPT 如何回应/i;
|
||||
const CHATGPT_HOME_TEXT_PATTERN = /new\\s+chat|temporary\\s+chat|message\\s+chatgpt|send\\s+a\\s+message|chatgpt\\s+can\\s+make\\s+mistakes|新建聊天|临时聊天|给\\s*ChatGPT\\s*发消息|ChatGPT\\s*可能会犯错/i;
|
||||
|
||||
let buttonList = [];
|
||||
let selectorMap = {};
|
||||
let pageText = '';
|
||||
let clickedButtons = [];
|
||||
let logs = [];
|
||||
const location = { href: 'https://chatgpt.com/' };
|
||||
const document = {
|
||||
body: { innerText: '', textContent: '' },
|
||||
querySelectorAll(selector) {
|
||||
if (selector === 'button') {
|
||||
return buttonList;
|
||||
}
|
||||
return selectorMap[selector] || [];
|
||||
},
|
||||
};
|
||||
|
||||
function isVisibleElement(el) {
|
||||
return Boolean(el) && !el.hidden;
|
||||
}
|
||||
|
||||
function throwIfStopped() {}
|
||||
async function sleep() {}
|
||||
async function humanPause() {}
|
||||
function log(message, level = 'info') {
|
||||
logs.push({ message, level });
|
||||
}
|
||||
function simulateClick(button) {
|
||||
clickedButtons.push(button.textContent || button.id || 'button');
|
||||
button.hidden = true;
|
||||
}
|
||||
|
||||
${bundle}
|
||||
|
||||
return {
|
||||
setPage({ href = 'https://chatgpt.com/', text = '', buttons = [], selectors = {} }) {
|
||||
location.href = href;
|
||||
pageText = text;
|
||||
buttonList = buttons.map((button, index) => ({
|
||||
id: button.id || \`button-\${index}\`,
|
||||
textContent: button.textContent || '',
|
||||
className: button.className || '',
|
||||
hidden: Boolean(button.hidden),
|
||||
}));
|
||||
selectorMap = {};
|
||||
for (const [selector, count] of Object.entries(selectors)) {
|
||||
selectorMap[selector] = Array.from({ length: count }, (_, index) => ({ id: \`\${selector}-\${index}\`, hidden: false }));
|
||||
}
|
||||
document.body.innerText = pageText;
|
||||
document.body.textContent = pageText;
|
||||
clickedButtons = [];
|
||||
logs = [];
|
||||
},
|
||||
isChatgptOnboardingPage() {
|
||||
return isChatgptOnboardingPage();
|
||||
},
|
||||
isChatgptAuthenticatedHomePage() {
|
||||
return isChatgptAuthenticatedHomePage();
|
||||
},
|
||||
async skipChatgptOnboarding() {
|
||||
return skipChatgptOnboarding();
|
||||
},
|
||||
snapshot() {
|
||||
return { clickedButtons, logs };
|
||||
},
|
||||
};
|
||||
`)();
|
||||
|
||||
(async () => {
|
||||
api.setPage({
|
||||
href: 'https://chatgpt.com/',
|
||||
text: 'New chat ChatGPT can make mistakes',
|
||||
selectors: {
|
||||
'textarea[placeholder*="Message" i]': 1,
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(api.isChatgptOnboardingPage(), false, '已登录主页不应仅因 chatgpt.com URL 被误判为 onboarding');
|
||||
assert.strictEqual(api.isChatgptAuthenticatedHomePage(), true, '主页特征存在时应识别为已登录 ChatGPT 页面');
|
||||
|
||||
let result = await api.skipChatgptOnboarding();
|
||||
let snapshot = api.snapshot();
|
||||
assert.deepStrictEqual(result, { success: true, alreadyCompleted: true }, '无 Skip 按钮但已进入主页时应按成功处理');
|
||||
assert.deepStrictEqual(snapshot.clickedButtons, [], '已登录主页场景不应再误点按钮');
|
||||
|
||||
api.setPage({
|
||||
href: 'https://chatgpt.com/',
|
||||
text: 'Welcome to ChatGPT',
|
||||
buttons: [
|
||||
{ textContent: 'Skip', className: 'btn-ghost' },
|
||||
{ textContent: 'Skip', className: 'btn-ghost' },
|
||||
],
|
||||
});
|
||||
|
||||
assert.strictEqual(api.isChatgptOnboardingPage(), true, '存在 Skip 按钮时应识别为 onboarding');
|
||||
result = await api.skipChatgptOnboarding();
|
||||
snapshot = api.snapshot();
|
||||
assert.deepStrictEqual(result, { success: true }, '真实 onboarding 仍应继续执行跳过逻辑');
|
||||
assert.deepStrictEqual(snapshot.clickedButtons, ['Skip', 'Skip'], '双 Skip onboarding 应依次点击两个按钮');
|
||||
|
||||
console.log('step5 chatgpt onboarding tests passed');
|
||||
})().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
|
||||
const source = fs.readFileSync('background.js', 'utf8');
|
||||
|
||||
function extractFunction(name) {
|
||||
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||
const start = markers
|
||||
.map((marker) => source.indexOf(marker))
|
||||
.find((index) => index >= 0);
|
||||
if (start < 0) {
|
||||
throw new Error(`missing function ${name}`);
|
||||
}
|
||||
|
||||
let parenDepth = 0;
|
||||
let signatureEnded = false;
|
||||
let braceStart = -1;
|
||||
for (let i = start; i < source.length; i += 1) {
|
||||
const ch = source[i];
|
||||
if (ch === '(') {
|
||||
parenDepth += 1;
|
||||
} else if (ch === ')') {
|
||||
parenDepth -= 1;
|
||||
if (parenDepth === 0) {
|
||||
signatureEnded = true;
|
||||
}
|
||||
} else if (ch === '{' && signatureEnded) {
|
||||
braceStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (braceStart < 0) {
|
||||
throw new Error(`missing body for function ${name}`);
|
||||
}
|
||||
|
||||
let depth = 0;
|
||||
let end = braceStart;
|
||||
for (; end < source.length; end += 1) {
|
||||
const ch = source[end];
|
||||
if (ch === '{') depth += 1;
|
||||
if (ch === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) {
|
||||
end += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
const bundle = [
|
||||
extractFunction('waitForStep5ChatgptRedirect'),
|
||||
].join('\n');
|
||||
|
||||
const api = new Function(`
|
||||
let waitArgs = null;
|
||||
let waitResult = null;
|
||||
let currentTab = null;
|
||||
|
||||
const chrome = {
|
||||
tabs: {
|
||||
async get() {
|
||||
return currentTab;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async function waitForTabUrlMatch(tabId, matcher, options = {}) {
|
||||
waitArgs = { tabId, matcher, options };
|
||||
return waitResult;
|
||||
}
|
||||
|
||||
${bundle}
|
||||
|
||||
return {
|
||||
async run(tabId, timeoutMs) {
|
||||
return waitForStep5ChatgptRedirect(tabId, timeoutMs);
|
||||
},
|
||||
setWaitResult(value) {
|
||||
waitResult = value;
|
||||
},
|
||||
setCurrentTab(value) {
|
||||
currentTab = value;
|
||||
},
|
||||
snapshot() {
|
||||
return { waitArgs };
|
||||
},
|
||||
};
|
||||
`)();
|
||||
|
||||
(async () => {
|
||||
const redirected = { id: 86, url: 'https://chatgpt.com/' };
|
||||
api.setWaitResult(redirected);
|
||||
api.setCurrentTab({ id: 86, url: 'https://auth.openai.com/' });
|
||||
|
||||
let result = await api.run(86, 22000);
|
||||
let snapshot = api.snapshot();
|
||||
assert.deepStrictEqual(result, redirected, '等待命中 chatgpt.com 时应直接返回匹配到的标签页');
|
||||
assert.strictEqual(snapshot.waitArgs.tabId, 86, '应使用 signup-page 当前标签页等待跳转');
|
||||
assert.strictEqual(snapshot.waitArgs.options.timeoutMs, 22000, '应透传等待超时时间');
|
||||
assert.strictEqual(snapshot.waitArgs.options.retryDelayMs, 300, '应使用较短轮询间隔覆盖 URL 更新 race');
|
||||
assert.strictEqual(snapshot.waitArgs.matcher('https://chatgpt.com/?model=gpt-5'), true, 'matcher 应接受 chatgpt.com');
|
||||
assert.strictEqual(snapshot.waitArgs.matcher('https://auth.openai.com/u/signup'), false, 'matcher 不应把旧认证页误判为成功');
|
||||
|
||||
api.setWaitResult(null);
|
||||
api.setCurrentTab({ id: 86, url: 'https://chatgpt.com/?temporary-chat=true' });
|
||||
result = await api.run(86, 15000);
|
||||
assert.deepStrictEqual(result, { id: 86, url: 'https://chatgpt.com/?temporary-chat=true' }, '等待超时后仍应回读当前标签页 URL 兜底');
|
||||
|
||||
api.setCurrentTab({ id: 86, url: 'https://auth.openai.com/u/signup' });
|
||||
result = await api.run(86, 15000);
|
||||
assert.strictEqual(result, null, '仍停留旧域名时不应误判为跳转成功');
|
||||
|
||||
result = await api.run(null, 15000);
|
||||
assert.strictEqual(result, null, '无有效标签页时应直接返回 null');
|
||||
|
||||
console.log('step5 chatgpt redirect race tests passed');
|
||||
})().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user