Use direct SUB2API admin API flow

This commit is contained in:
QLHazyCoder
2026-05-14 08:03:55 +08:00
parent 023e601dcc
commit a41997325d
9 changed files with 989 additions and 236 deletions
+24 -46
View File
@@ -19,6 +19,25 @@
SUB2API_STEP1_RESPONSE_TIMEOUT_MS,
} = deps;
let sub2ApiApi = null;
function getSub2ApiApi() {
if (sub2ApiApi) {
return sub2ApiApi;
}
const factory = deps.createSub2ApiApi
|| self.MultiPageBackgroundSub2ApiApi?.createSub2ApiApi;
if (typeof factory !== 'function') {
throw new Error('SUB2API 直连接口模块未加载,无法生成 OAuth 链接。');
}
sub2ApiApi = factory({
addLog,
normalizeSub2ApiUrl,
DEFAULT_SUB2API_GROUP_NAME,
});
return sub2ApiApi;
}
function normalizeAdminKey(value = '') {
return String(value || '').trim();
}
@@ -250,7 +269,6 @@
async function requestSub2ApiOAuthUrl(state, options = {}) {
const { logLabel = 'OAuth 刷新' } = options;
const sub2apiUrl = normalizeSub2ApiUrl(state.sub2apiUrl);
const groupName = (state.sub2apiGroupName || DEFAULT_SUB2API_GROUP_NAME).trim() || DEFAULT_SUB2API_GROUP_NAME;
if (!sub2apiUrl) {
throw new Error('SUB2API URL is not configured. Please fill it in the side panel first.');
@@ -262,54 +280,14 @@
throw new Error('尚未配置 SUB2API 登录密码,请先在侧边栏填写。');
}
await addLog(`${logLabel}:正在打开 SUB2API 后台...`);
const injectFiles = ['content/utils.js', 'content/sub2api-panel.js'];
await closeConflictingTabsForSource('sub2api-panel', sub2apiUrl);
const tab = typeof createAutomationTab === 'function'
? await createAutomationTab({ url: sub2apiUrl, active: true })
: await chrome.tabs.create({ url: sub2apiUrl, active: true });
const tabId = tab.id;
await rememberSourceLastUrl('sub2api-panel', sub2apiUrl);
await addLog(`${logLabel}:SUB2API 页面已打开,正在等待页面进入目标地址...`);
const matchedTab = await waitForTabUrlFamily('sub2api-panel', tabId, sub2apiUrl, {
timeoutMs: 15000,
retryDelayMs: 400,
});
if (!matchedTab) {
await addLog(`${logLabel}:SUB2API 页面尚未稳定,继续尝试连接内容脚本...`, 'warn');
}
await ensureContentScriptReadyOnTab('sub2api-panel', tabId, {
inject: injectFiles,
injectSource: 'sub2api-panel',
timeoutMs: 45000,
retryDelayMs: 900,
logMessage: `${logLabel}:SUB2API 页面仍在加载,正在重试连接内容脚本...`,
});
const result = await sendToContentScript('sub2api-panel', {
type: 'REQUEST_OAUTH_URL',
source: 'background',
payload: {
const api = getSub2ApiApi();
return api.generateOpenAiAuthUrl({
...state,
sub2apiUrl,
sub2apiEmail: state.sub2apiEmail,
sub2apiPassword: state.sub2apiPassword,
sub2apiGroupName: groupName,
sub2apiDefaultProxyName: state.sub2apiDefaultProxyName,
sub2apiAccountPriority: state.sub2apiAccountPriority,
logStep: 7,
},
}, {
responseTimeoutMs: SUB2API_STEP1_RESPONSE_TIMEOUT_MS,
logLabel,
timeoutMs: SUB2API_STEP1_RESPONSE_TIMEOUT_MS,
});
if (result?.error) {
throw new Error(result.error);
}
return result || {};
}
return {