platform-verify.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. (function attachBackgroundStep10(root, factory) {
  2. root.MultiPageBackgroundStep10 = factory();
  3. })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep10Module() {
  4. function createStep10Executor(deps = {}) {
  5. const {
  6. addLog,
  7. chrome,
  8. closeConflictingTabsForSource,
  9. completeStepFromBackground,
  10. ensureContentScriptReadyOnTab,
  11. getPanelMode,
  12. getTabId,
  13. isLocalhostOAuthCallbackUrl,
  14. isTabAlive,
  15. normalizeSub2ApiUrl,
  16. rememberSourceLastUrl,
  17. reuseOrCreateTab,
  18. sendToContentScript,
  19. sendToContentScriptResilient,
  20. shouldBypassStep9ForLocalCpa,
  21. SUB2API_STEP9_RESPONSE_TIMEOUT_MS,
  22. } = deps;
  23. async function executeStep10(state) {
  24. if (getPanelMode(state) === 'sub2api') {
  25. return executeSub2ApiStep10(state);
  26. }
  27. return executeCpaStep10(state);
  28. }
  29. async function executeCpaStep10(state) {
  30. if (state.localhostUrl && !isLocalhostOAuthCallbackUrl(state.localhostUrl)) {
  31. throw new Error('步骤 9 捕获到的 localhost OAuth 回调地址无效,请重新执行步骤 9。');
  32. }
  33. if (!state.localhostUrl) {
  34. throw new Error('缺少 localhost 回调地址,请先完成步骤 9。');
  35. }
  36. if (!state.vpsUrl) {
  37. throw new Error('尚未填写 CPA 地址,请先在侧边栏输入。');
  38. }
  39. if (shouldBypassStep9ForLocalCpa(state)) {
  40. await addLog('步骤 10:检测到本地 CPA,且当前策略为“跳过第10步”,本轮不再重复提交回调地址。', 'info');
  41. await completeStepFromBackground(10, {
  42. localhostUrl: state.localhostUrl,
  43. verifiedStatus: 'local-auto',
  44. });
  45. return;
  46. }
  47. await addLog('步骤 10:正在打开 CPA 面板...');
  48. const injectFiles = ['content/activation-utils.js', 'content/utils.js', 'content/vps-panel.js'];
  49. let tabId = await getTabId('vps-panel');
  50. const alive = tabId && await isTabAlive('vps-panel');
  51. if (!alive) {
  52. tabId = await reuseOrCreateTab('vps-panel', state.vpsUrl, {
  53. inject: injectFiles,
  54. reloadIfSameUrl: true,
  55. });
  56. } else {
  57. await closeConflictingTabsForSource('vps-panel', state.vpsUrl, { excludeTabIds: [tabId] });
  58. await chrome.tabs.update(tabId, { active: true });
  59. await rememberSourceLastUrl('vps-panel', state.vpsUrl);
  60. }
  61. await ensureContentScriptReadyOnTab('vps-panel', tabId, {
  62. inject: injectFiles,
  63. timeoutMs: 45000,
  64. retryDelayMs: 900,
  65. logMessage: '步骤 10:CPA 面板仍在加载,正在重试连接...',
  66. });
  67. await addLog('步骤 10:正在填写回调地址...');
  68. const result = await sendToContentScriptResilient('vps-panel', {
  69. type: 'EXECUTE_STEP',
  70. step: 10,
  71. source: 'background',
  72. payload: { localhostUrl: state.localhostUrl, vpsPassword: state.vpsPassword },
  73. }, {
  74. timeoutMs: 30000,
  75. retryDelayMs: 700,
  76. logMessage: '步骤 10:CPA 面板通信未就绪,正在等待页面恢复...',
  77. });
  78. if (result?.error) {
  79. throw new Error(result.error);
  80. }
  81. }
  82. async function executeSub2ApiStep10(state) {
  83. if (state.localhostUrl && !isLocalhostOAuthCallbackUrl(state.localhostUrl)) {
  84. throw new Error('步骤 9 捕获到的 localhost OAuth 回调地址无效,请重新执行步骤 9。');
  85. }
  86. if (!state.localhostUrl) {
  87. throw new Error('缺少 localhost 回调地址,请先完成步骤 9。');
  88. }
  89. if (!state.sub2apiSessionId) {
  90. throw new Error('缺少 SUB2API 会话信息,请重新执行步骤 1。');
  91. }
  92. if (!state.sub2apiEmail) {
  93. throw new Error('尚未配置 SUB2API 登录邮箱,请先在侧边栏填写。');
  94. }
  95. if (!state.sub2apiPassword) {
  96. throw new Error('尚未配置 SUB2API 登录密码,请先在侧边栏填写。');
  97. }
  98. const sub2apiUrl = normalizeSub2ApiUrl(state.sub2apiUrl);
  99. const injectFiles = ['content/utils.js', 'content/sub2api-panel.js'];
  100. await addLog('步骤 10:正在打开 SUB2API 后台...');
  101. let tabId = await getTabId('sub2api-panel');
  102. const alive = tabId && await isTabAlive('sub2api-panel');
  103. if (!alive) {
  104. tabId = await reuseOrCreateTab('sub2api-panel', sub2apiUrl, {
  105. inject: injectFiles,
  106. injectSource: 'sub2api-panel',
  107. reloadIfSameUrl: true,
  108. });
  109. } else {
  110. await closeConflictingTabsForSource('sub2api-panel', sub2apiUrl, { excludeTabIds: [tabId] });
  111. await chrome.tabs.update(tabId, { active: true });
  112. await rememberSourceLastUrl('sub2api-panel', sub2apiUrl);
  113. }
  114. await ensureContentScriptReadyOnTab('sub2api-panel', tabId, {
  115. inject: injectFiles,
  116. injectSource: 'sub2api-panel',
  117. });
  118. await addLog('步骤 10:正在向 SUB2API 提交回调并创建账号...');
  119. const result = await sendToContentScript('sub2api-panel', {
  120. type: 'EXECUTE_STEP',
  121. step: 10,
  122. source: 'background',
  123. payload: {
  124. localhostUrl: state.localhostUrl,
  125. sub2apiUrl,
  126. sub2apiEmail: state.sub2apiEmail,
  127. sub2apiPassword: state.sub2apiPassword,
  128. sub2apiGroupName: state.sub2apiGroupName,
  129. sub2apiSessionId: state.sub2apiSessionId,
  130. sub2apiOAuthState: state.sub2apiOAuthState,
  131. sub2apiGroupId: state.sub2apiGroupId,
  132. sub2apiDraftName: state.sub2apiDraftName,
  133. },
  134. }, {
  135. responseTimeoutMs: SUB2API_STEP9_RESPONSE_TIMEOUT_MS,
  136. });
  137. if (result?.error) {
  138. throw new Error(result.error);
  139. }
  140. }
  141. return {
  142. executeCpaStep10,
  143. executeStep10,
  144. executeSub2ApiStep10,
  145. };
  146. }
  147. return { createStep10Executor };
  148. });