sidepanel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // sidepanel/sidepanel.js — Side Panel logic
  2. const STATUS_ICONS = {
  3. pending: '',
  4. running: '',
  5. completed: '\u2713', // ✓
  6. failed: '\u2717', // ✗
  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 statusBar = document.getElementById('status-bar');
  13. const inputEmail = document.getElementById('input-email');
  14. const btnReset = document.getElementById('btn-reset');
  15. const stepsProgress = document.getElementById('steps-progress');
  16. const btnAutoRun = document.getElementById('btn-auto-run');
  17. const btnAutoContinue = document.getElementById('btn-auto-continue');
  18. const autoContinueBar = document.getElementById('auto-continue-bar');
  19. const btnClearLog = document.getElementById('btn-clear-log');
  20. // ============================================================
  21. // State Restore on load
  22. // ============================================================
  23. async function restoreState() {
  24. try {
  25. const state = await chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' });
  26. if (state.oauthUrl) {
  27. displayOauthUrl.textContent = state.oauthUrl;
  28. displayOauthUrl.classList.add('has-value');
  29. }
  30. if (state.localhostUrl) {
  31. displayLocalhostUrl.textContent = state.localhostUrl;
  32. displayLocalhostUrl.classList.add('has-value');
  33. }
  34. if (state.email) {
  35. inputEmail.value = state.email;
  36. }
  37. if (state.stepStatuses) {
  38. for (const [step, status] of Object.entries(state.stepStatuses)) {
  39. updateStepUI(Number(step), status);
  40. }
  41. }
  42. if (state.logs) {
  43. for (const entry of state.logs) {
  44. appendLog(entry);
  45. }
  46. }
  47. updateStatusDisplay(state);
  48. updateProgressCounter();
  49. } catch (err) {
  50. console.error('Failed to restore state:', err);
  51. }
  52. }
  53. // ============================================================
  54. // UI Updates
  55. // ============================================================
  56. function updateStepUI(step, status) {
  57. const statusEl = document.querySelector(`.step-status[data-step="${step}"]`);
  58. const row = document.querySelector(`.step-row[data-step="${step}"]`);
  59. const indicator = document.querySelector(`.step-indicator[data-step="${step}"]`);
  60. if (statusEl) statusEl.textContent = STATUS_ICONS[status] || '';
  61. if (row) {
  62. row.className = `step-row ${status}`;
  63. }
  64. updateButtonStates();
  65. updateProgressCounter();
  66. }
  67. function updateProgressCounter() {
  68. let completed = 0;
  69. document.querySelectorAll('.step-row').forEach(row => {
  70. if (row.classList.contains('completed')) completed++;
  71. });
  72. stepsProgress.textContent = `${completed} / 9`;
  73. }
  74. function updateButtonStates() {
  75. const statuses = {};
  76. document.querySelectorAll('.step-row').forEach(row => {
  77. const step = Number(row.dataset.step);
  78. if (row.classList.contains('completed')) statuses[step] = 'completed';
  79. else if (row.classList.contains('running')) statuses[step] = 'running';
  80. else if (row.classList.contains('failed')) statuses[step] = 'failed';
  81. else statuses[step] = 'pending';
  82. });
  83. const anyRunning = Object.values(statuses).some(s => s === 'running');
  84. for (let step = 1; step <= 9; step++) {
  85. const btn = document.querySelector(`.step-btn[data-step="${step}"]`);
  86. if (!btn) continue;
  87. if (anyRunning) {
  88. btn.disabled = true;
  89. } else if (step === 1) {
  90. btn.disabled = false;
  91. } else {
  92. const prevStatus = statuses[step - 1];
  93. const currentStatus = statuses[step];
  94. btn.disabled = !(prevStatus === 'completed' || currentStatus === 'failed' || currentStatus === 'completed');
  95. }
  96. }
  97. }
  98. function updateStatusDisplay(state) {
  99. if (!state || !state.stepStatuses) return;
  100. statusBar.className = 'status-bar';
  101. const running = Object.entries(state.stepStatuses).find(([, s]) => s === 'running');
  102. if (running) {
  103. displayStatus.textContent = `Step ${running[0]} running...`;
  104. statusBar.classList.add('running');
  105. return;
  106. }
  107. const failed = Object.entries(state.stepStatuses).find(([, s]) => s === 'failed');
  108. if (failed) {
  109. displayStatus.textContent = `Step ${failed[0]} failed`;
  110. statusBar.classList.add('failed');
  111. return;
  112. }
  113. const lastCompleted = Object.entries(state.stepStatuses)
  114. .filter(([, s]) => s === 'completed')
  115. .map(([k]) => Number(k))
  116. .sort((a, b) => b - a)[0];
  117. if (lastCompleted === 9) {
  118. displayStatus.textContent = 'All steps completed!';
  119. statusBar.classList.add('completed');
  120. } else if (lastCompleted) {
  121. displayStatus.textContent = `Step ${lastCompleted} done`;
  122. } else {
  123. displayStatus.textContent = 'Ready';
  124. }
  125. }
  126. function appendLog(entry) {
  127. const time = new Date(entry.timestamp).toLocaleTimeString('en-US', { hour12: false });
  128. const levelLabel = entry.level.toUpperCase();
  129. const line = document.createElement('div');
  130. line.className = `log-line log-${entry.level}`;
  131. const stepMatch = entry.message.match(/Step (\d)/);
  132. const stepNum = stepMatch ? stepMatch[1] : null;
  133. let html = `<span class="log-time">${time}</span> `;
  134. html += `<span class="log-level log-level-${entry.level}">${levelLabel}</span> `;
  135. if (stepNum) {
  136. html += `<span class="log-step-tag step-${stepNum}">S${stepNum}</span>`;
  137. }
  138. html += `<span class="log-msg">${escapeHtml(entry.message)}</span>`;
  139. line.innerHTML = html;
  140. logArea.appendChild(line);
  141. logArea.scrollTop = logArea.scrollHeight;
  142. }
  143. function escapeHtml(text) {
  144. const div = document.createElement('div');
  145. div.textContent = text;
  146. return div.innerHTML;
  147. }
  148. // ============================================================
  149. // Button Handlers
  150. // ============================================================
  151. document.querySelectorAll('.step-btn').forEach(btn => {
  152. btn.addEventListener('click', async () => {
  153. const step = Number(btn.dataset.step);
  154. if (step === 3) {
  155. const email = inputEmail.value.trim();
  156. if (!email) {
  157. appendLog({ message: 'Please paste email address first', level: 'error', timestamp: Date.now() });
  158. return;
  159. }
  160. await chrome.runtime.sendMessage({ type: 'EXECUTE_STEP', source: 'sidepanel', payload: { step, email } });
  161. } else {
  162. await chrome.runtime.sendMessage({ type: 'EXECUTE_STEP', source: 'sidepanel', payload: { step } });
  163. }
  164. });
  165. });
  166. // Auto Run
  167. btnAutoRun.addEventListener('click', async () => {
  168. btnAutoRun.disabled = true;
  169. btnAutoRun.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></svg> Running...';
  170. await chrome.runtime.sendMessage({ type: 'AUTO_RUN', source: 'sidepanel' });
  171. });
  172. btnAutoContinue.addEventListener('click', async () => {
  173. const email = inputEmail.value.trim();
  174. if (!email) {
  175. appendLog({ message: 'Please paste DuckDuckGo email first!', level: 'error', timestamp: Date.now() });
  176. return;
  177. }
  178. autoContinueBar.style.display = 'none';
  179. await chrome.runtime.sendMessage({ type: 'RESUME_AUTO_RUN', source: 'sidepanel', payload: { email } });
  180. });
  181. // Reset
  182. btnReset.addEventListener('click', async () => {
  183. if (confirm('Reset all steps and data?')) {
  184. await chrome.runtime.sendMessage({ type: 'RESET', source: 'sidepanel' });
  185. displayOauthUrl.textContent = 'Waiting...';
  186. displayOauthUrl.classList.remove('has-value');
  187. displayLocalhostUrl.textContent = 'Waiting...';
  188. displayLocalhostUrl.classList.remove('has-value');
  189. inputEmail.value = '';
  190. displayStatus.textContent = 'Ready';
  191. statusBar.className = 'status-bar';
  192. logArea.innerHTML = '';
  193. document.querySelectorAll('.step-row').forEach(row => row.className = 'step-row');
  194. document.querySelectorAll('.step-status').forEach(el => el.textContent = '');
  195. btnAutoRun.disabled = false;
  196. btnAutoRun.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"/></svg> Auto';
  197. autoContinueBar.style.display = 'none';
  198. updateButtonStates();
  199. updateProgressCounter();
  200. }
  201. });
  202. // Clear log
  203. btnClearLog.addEventListener('click', () => {
  204. logArea.innerHTML = '';
  205. });
  206. // Save email on change
  207. inputEmail.addEventListener('change', async () => {
  208. const email = inputEmail.value.trim();
  209. if (email) {
  210. await chrome.runtime.sendMessage({ type: 'SAVE_EMAIL', source: 'sidepanel', payload: { email } });
  211. }
  212. });
  213. // ============================================================
  214. // Listen for Background broadcasts
  215. // ============================================================
  216. chrome.runtime.onMessage.addListener((message) => {
  217. switch (message.type) {
  218. case 'LOG_ENTRY':
  219. appendLog(message.payload);
  220. break;
  221. case 'STEP_STATUS_CHANGED': {
  222. const { step, status } = message.payload;
  223. updateStepUI(step, status);
  224. chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' }).then(updateStatusDisplay);
  225. if (status === 'completed') {
  226. chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' }).then(state => {
  227. if (state.oauthUrl) {
  228. displayOauthUrl.textContent = state.oauthUrl;
  229. displayOauthUrl.classList.add('has-value');
  230. }
  231. if (state.localhostUrl) {
  232. displayLocalhostUrl.textContent = state.localhostUrl;
  233. displayLocalhostUrl.classList.add('has-value');
  234. }
  235. });
  236. }
  237. break;
  238. }
  239. case 'DATA_UPDATED': {
  240. if (message.payload.oauthUrl) {
  241. displayOauthUrl.textContent = message.payload.oauthUrl;
  242. displayOauthUrl.classList.add('has-value');
  243. }
  244. if (message.payload.localhostUrl) {
  245. displayLocalhostUrl.textContent = message.payload.localhostUrl;
  246. displayLocalhostUrl.classList.add('has-value');
  247. }
  248. break;
  249. }
  250. case 'AUTO_RUN_STATUS': {
  251. const { phase } = message.payload;
  252. switch (phase) {
  253. case 'waiting_email':
  254. autoContinueBar.style.display = 'flex';
  255. btnAutoRun.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg> Paused';
  256. break;
  257. case 'complete':
  258. btnAutoRun.disabled = false;
  259. btnAutoRun.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"/></svg> Auto';
  260. autoContinueBar.style.display = 'none';
  261. break;
  262. case 'stopped':
  263. btnAutoRun.disabled = false;
  264. btnAutoRun.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"/></svg> Auto';
  265. autoContinueBar.style.display = 'none';
  266. break;
  267. }
  268. break;
  269. }
  270. }
  271. });
  272. // ============================================================
  273. // Init
  274. // ============================================================
  275. restoreState().then(() => {
  276. updateButtonStates();
  277. });