sidepanel.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // sidepanel/sidepanel.js — Side Panel logic
  2. const STATUS_ICONS = {
  3. pending: '\u2B1A', // ⬚
  4. running: '\u23F3', // ⏳
  5. completed: '\u2705', // ✅
  6. failed: '\u274C', // ❌
  7. };
  8. const logArea = document.getElementById('log-area');
  9. const displayOauthUrl = document.getElementById('display-oauth-url');
  10. const displayLocalhostUrl = document.getElementById('display-localhost-url');
  11. const displayStatus = document.getElementById('display-status');
  12. const inputEmail = document.getElementById('input-email');
  13. const btnReset = document.getElementById('btn-reset');
  14. // ============================================================
  15. // State Restore on load
  16. // ============================================================
  17. async function restoreState() {
  18. try {
  19. const state = await chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' });
  20. // Restore data fields
  21. if (state.oauthUrl) {
  22. displayOauthUrl.textContent = state.oauthUrl;
  23. displayOauthUrl.classList.add('has-value');
  24. }
  25. if (state.localhostUrl) {
  26. displayLocalhostUrl.textContent = state.localhostUrl;
  27. displayLocalhostUrl.classList.add('has-value');
  28. }
  29. if (state.email) {
  30. inputEmail.value = state.email;
  31. }
  32. // Restore step statuses
  33. if (state.stepStatuses) {
  34. for (const [step, status] of Object.entries(state.stepStatuses)) {
  35. updateStepUI(Number(step), status);
  36. }
  37. }
  38. // Restore logs
  39. if (state.logs) {
  40. for (const entry of state.logs) {
  41. appendLog(entry);
  42. }
  43. }
  44. updateStatusDisplay(state);
  45. } catch (err) {
  46. console.error('Failed to restore state:', err);
  47. }
  48. }
  49. // ============================================================
  50. // UI Updates
  51. // ============================================================
  52. function updateStepUI(step, status) {
  53. const statusEl = document.querySelector(`.step-status[data-step="${step}"]`);
  54. if (statusEl) statusEl.textContent = STATUS_ICONS[status] || '\u2B1A';
  55. // Interlock logic
  56. updateButtonStates();
  57. }
  58. function updateButtonStates() {
  59. // Get all current statuses from DOM
  60. const statuses = {};
  61. document.querySelectorAll('.step-status').forEach(el => {
  62. const step = Number(el.dataset.step);
  63. const icon = el.textContent;
  64. const status = Object.entries(STATUS_ICONS).find(([, v]) => v === icon)?.[0] || 'pending';
  65. statuses[step] = status;
  66. });
  67. // Find if any step is running
  68. const anyRunning = Object.values(statuses).some(s => s === 'running');
  69. for (let step = 1; step <= 9; step++) {
  70. const btn = document.querySelector(`.step-btn[data-step="${step}"]`);
  71. if (!btn) continue;
  72. if (anyRunning) {
  73. // When any step is running, disable all buttons
  74. btn.disabled = true;
  75. } else if (step === 1) {
  76. // Step 1 is always available (unless running)
  77. btn.disabled = false;
  78. } else {
  79. // Steps 2-9: enabled if previous step completed (or current step failed for retry)
  80. const prevStatus = statuses[step - 1];
  81. const currentStatus = statuses[step];
  82. btn.disabled = !(prevStatus === 'completed' || currentStatus === 'failed' || currentStatus === 'completed');
  83. }
  84. }
  85. }
  86. function updateStatusDisplay(state) {
  87. if (!state || !state.stepStatuses) return;
  88. const running = Object.entries(state.stepStatuses).find(([, s]) => s === 'running');
  89. if (running) {
  90. displayStatus.textContent = `Step ${running[0]} running...`;
  91. displayStatus.classList.add('has-value');
  92. } else {
  93. const lastCompleted = Object.entries(state.stepStatuses)
  94. .filter(([, s]) => s === 'completed')
  95. .map(([k]) => Number(k))
  96. .sort((a, b) => b - a)[0];
  97. if (lastCompleted === 9) {
  98. displayStatus.textContent = 'All steps completed!';
  99. displayStatus.classList.add('has-value');
  100. } else if (lastCompleted) {
  101. displayStatus.textContent = `Step ${lastCompleted} done. Ready for step ${lastCompleted + 1}.`;
  102. displayStatus.classList.add('has-value');
  103. } else {
  104. displayStatus.textContent = 'Waiting';
  105. displayStatus.classList.remove('has-value');
  106. }
  107. }
  108. }
  109. function appendLog(entry) {
  110. const time = new Date(entry.timestamp).toLocaleTimeString('en-US', { hour12: false });
  111. const levelLabel = entry.level.toUpperCase().padEnd(5);
  112. const line = document.createElement('div');
  113. line.className = `log-${entry.level}`;
  114. line.textContent = `${time} [${levelLabel}] ${entry.message}`;
  115. logArea.appendChild(line);
  116. logArea.scrollTop = logArea.scrollHeight;
  117. }
  118. // ============================================================
  119. // Button Handlers
  120. // ============================================================
  121. document.querySelectorAll('.step-btn').forEach(btn => {
  122. btn.addEventListener('click', async () => {
  123. const step = Number(btn.dataset.step);
  124. // Save email if step 3 and email input has value
  125. if (step === 3) {
  126. const email = inputEmail.value.trim();
  127. if (!email) {
  128. appendLog({ message: 'Please paste email address first', level: 'error', timestamp: Date.now() });
  129. return;
  130. }
  131. await chrome.runtime.sendMessage({
  132. type: 'EXECUTE_STEP',
  133. source: 'sidepanel',
  134. payload: { step, email },
  135. });
  136. } else {
  137. await chrome.runtime.sendMessage({
  138. type: 'EXECUTE_STEP',
  139. source: 'sidepanel',
  140. payload: { step },
  141. });
  142. }
  143. });
  144. });
  145. // Reset button
  146. btnReset.addEventListener('click', async () => {
  147. if (confirm('Reset all steps and data?')) {
  148. await chrome.runtime.sendMessage({ type: 'RESET', source: 'sidepanel' });
  149. // Clear UI
  150. displayOauthUrl.textContent = 'Not obtained';
  151. displayOauthUrl.classList.remove('has-value');
  152. displayLocalhostUrl.textContent = 'Not captured';
  153. displayLocalhostUrl.classList.remove('has-value');
  154. inputEmail.value = '';
  155. displayStatus.textContent = 'Waiting';
  156. displayStatus.classList.remove('has-value');
  157. logArea.innerHTML = '';
  158. document.querySelectorAll('.step-status').forEach(el => el.textContent = '\u2B1A');
  159. updateButtonStates();
  160. }
  161. });
  162. // Save email when user types/pastes
  163. inputEmail.addEventListener('change', async () => {
  164. const email = inputEmail.value.trim();
  165. if (email) {
  166. await chrome.runtime.sendMessage({
  167. type: 'SAVE_EMAIL',
  168. source: 'sidepanel',
  169. payload: { email },
  170. });
  171. }
  172. });
  173. // ============================================================
  174. // Listen for Background broadcasts
  175. // ============================================================
  176. chrome.runtime.onMessage.addListener((message) => {
  177. switch (message.type) {
  178. case 'LOG_ENTRY':
  179. appendLog(message.payload);
  180. break;
  181. case 'STEP_STATUS_CHANGED': {
  182. const { step, status } = message.payload;
  183. updateStepUI(step, status);
  184. // Update status display
  185. chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' }).then(updateStatusDisplay);
  186. break;
  187. }
  188. case 'DATA_UPDATED': {
  189. if (message.payload.oauthUrl) {
  190. displayOauthUrl.textContent = message.payload.oauthUrl;
  191. displayOauthUrl.classList.add('has-value');
  192. }
  193. if (message.payload.localhostUrl) {
  194. displayLocalhostUrl.textContent = message.payload.localhostUrl;
  195. displayLocalhostUrl.classList.add('has-value');
  196. }
  197. break;
  198. }
  199. }
  200. });
  201. // ============================================================
  202. // Init
  203. // ============================================================
  204. restoreState().then(() => {
  205. updateButtonStates();
  206. });