vps-panel.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // content/vps-panel.js — Content script for VPS panel (steps 1, 9)
  2. // Injected on: VPS panel (user-configured URL)
  3. //
  4. // Actual DOM structure (after login click):
  5. // <div class="card">
  6. // <div class="card-header">
  7. // <span class="OAuthPage-module__cardTitle___yFaP0">Codex OAuth</span>
  8. // <button class="btn btn-primary"><span>登录</span></button>
  9. // </div>
  10. // <div class="OAuthPage-module__cardContent___1sXLA">
  11. // <div class="OAuthPage-module__authUrlBox___Iu1d4">
  12. // <div class="OAuthPage-module__authUrlLabel___mYFJB">授权链接:</div>
  13. // <div class="OAuthPage-module__authUrlValue___axvUJ">https://auth.openai.com/...</div>
  14. // <div class="OAuthPage-module__authUrlActions___venPj">
  15. // <button class="btn btn-secondary btn-sm"><span>复制链接</span></button>
  16. // <button class="btn btn-secondary btn-sm"><span>打开链接</span></button>
  17. // </div>
  18. // </div>
  19. // <div class="OAuthPage-module__callbackSection___8kA31">
  20. // <input class="input" placeholder="http://localhost:1455/auth/callback?code=...&state=...">
  21. // <button class="btn btn-secondary btn-sm"><span>提交回调 URL</span></button>
  22. // </div>
  23. // </div>
  24. // </div>
  25. console.log('[MultiPage:vps-panel] Content script loaded on', location.href);
  26. // Listen for commands from Background
  27. chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  28. if (message.type === 'EXECUTE_STEP') {
  29. handleStep(message.step, message.payload).then(() => {
  30. sendResponse({ ok: true });
  31. }).catch(err => {
  32. reportError(message.step, err.message);
  33. sendResponse({ error: err.message });
  34. });
  35. return true;
  36. }
  37. });
  38. async function handleStep(step, payload) {
  39. switch (step) {
  40. case 1: return await step1_getOAuthLink();
  41. case 9: return await step9_vpsVerify(payload);
  42. default:
  43. throw new Error(`vps-panel.js does not handle step ${step}`);
  44. }
  45. }
  46. // ============================================================
  47. // Step 1: Get OAuth Link
  48. // ============================================================
  49. async function step1_getOAuthLink() {
  50. log('Step 1: Waiting for VPS panel to load (auto-login may take a moment)...');
  51. // The page may start at #/login and auto-redirect to #/oauth.
  52. // Wait for the Codex OAuth card to appear (up to 30s for auto-login + redirect).
  53. let loginBtn = null;
  54. try {
  55. // Wait for any card-header containing "Codex" to appear
  56. const header = await waitForElementByText('.card-header', /codex/i, 30000);
  57. loginBtn = header.querySelector('button.btn.btn-primary, button.btn');
  58. log('Step 1: Found Codex OAuth card');
  59. } catch {
  60. throw new Error(
  61. 'Codex OAuth card did not appear after 30s. Page may still be loading or not logged in. ' +
  62. 'Current URL: ' + location.href
  63. );
  64. }
  65. if (!loginBtn) {
  66. throw new Error('Found Codex OAuth card but no login button inside it. URL: ' + location.href);
  67. }
  68. // Check if button is disabled (already clicked / loading)
  69. if (loginBtn.disabled) {
  70. log('Step 1: Login button is disabled (already loading), waiting for auth URL...');
  71. } else {
  72. simulateClick(loginBtn);
  73. log('Step 1: Clicked login button, waiting for auth URL...');
  74. }
  75. // Wait for the auth URL to appear in the specific div
  76. let authUrlEl = null;
  77. try {
  78. authUrlEl = await waitForElement('[class*="authUrlValue"]', 15000);
  79. } catch {
  80. throw new Error(
  81. 'Auth URL did not appear after clicking login. ' +
  82. 'Check if VPS panel is logged in and Codex service is running. URL: ' + location.href
  83. );
  84. }
  85. const oauthUrl = (authUrlEl.textContent || '').trim();
  86. if (!oauthUrl || !oauthUrl.startsWith('http')) {
  87. throw new Error(`Invalid OAuth URL found: "${oauthUrl.slice(0, 50)}". Expected URL starting with http.`);
  88. }
  89. log(`Step 1: OAuth URL obtained: ${oauthUrl.slice(0, 80)}...`, 'ok');
  90. reportComplete(1, { oauthUrl });
  91. }
  92. // ============================================================
  93. // Step 9: VPS Verify — paste localhost URL and submit
  94. // ============================================================
  95. async function step9_vpsVerify(payload) {
  96. // Get localhostUrl from payload (passed directly by background) or fallback to state
  97. let localhostUrl = payload?.localhostUrl;
  98. if (!localhostUrl) {
  99. log('Step 9: localhostUrl not in payload, fetching from state...');
  100. const state = await chrome.runtime.sendMessage({ type: 'GET_STATE' });
  101. localhostUrl = state.localhostUrl;
  102. }
  103. if (!localhostUrl) {
  104. throw new Error('No localhost URL found. Complete step 8 first.');
  105. }
  106. log(`Step 9: Got localhostUrl: ${localhostUrl.slice(0, 60)}...`);
  107. log('Step 9: Looking for callback URL input...');
  108. // Find the callback URL input
  109. // Actual DOM: <input class="input" placeholder="http://localhost:1455/auth/callback?code=...&state=...">
  110. let urlInput = null;
  111. try {
  112. urlInput = await waitForElement('[class*="callbackSection"] input.input', 10000);
  113. } catch {
  114. try {
  115. urlInput = await waitForElement('input[placeholder*="localhost"]', 5000);
  116. } catch {
  117. throw new Error('Could not find callback URL input on VPS panel. URL: ' + location.href);
  118. }
  119. }
  120. fillInput(urlInput, localhostUrl);
  121. log(`Step 9: Filled callback URL: ${localhostUrl.slice(0, 80)}...`);
  122. // Find and click "提交回调 URL" button
  123. let submitBtn = null;
  124. try {
  125. submitBtn = await waitForElementByText(
  126. '[class*="callbackActions"] button, [class*="callbackSection"] button',
  127. /提交/,
  128. 5000
  129. );
  130. } catch {
  131. try {
  132. submitBtn = await waitForElementByText('button.btn', /提交回调/, 5000);
  133. } catch {
  134. throw new Error('Could not find "提交回调 URL" button. URL: ' + location.href);
  135. }
  136. }
  137. simulateClick(submitBtn);
  138. log('Step 9: Clicked "提交回调 URL", waiting for authentication result...');
  139. // Wait for "认证成功!" status badge to appear
  140. try {
  141. await waitForElementByText('.status-badge, [class*="status"]', /认证成功/, 30000);
  142. log('Step 9: Authentication successful!', 'ok');
  143. } catch {
  144. // Check if there's an error message instead
  145. const statusEl = document.querySelector('.status-badge, [class*="status"]');
  146. const statusText = statusEl ? statusEl.textContent : 'unknown';
  147. if (/成功|success/i.test(statusText)) {
  148. log('Step 9: Authentication successful!', 'ok');
  149. } else {
  150. log(`Step 9: Status after submit: "${statusText}". May still be processing.`, 'warn');
  151. }
  152. }
  153. reportComplete(9);
  154. }