| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955 |
- // sidepanel/sidepanel.js — Side Panel logic
- const STATUS_ICONS = {
- pending: '',
- running: '',
- completed: '\u2713', // ✓
- failed: '\u2717', // ✗
- stopped: '\u25A0', // ■
- manual_completed: '手',
- };
- const logArea = document.getElementById('log-area');
- const displayOauthUrl = document.getElementById('display-oauth-url');
- const displayLocalhostUrl = document.getElementById('display-localhost-url');
- const displayStatus = document.getElementById('display-status');
- const statusBar = document.getElementById('status-bar');
- const inputEmail = document.getElementById('input-email');
- const inputPassword = document.getElementById('input-password');
- const btnFetchEmail = document.getElementById('btn-fetch-email');
- const btnTogglePassword = document.getElementById('btn-toggle-password');
- const btnSaveSettings = document.getElementById('btn-save-settings');
- const btnStop = document.getElementById('btn-stop');
- const btnReset = document.getElementById('btn-reset');
- const stepsProgress = document.getElementById('steps-progress');
- const btnAutoRun = document.getElementById('btn-auto-run');
- const btnAutoContinue = document.getElementById('btn-auto-continue');
- const autoContinueBar = document.getElementById('auto-continue-bar');
- const btnClearLog = document.getElementById('btn-clear-log');
- const inputVpsUrl = document.getElementById('input-vps-url');
- const inputVpsPassword = document.getElementById('input-vps-password');
- const selectMailProvider = document.getElementById('select-mail-provider');
- const rowInbucketHost = document.getElementById('row-inbucket-host');
- const inputInbucketHost = document.getElementById('input-inbucket-host');
- const rowInbucketMailbox = document.getElementById('row-inbucket-mailbox');
- const inputInbucketMailbox = document.getElementById('input-inbucket-mailbox');
- const inputRunCount = document.getElementById('input-run-count');
- const inputAutoSkipFailures = document.getElementById('input-auto-skip-failures');
- const STEP_DEFAULT_STATUSES = {
- 1: 'pending',
- 2: 'pending',
- 3: 'pending',
- 4: 'pending',
- 5: 'pending',
- 6: 'pending',
- 7: 'pending',
- 8: 'pending',
- 9: 'pending',
- };
- const MANUAL_COMPLETION_STEPS = new Set([2, 3, 4, 5, 6, 7, 8]);
- let latestState = null;
- let currentAutoRun = {
- autoRunning: false,
- phase: 'idle',
- currentRun: 0,
- totalRuns: 1,
- attemptRun: 0,
- };
- let settingsDirty = false;
- let settingsSaveInFlight = false;
- let settingsAutoSaveTimer = null;
- const EYE_OPEN_ICON = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7S1 12 1 12z"/><circle cx="12" cy="12" r="3"/></svg>';
- const EYE_CLOSED_ICON = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17.94 17.94A10.94 10.94 0 0 1 12 19C5 19 1 12 1 12a21.77 21.77 0 0 1 5.06-6.94"/><path d="M9.9 4.24A10.94 10.94 0 0 1 12 5c7 0 11 7 11 7a21.86 21.86 0 0 1-2.16 3.19"/><path d="M1 1l22 22"/><path d="M14.12 14.12a3 3 0 1 1-4.24-4.24"/></svg>';
- // ============================================================
- // Toast Notifications
- // ============================================================
- const toastContainer = document.getElementById('toast-container');
- const TOAST_ICONS = {
- error: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>',
- warn: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>',
- success: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>',
- info: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>',
- };
- const LOG_LEVEL_LABELS = {
- info: '信息',
- ok: '成功',
- warn: '警告',
- error: '错误',
- };
- function showToast(message, type = 'error', duration = 4000) {
- const toast = document.createElement('div');
- toast.className = `toast toast-${type}`;
- toast.innerHTML = `${TOAST_ICONS[type] || ''}<span class="toast-msg">${escapeHtml(message)}</span><button class="toast-close">×</button>`;
- toast.querySelector('.toast-close').addEventListener('click', () => dismissToast(toast));
- toastContainer.appendChild(toast);
- if (duration > 0) {
- setTimeout(() => dismissToast(toast), duration);
- }
- }
- function dismissToast(toast) {
- if (!toast.parentNode) return;
- toast.classList.add('toast-exit');
- toast.addEventListener('animationend', () => toast.remove());
- }
- function isDoneStatus(status) {
- return status === 'completed' || status === 'manual_completed';
- }
- function getStepStatuses(state = latestState) {
- return { ...STEP_DEFAULT_STATUSES, ...(state?.stepStatuses || {}) };
- }
- function syncLatestState(nextState) {
- const mergedStepStatuses = nextState?.stepStatuses
- ? { ...STEP_DEFAULT_STATUSES, ...(latestState?.stepStatuses || {}), ...nextState.stepStatuses }
- : getStepStatuses(latestState);
- latestState = {
- ...(latestState || {}),
- ...(nextState || {}),
- stepStatuses: mergedStepStatuses,
- };
- }
- function syncAutoRunState(source = {}) {
- const phase = source.autoRunPhase ?? source.phase ?? currentAutoRun.phase;
- const autoRunning = source.autoRunning !== undefined
- ? Boolean(source.autoRunning)
- : (source.autoRunPhase !== undefined || source.phase !== undefined
- ? ['running', 'waiting_email', 'retrying'].includes(phase)
- : currentAutoRun.autoRunning);
- currentAutoRun = {
- autoRunning,
- phase,
- currentRun: source.autoRunCurrentRun ?? source.currentRun ?? currentAutoRun.currentRun,
- totalRuns: source.autoRunTotalRuns ?? source.totalRuns ?? currentAutoRun.totalRuns,
- attemptRun: source.autoRunAttemptRun ?? source.attemptRun ?? currentAutoRun.attemptRun,
- };
- }
- function isAutoRunLockedPhase() {
- return currentAutoRun.phase === 'running' || currentAutoRun.phase === 'retrying';
- }
- function isAutoRunPausedPhase() {
- return currentAutoRun.phase === 'waiting_email';
- }
- function getAutoRunLabel(payload = currentAutoRun) {
- const attemptLabel = payload.attemptRun ? ` · 尝试${payload.attemptRun}` : '';
- if ((payload.totalRuns || 1) > 1) {
- return ` (${payload.currentRun}/${payload.totalRuns}${attemptLabel})`;
- }
- return attemptLabel ? ` (${attemptLabel.slice(3)})` : '';
- }
- function setDefaultAutoRunButton() {
- btnAutoRun.disabled = false;
- inputRunCount.disabled = false;
- 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> 自动';
- }
- function collectSettingsPayload() {
- return {
- vpsUrl: inputVpsUrl.value.trim(),
- vpsPassword: inputVpsPassword.value,
- customPassword: inputPassword.value,
- mailProvider: selectMailProvider.value,
- inbucketHost: inputInbucketHost.value.trim(),
- inbucketMailbox: inputInbucketMailbox.value.trim(),
- autoRunSkipFailures: inputAutoSkipFailures.checked,
- };
- }
- function markSettingsDirty(isDirty = true) {
- settingsDirty = isDirty;
- updateSaveButtonState();
- }
- function updateSaveButtonState() {
- btnSaveSettings.disabled = settingsSaveInFlight || !settingsDirty;
- btnSaveSettings.textContent = settingsSaveInFlight ? '保存中' : '保存';
- }
- function scheduleSettingsAutoSave() {
- clearTimeout(settingsAutoSaveTimer);
- settingsAutoSaveTimer = setTimeout(() => {
- saveSettings({ silent: true }).catch(() => {});
- }, 500);
- }
- async function saveSettings(options = {}) {
- const { silent = false } = options;
- clearTimeout(settingsAutoSaveTimer);
- if (!settingsDirty && !settingsSaveInFlight && silent) {
- return;
- }
- const payload = collectSettingsPayload();
- settingsSaveInFlight = true;
- updateSaveButtonState();
- try {
- const response = await chrome.runtime.sendMessage({
- type: 'SAVE_SETTING',
- source: 'sidepanel',
- payload,
- });
- if (response?.error) {
- throw new Error(response.error);
- }
- syncLatestState(payload);
- markSettingsDirty(false);
- updateMailProviderUI();
- updateButtonStates();
- if (!silent) {
- showToast('配置已保存', 'success', 1800);
- }
- } catch (err) {
- markSettingsDirty(true);
- if (!silent) {
- showToast(`保存失败:${err.message}`, 'error');
- }
- throw err;
- } finally {
- settingsSaveInFlight = false;
- updateSaveButtonState();
- }
- }
- function applyAutoRunStatus(payload = currentAutoRun) {
- syncAutoRunState(payload);
- const runLabel = getAutoRunLabel(currentAutoRun);
- const locked = isAutoRunLockedPhase();
- const paused = isAutoRunPausedPhase();
- inputRunCount.disabled = currentAutoRun.autoRunning;
- btnAutoRun.disabled = currentAutoRun.autoRunning;
- btnFetchEmail.disabled = locked;
- inputEmail.disabled = locked;
- switch (currentAutoRun.phase) {
- case 'waiting_email':
- autoContinueBar.style.display = 'flex';
- btnAutoRun.innerHTML = `已暂停${runLabel}`;
- break;
- case 'running':
- autoContinueBar.style.display = 'none';
- btnAutoRun.innerHTML = `运行中${runLabel}`;
- break;
- case 'retrying':
- autoContinueBar.style.display = 'none';
- btnAutoRun.innerHTML = `重试中${runLabel}`;
- break;
- default:
- autoContinueBar.style.display = 'none';
- setDefaultAutoRunButton();
- inputEmail.disabled = false;
- if (!locked) {
- btnFetchEmail.disabled = false;
- }
- break;
- }
- updateStopButtonState(paused || locked || Object.values(getStepStatuses()).some(status => status === 'running'));
- }
- function initializeManualStepActions() {
- document.querySelectorAll('.step-row').forEach((row) => {
- const step = Number(row.dataset.step);
- const statusEl = row.querySelector('.step-status');
- if (!statusEl) return;
- const actions = document.createElement('div');
- actions.className = 'step-actions';
- const manualBtn = document.createElement('button');
- manualBtn.type = 'button';
- manualBtn.className = 'step-manual-btn';
- manualBtn.dataset.step = String(step);
- manualBtn.title = '标记此步已手动完成';
- manualBtn.setAttribute('aria-label', `标记步骤 ${step} 已手动完成`);
- manualBtn.innerHTML = '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="13 17 18 12 13 7"/><polyline points="6 17 11 12 6 7"/></svg>';
- manualBtn.addEventListener('click', async (event) => {
- event.stopPropagation();
- try {
- await handleManualComplete(step);
- } catch (err) {
- showToast(err.message, 'error');
- }
- });
- statusEl.parentNode.replaceChild(actions, statusEl);
- actions.appendChild(manualBtn);
- actions.appendChild(statusEl);
- });
- }
- // ============================================================
- // State Restore on load
- // ============================================================
- async function restoreState() {
- try {
- const state = await chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' });
- syncLatestState(state);
- syncAutoRunState(state);
- if (state.oauthUrl) {
- displayOauthUrl.textContent = state.oauthUrl;
- displayOauthUrl.classList.add('has-value');
- }
- if (state.localhostUrl) {
- displayLocalhostUrl.textContent = state.localhostUrl;
- displayLocalhostUrl.classList.add('has-value');
- }
- if (state.email) {
- inputEmail.value = state.email;
- }
- syncPasswordField(state);
- if (state.vpsUrl) {
- inputVpsUrl.value = state.vpsUrl;
- }
- if (state.vpsPassword) {
- inputVpsPassword.value = state.vpsPassword;
- }
- if (state.mailProvider) {
- selectMailProvider.value = state.mailProvider;
- }
- if (state.inbucketHost) {
- inputInbucketHost.value = state.inbucketHost;
- }
- if (state.inbucketMailbox) {
- inputInbucketMailbox.value = state.inbucketMailbox;
- }
- inputAutoSkipFailures.checked = Boolean(state.autoRunSkipFailures);
- if (state.stepStatuses) {
- for (const [step, status] of Object.entries(state.stepStatuses)) {
- updateStepUI(Number(step), status);
- }
- }
- if (state.logs) {
- for (const entry of state.logs) {
- appendLog(entry);
- }
- }
- applyAutoRunStatus(state);
- markSettingsDirty(false);
- updateStatusDisplay(latestState);
- updateProgressCounter();
- updateMailProviderUI();
- updateButtonStates();
- } catch (err) {
- console.error('Failed to restore state:', err);
- }
- }
- function syncPasswordField(state) {
- inputPassword.value = state.customPassword || state.password || '';
- }
- function updateMailProviderUI() {
- const useInbucket = selectMailProvider.value === 'inbucket';
- rowInbucketHost.style.display = useInbucket ? '' : 'none';
- rowInbucketMailbox.style.display = useInbucket ? '' : 'none';
- }
- // ============================================================
- // UI Updates
- // ============================================================
- function updateStepUI(step, status) {
- const statusEl = document.querySelector(`.step-status[data-step="${step}"]`);
- const row = document.querySelector(`.step-row[data-step="${step}"]`);
- syncLatestState({
- stepStatuses: {
- ...getStepStatuses(),
- [step]: status,
- },
- });
- if (statusEl) statusEl.textContent = STATUS_ICONS[status] || '';
- if (row) {
- row.className = `step-row ${status}`;
- }
- updateButtonStates();
- updateProgressCounter();
- }
- function updateProgressCounter() {
- const completed = Object.values(getStepStatuses()).filter(isDoneStatus).length;
- stepsProgress.textContent = `${completed} / 9`;
- }
- function updateButtonStates() {
- const statuses = getStepStatuses();
- const anyRunning = Object.values(statuses).some(s => s === 'running');
- const autoLocked = isAutoRunLockedPhase();
- for (let step = 1; step <= 9; step++) {
- const btn = document.querySelector(`.step-btn[data-step="${step}"]`);
- if (!btn) continue;
- if (anyRunning || autoLocked) {
- btn.disabled = true;
- } else if (step === 1) {
- btn.disabled = false;
- } else {
- const prevStatus = statuses[step - 1];
- const currentStatus = statuses[step];
- btn.disabled = !(isDoneStatus(prevStatus) || currentStatus === 'failed' || isDoneStatus(currentStatus) || currentStatus === 'stopped');
- }
- }
- document.querySelectorAll('.step-manual-btn').forEach((btn) => {
- const step = Number(btn.dataset.step);
- const currentStatus = statuses[step];
- const prevStatus = statuses[step - 1];
- if (!MANUAL_COMPLETION_STEPS.has(step) || anyRunning || autoLocked || currentStatus === 'running' || isDoneStatus(currentStatus)) {
- btn.style.display = 'none';
- btn.disabled = true;
- btn.title = '当前不可手动同步';
- return;
- }
- if (step > 1 && !isDoneStatus(prevStatus)) {
- btn.style.display = 'none';
- btn.disabled = true;
- btn.title = `请先完成步骤 ${step - 1}`;
- return;
- }
- let disabledReason = '';
- if (step === 3) {
- if (!(latestState?.email || inputEmail.value.trim())) {
- disabledReason = '请先填写邮箱';
- } else if (!(latestState?.password || latestState?.customPassword || inputPassword.value)) {
- disabledReason = '请先在侧边栏填写固定密码';
- }
- } else if (step === 6 && !(latestState?.email || inputEmail.value.trim())) {
- disabledReason = '请先填写邮箱';
- } else if (step === 8 && !latestState?.localhostUrl) {
- disabledReason = '尚未捕获 localhost 回调地址';
- }
- btn.style.display = '';
- btn.disabled = Boolean(disabledReason);
- btn.title = disabledReason || `将步骤 ${step} 标记为已手动完成`;
- });
- updateStopButtonState(anyRunning || isAutoRunPausedPhase() || autoLocked);
- }
- function updateStopButtonState(active) {
- btnStop.disabled = !active;
- }
- function updateStatusDisplay(state) {
- if (!state || !state.stepStatuses) return;
- statusBar.className = 'status-bar';
- if (isAutoRunPausedPhase()) {
- displayStatus.textContent = `自动已暂停${getAutoRunLabel()},等待邮箱后继续`;
- statusBar.classList.add('paused');
- return;
- }
- const running = Object.entries(state.stepStatuses).find(([, s]) => s === 'running');
- if (running) {
- displayStatus.textContent = `步骤 ${running[0]} 运行中...`;
- statusBar.classList.add('running');
- return;
- }
- if (isAutoRunLockedPhase()) {
- displayStatus.textContent = `${currentAutoRun.phase === 'retrying' ? '自动重试中' : '自动运行中'}${getAutoRunLabel()}`;
- statusBar.classList.add('running');
- return;
- }
- const failed = Object.entries(state.stepStatuses).find(([, s]) => s === 'failed');
- if (failed) {
- displayStatus.textContent = `步骤 ${failed[0]} 失败`;
- statusBar.classList.add('failed');
- return;
- }
- const stopped = Object.entries(state.stepStatuses).find(([, s]) => s === 'stopped');
- if (stopped) {
- displayStatus.textContent = `步骤 ${stopped[0]} 已停止`;
- statusBar.classList.add('stopped');
- return;
- }
- const lastCompleted = Object.entries(state.stepStatuses)
- .filter(([, s]) => isDoneStatus(s))
- .map(([k]) => Number(k))
- .sort((a, b) => b - a)[0];
- if (lastCompleted === 9) {
- displayStatus.textContent = isDoneStatus(state.stepStatuses[9]) && state.stepStatuses[9] === 'manual_completed' ? '全部步骤已手动完成' : '全部步骤已完成';
- statusBar.classList.add('completed');
- } else if (lastCompleted) {
- displayStatus.textContent = state.stepStatuses[lastCompleted] === 'manual_completed'
- ? `步骤 ${lastCompleted} 已手动完成`
- : `步骤 ${lastCompleted} 已完成`;
- } else {
- displayStatus.textContent = '就绪';
- }
- }
- function appendLog(entry) {
- const time = new Date(entry.timestamp).toLocaleTimeString('zh-CN', { hour12: false });
- const levelLabel = LOG_LEVEL_LABELS[entry.level] || entry.level;
- const line = document.createElement('div');
- line.className = `log-line log-${entry.level}`;
- const stepMatch = entry.message.match(/(?:Step\s+(\d+)|步骤\s*(\d+))/);
- const stepNum = stepMatch ? (stepMatch[1] || stepMatch[2]) : null;
- let html = `<span class="log-time">${time}</span> `;
- html += `<span class="log-level log-level-${entry.level}">${levelLabel}</span> `;
- if (stepNum) {
- html += `<span class="log-step-tag step-${stepNum}">步${stepNum}</span>`;
- }
- html += `<span class="log-msg">${escapeHtml(entry.message)}</span>`;
- line.innerHTML = html;
- logArea.appendChild(line);
- logArea.scrollTop = logArea.scrollHeight;
- }
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
- async function fetchDuckEmail(options = {}) {
- const { showFailureToast = true } = options;
- const defaultLabel = '获取';
- btnFetchEmail.disabled = true;
- btnFetchEmail.textContent = '...';
- try {
- const response = await chrome.runtime.sendMessage({
- type: 'FETCH_DUCK_EMAIL',
- source: 'sidepanel',
- payload: { generateNew: true },
- });
- if (response?.error) {
- throw new Error(response.error);
- }
- if (!response?.email) {
- throw new Error('未返回 Duck 邮箱。');
- }
- inputEmail.value = response.email;
- showToast(`已获取 ${response.email}`, 'success', 2500);
- return response.email;
- } catch (err) {
- if (showFailureToast) {
- showToast(`自动获取失败:${err.message}`, 'error');
- }
- throw err;
- } finally {
- btnFetchEmail.disabled = false;
- btnFetchEmail.textContent = defaultLabel;
- }
- }
- function syncPasswordToggleLabel() {
- const isHidden = inputPassword.type === 'password';
- btnTogglePassword.innerHTML = isHidden ? EYE_OPEN_ICON : EYE_CLOSED_ICON;
- btnTogglePassword.setAttribute('aria-label', isHidden ? '显示密码' : '隐藏密码');
- btnTogglePassword.title = isHidden ? '显示密码' : '隐藏密码';
- }
- async function maybeTakeoverAutoRun(actionLabel) {
- if (!isAutoRunPausedPhase()) {
- return true;
- }
- const confirmed = confirm(`当前自动流程已暂停。若继续${actionLabel},将停止自动流程并切换为手动控制。是否继续?`);
- if (!confirmed) {
- return false;
- }
- await chrome.runtime.sendMessage({ type: 'TAKEOVER_AUTO_RUN', source: 'sidepanel', payload: {} });
- return true;
- }
- async function handleManualComplete(step) {
- if (!(await maybeTakeoverAutoRun(`手动同步步骤 ${step}`))) {
- return;
- }
- if (step === 3 && inputPassword.value !== (latestState?.customPassword || '')) {
- await chrome.runtime.sendMessage({
- type: 'SAVE_SETTING',
- source: 'sidepanel',
- payload: { customPassword: inputPassword.value },
- });
- syncLatestState({ customPassword: inputPassword.value });
- }
- const confirmed = confirm(`这不会真正执行步骤 ${step},只会将面板状态同步为“已手动完成”。请确认你已经在网页中手动完成该步骤。`);
- if (!confirmed) {
- return;
- }
- const response = await chrome.runtime.sendMessage({
- type: 'MARK_STEP_MANUAL_COMPLETE',
- source: 'sidepanel',
- payload: { step },
- });
- if (response?.error) {
- throw new Error(response.error);
- }
- showToast(`步骤 ${step} 已同步为手动完成`, 'success', 2200);
- }
- // ============================================================
- // Button Handlers
- // ============================================================
- document.querySelectorAll('.step-btn').forEach(btn => {
- btn.addEventListener('click', async () => {
- try {
- const step = Number(btn.dataset.step);
- if (!(await maybeTakeoverAutoRun(`执行步骤 ${step}`))) {
- return;
- }
- if (step === 3) {
- if (inputPassword.value !== (latestState?.customPassword || '')) {
- await chrome.runtime.sendMessage({
- type: 'SAVE_SETTING',
- source: 'sidepanel',
- payload: { customPassword: inputPassword.value },
- });
- syncLatestState({ customPassword: inputPassword.value });
- }
- let email = inputEmail.value.trim();
- if (!email) {
- try {
- email = await fetchDuckEmail({ showFailureToast: false });
- } catch (err) {
- showToast(`自动获取失败:${err.message},请手动粘贴邮箱后重试。`, 'warn');
- return;
- }
- }
- const response = await chrome.runtime.sendMessage({ type: 'EXECUTE_STEP', source: 'sidepanel', payload: { step, email } });
- if (response?.error) {
- throw new Error(response.error);
- }
- } else {
- const response = await chrome.runtime.sendMessage({ type: 'EXECUTE_STEP', source: 'sidepanel', payload: { step } });
- if (response?.error) {
- throw new Error(response.error);
- }
- }
- } catch (err) {
- showToast(err.message, 'error');
- }
- });
- });
- btnFetchEmail.addEventListener('click', async () => {
- await fetchDuckEmail().catch(() => {});
- });
- btnTogglePassword.addEventListener('click', () => {
- inputPassword.type = inputPassword.type === 'password' ? 'text' : 'password';
- syncPasswordToggleLabel();
- });
- btnSaveSettings.addEventListener('click', async () => {
- if (!settingsDirty) {
- showToast('配置已是最新', 'info', 1400);
- return;
- }
- await saveSettings({ silent: false }).catch(() => {});
- });
- btnStop.addEventListener('click', async () => {
- btnStop.disabled = true;
- await chrome.runtime.sendMessage({ type: 'STOP_FLOW', source: 'sidepanel', payload: {} });
- showToast('正在停止当前流程...', 'warn', 2000);
- });
- // Auto Run
- btnAutoRun.addEventListener('click', async () => {
- const totalRuns = parseInt(inputRunCount.value) || 1;
- btnAutoRun.disabled = true;
- inputRunCount.disabled = true;
- 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> 运行中...';
- await chrome.runtime.sendMessage({
- type: 'AUTO_RUN',
- source: 'sidepanel',
- payload: {
- totalRuns,
- autoRunSkipFailures: inputAutoSkipFailures.checked,
- },
- });
- });
- btnAutoContinue.addEventListener('click', async () => {
- const email = inputEmail.value.trim();
- if (!email) {
- showToast('请先获取或粘贴 DuckDuckGo 邮箱。', 'warn');
- return;
- }
- autoContinueBar.style.display = 'none';
- await chrome.runtime.sendMessage({ type: 'RESUME_AUTO_RUN', source: 'sidepanel', payload: { email } });
- });
- // Reset
- btnReset.addEventListener('click', async () => {
- if (confirm('确认重置全部步骤和数据吗?')) {
- await chrome.runtime.sendMessage({ type: 'RESET', source: 'sidepanel' });
- syncLatestState({ stepStatuses: STEP_DEFAULT_STATUSES });
- syncAutoRunState({ autoRunning: false, autoRunPhase: 'idle', autoRunCurrentRun: 0, autoRunTotalRuns: 1, autoRunAttemptRun: 0 });
- displayOauthUrl.textContent = '等待中...';
- displayOauthUrl.classList.remove('has-value');
- displayLocalhostUrl.textContent = '等待中...';
- displayLocalhostUrl.classList.remove('has-value');
- inputEmail.value = '';
- displayStatus.textContent = '就绪';
- statusBar.className = 'status-bar';
- logArea.innerHTML = '';
- document.querySelectorAll('.step-row').forEach(row => row.className = 'step-row');
- document.querySelectorAll('.step-status').forEach(el => el.textContent = '');
- setDefaultAutoRunButton();
- applyAutoRunStatus(currentAutoRun);
- markSettingsDirty(false);
- updateStopButtonState(false);
- updateButtonStates();
- updateProgressCounter();
- }
- });
- // Clear log
- btnClearLog.addEventListener('click', () => {
- logArea.innerHTML = '';
- });
- // Save settings on change
- inputEmail.addEventListener('change', async () => {
- const email = inputEmail.value.trim();
- if (email) {
- await chrome.runtime.sendMessage({ type: 'SAVE_EMAIL', source: 'sidepanel', payload: { email } });
- }
- });
- inputEmail.addEventListener('input', updateButtonStates);
- inputVpsUrl.addEventListener('input', () => {
- markSettingsDirty(true);
- scheduleSettingsAutoSave();
- });
- inputVpsUrl.addEventListener('blur', () => {
- saveSettings({ silent: true }).catch(() => {});
- });
- inputVpsPassword.addEventListener('input', () => {
- markSettingsDirty(true);
- scheduleSettingsAutoSave();
- });
- inputVpsPassword.addEventListener('blur', () => {
- saveSettings({ silent: true }).catch(() => {});
- });
- inputPassword.addEventListener('input', () => {
- markSettingsDirty(true);
- updateButtonStates();
- scheduleSettingsAutoSave();
- });
- inputPassword.addEventListener('blur', () => {
- saveSettings({ silent: true }).catch(() => {});
- });
- selectMailProvider.addEventListener('change', () => {
- updateMailProviderUI();
- markSettingsDirty(true);
- saveSettings({ silent: true }).catch(() => {});
- });
- inputInbucketMailbox.addEventListener('input', () => {
- markSettingsDirty(true);
- scheduleSettingsAutoSave();
- });
- inputInbucketMailbox.addEventListener('blur', () => {
- saveSettings({ silent: true }).catch(() => {});
- });
- inputInbucketHost.addEventListener('input', () => {
- markSettingsDirty(true);
- scheduleSettingsAutoSave();
- });
- inputInbucketHost.addEventListener('blur', () => {
- saveSettings({ silent: true }).catch(() => {});
- });
- inputAutoSkipFailures.addEventListener('change', () => {
- markSettingsDirty(true);
- saveSettings({ silent: true }).catch(() => {});
- });
- // ============================================================
- // Listen for Background broadcasts
- // ============================================================
- chrome.runtime.onMessage.addListener((message) => {
- switch (message.type) {
- case 'LOG_ENTRY':
- appendLog(message.payload);
- if (message.payload.level === 'error') {
- showToast(message.payload.message, 'error');
- }
- break;
- case 'STEP_STATUS_CHANGED': {
- const { step, status } = message.payload;
- updateStepUI(step, status);
- chrome.runtime.sendMessage({ type: 'GET_STATE', source: 'sidepanel' }).then(state => {
- syncLatestState(state);
- syncAutoRunState(state);
- updateStatusDisplay(latestState);
- updateButtonStates();
- if (status === 'completed' || status === 'manual_completed') {
- syncPasswordField(state);
- if (state.oauthUrl) {
- displayOauthUrl.textContent = state.oauthUrl;
- displayOauthUrl.classList.add('has-value');
- }
- if (state.localhostUrl) {
- displayLocalhostUrl.textContent = state.localhostUrl;
- displayLocalhostUrl.classList.add('has-value');
- }
- }
- }
- ).catch(() => {});
- break;
- }
- case 'AUTO_RUN_RESET': {
- // Full UI reset for next run
- syncLatestState({
- oauthUrl: null,
- localhostUrl: null,
- email: null,
- password: null,
- stepStatuses: STEP_DEFAULT_STATUSES,
- logs: [],
- });
- displayOauthUrl.textContent = '等待中...';
- displayOauthUrl.classList.remove('has-value');
- displayLocalhostUrl.textContent = '等待中...';
- displayLocalhostUrl.classList.remove('has-value');
- inputEmail.value = '';
- displayStatus.textContent = '就绪';
- statusBar.className = 'status-bar';
- logArea.innerHTML = '';
- document.querySelectorAll('.step-row').forEach(row => row.className = 'step-row');
- document.querySelectorAll('.step-status').forEach(el => el.textContent = '');
- applyAutoRunStatus(currentAutoRun);
- updateProgressCounter();
- updateButtonStates();
- break;
- }
- case 'DATA_UPDATED': {
- syncLatestState(message.payload);
- if (message.payload.email) {
- inputEmail.value = message.payload.email;
- }
- if (message.payload.password !== undefined) {
- inputPassword.value = message.payload.password || '';
- }
- if (message.payload.oauthUrl) {
- displayOauthUrl.textContent = message.payload.oauthUrl;
- displayOauthUrl.classList.add('has-value');
- }
- if (message.payload.localhostUrl) {
- displayLocalhostUrl.textContent = message.payload.localhostUrl;
- displayLocalhostUrl.classList.add('has-value');
- }
- break;
- }
- case 'AUTO_RUN_STATUS': {
- syncLatestState({
- autoRunning: ['running', 'waiting_email', 'retrying'].includes(message.payload.phase),
- autoRunPhase: message.payload.phase,
- autoRunCurrentRun: message.payload.currentRun,
- autoRunTotalRuns: message.payload.totalRuns,
- autoRunAttemptRun: message.payload.attemptRun,
- });
- applyAutoRunStatus(message.payload);
- updateStatusDisplay(latestState);
- updateButtonStates();
- break;
- }
- }
- });
- // ============================================================
- // Theme Toggle
- // ============================================================
- const btnTheme = document.getElementById('btn-theme');
- function setTheme(theme) {
- document.documentElement.setAttribute('data-theme', theme);
- localStorage.setItem('multipage-theme', theme);
- }
- function initTheme() {
- const saved = localStorage.getItem('multipage-theme');
- if (saved) {
- setTheme(saved);
- } else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
- setTheme('dark');
- }
- }
- btnTheme.addEventListener('click', () => {
- const current = document.documentElement.getAttribute('data-theme');
- setTheme(current === 'dark' ? 'light' : 'dark');
- });
- // ============================================================
- // Init
- // ============================================================
- initializeManualStepActions();
- initTheme();
- updateSaveButtonState();
- restoreState().then(() => {
- syncPasswordToggleLabel();
- updateButtonStates();
- updateStatusDisplay(latestState);
- });
|