vps-panel.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // content/vps-panel.js — Content script for VPS panel (steps 1, 9)
  2. // Injected on: http://154.26.182.181:8317/*
  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();
  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() {
  96. log('Step 9: Getting localhost URL from storage...');
  97. const state = await chrome.runtime.sendMessage({ type: 'GET_STATE' });
  98. const localhostUrl = state.localhostUrl;
  99. if (!localhostUrl) {
  100. throw new Error('No localhost URL found. Complete step 8 first.');
  101. }
  102. log('Step 9: Looking for callback URL input...');
  103. // Find the callback URL input
  104. // Actual DOM: <input class="input" placeholder="http://localhost:1455/auth/callback?code=...&state=...">
  105. let urlInput = null;
  106. try {
  107. urlInput = await waitForElement('[class*="callbackSection"] input.input', 10000);
  108. } catch {
  109. try {
  110. urlInput = await waitForElement('input[placeholder*="localhost"]', 5000);
  111. } catch {
  112. throw new Error('Could not find callback URL input on VPS panel. URL: ' + location.href);
  113. }
  114. }
  115. fillInput(urlInput, localhostUrl);
  116. log(`Step 9: Filled callback URL: ${localhostUrl.slice(0, 80)}...`);
  117. // Find and click "提交回调 URL" button
  118. let submitBtn = null;
  119. try {
  120. submitBtn = await waitForElementByText(
  121. '[class*="callbackActions"] button, [class*="callbackSection"] button',
  122. /提交/,
  123. 5000
  124. );
  125. } catch {
  126. try {
  127. submitBtn = await waitForElementByText('button.btn', /提交回调/, 5000);
  128. } catch {
  129. throw new Error('Could not find "提交回调 URL" button. URL: ' + location.href);
  130. }
  131. }
  132. simulateClick(submitBtn);
  133. log('Step 9: Clicked "提交回调 URL", waiting for authentication result...');
  134. // Wait for "认证成功!" status badge to appear
  135. try {
  136. await waitForElementByText('.status-badge, [class*="status"]', /认证成功/, 30000);
  137. log('Step 9: Authentication successful!', 'ok');
  138. } catch {
  139. // Check if there's an error message instead
  140. const statusEl = document.querySelector('.status-badge, [class*="status"]');
  141. const statusText = statusEl ? statusEl.textContent : 'unknown';
  142. if (/成功|success/i.test(statusText)) {
  143. log('Step 9: Authentication successful!', 'ok');
  144. } else {
  145. log(`Step 9: Status after submit: "${statusText}". May still be processing.`, 'warn');
  146. }
  147. }
  148. reportComplete(9);
  149. }