'use strict'; /** * 云仓视频溯源 · 选项页(**只读状态页**) * * 插件不再提供规则编辑 / 试跑 / 导入导出 / 使用说明: * 识别前缀、生效站点、跳转模板全部由后台「平台管理 → 插件配置」维护, * 插件每小时自动同步(后台 background.js),这里只展示状态 + 提供「立即同步」。 */ (function () { const R = globalThis.PSTRules; const $ = id => document.getElementById(id); const SYNC_STATE_KEY = 'pstSyncState'; const CONFIG_SOURCE = 'https://pack.wms.gztbyc.com/api/web/extension/config'; init(); async function init() { $('sync-now').addEventListener('click', onSyncNow); $('download').addEventListener('click', onDownload); await render(); } async function render() { let cfg; try { cfg = await R.loadConfig(); } catch (_) { cfg = R.defaultConfig(); } const state = await readSyncState(); const remote = cfg.remote || {}; $('version').textContent = 'v' + (remote.version || '—'); $('source').textContent = CONFIG_SOURCE; if (state.error) { $('badge-error').style.display = ''; $('error-row').style.display = ''; $('sync-error').textContent = state.error; } else { $('badge-error').style.display = 'none'; $('error-row').style.display = 'none'; } $('last-sync').textContent = remote.syncedAt ? formatTime(remote.syncedAt) + (state.reason === 'manual' ? '(手动)' : '(自动)') : '还没同步过(点「立即同步」)'; renderChips($('hosts'), cfg.hosts, 'host', '不限制(所有站点)'); renderChips($('prefixes'), remote.prefixes && remote.prefixes.length ? remote.prefixes : [], '', '等待服务器下发'); $('digits').textContent = remote.minDigits ? remote.minDigits + ' ~ ' + remote.maxDigits + ' 位' : '—'; $('app-url').textContent = cfg.appUrlTemplate || '—'; $('web-url').textContent = cfg.urlTemplate || '—'; const download = $('download'); if (cfg.downloadUrl) { download.style.display = ''; download.title = cfg.downloadUrl; } else { download.style.display = 'none'; } } function renderChips(box, list, extraClass, emptyText) { box.textContent = ''; if (!list || !list.length) { const span = document.createElement('span'); span.className = 'muted'; span.textContent = emptyText; box.appendChild(span); return; } for (const item of list) { const chip = document.createElement('span'); chip.className = 'chip' + (extraClass ? ' ' + extraClass : ''); chip.textContent = item; box.appendChild(chip); } } async function onSyncNow() { const button = $('sync-now'); button.disabled = true; button.textContent = '同步中…'; try { const result = await sendSync(); if (result && result.ok) { $('foot').className = 'ok'; $('foot').textContent = result.unchanged ? '已是最新配置(v' + result.version + ')' : '已更新到 v' + result.version + ',页面会自动重新扫描'; } else { $('foot').className = 'err'; $('foot').textContent = '同步失败:' + ((result && result.error) || '未知错误'); } } finally { button.disabled = false; button.textContent = '立即同步'; await render(); } } function sendSync() { return new Promise(resolve => { chrome.runtime.sendMessage({ type: 'pst:sync' }, res => { const err = chrome.runtime.lastError; resolve(err ? { ok: false, error: err.message } : res); }); }); } async function onDownload() { const cfg = await R.loadConfig(); if (!cfg.downloadUrl) return; chrome.tabs.create({ url: cfg.downloadUrl }); } async function readSyncState() { try { const stored = await chrome.storage.local.get(SYNC_STATE_KEY); return (stored && stored[SYNC_STATE_KEY]) || {}; } catch (_) { return {}; } } function formatTime(iso) { const date = new Date(iso); if (Number.isNaN(date.getTime())) return iso; const pad = n => String(n).padStart(2, '0'); return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()); } })();