补齐换号扩展联动接口
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2 files changed
+23
-1
No files matched your search
+21
-1
@@ -9,6 +9,7 @@
|
||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||
const visible = el => !!el && !!el.getClientRects().length && getComputedStyle(el).visibility !== 'hidden';
|
||||
let flow = null, busy = false, running = false, panel, message, nextButton, resumeButton, retryButton, stopButton;
|
||||
let bridgeHost, bridgeReady = false, stopRevision = 0;
|
||||
let actions, toolbar, panelToggle, hideButton, restoreButton, panelHidden = false, visibilitySaving = false;
|
||||
class Stopped extends Error {}
|
||||
|
||||
@@ -39,6 +40,11 @@
|
||||
}
|
||||
function controls() {
|
||||
if (!nextButton) return;
|
||||
// Only lifecycle flags cross into the page; never credentials or account data.
|
||||
bridgeHost?.setAttribute('data-auto-news-state', JSON.stringify({
|
||||
ready: bridgeReady, idle: bridgeReady && !busy && !running && !flow,
|
||||
paused: !!flow?.paused
|
||||
}));
|
||||
nextButton.disabled = busy || !!flow;
|
||||
resumeButton.hidden = !flow || (!flow.paused && running);
|
||||
resumeButton.disabled = busy || running;
|
||||
@@ -107,6 +113,9 @@
|
||||
async function suspend(reason) {
|
||||
if (flow && !flow.paused) {
|
||||
const run = flow.id;
|
||||
flow.paused = true;
|
||||
flow.reason = reason;
|
||||
controls();
|
||||
try { await request('/suspend', { run, reason }); }
|
||||
finally { if (flow?.id === run) { flow.paused = true; flow.reason = reason; } }
|
||||
}
|
||||
@@ -165,14 +174,19 @@
|
||||
}
|
||||
async function begin() {
|
||||
if (location.hostname === 'dww11.com') { location.assign(HOME); return; }
|
||||
const revision = stopRevision;
|
||||
await recordRestriction();
|
||||
if (revision !== stopRevision) return;
|
||||
flow = (await request('/start')).flow;
|
||||
if (revision !== stopRevision) { await suspend('新闻工作台已终止流程。'); return; }
|
||||
await drive();
|
||||
}
|
||||
async function resume(retry = false) {
|
||||
if (location.hostname === 'dww11.com') { location.assign(HOME); return; }
|
||||
if (retry && !confirm('将重新提交本次抽到的账号(不抽新账号)。如上次提交已成功,可能再次登录。确认重试?')) return;
|
||||
const revision = stopRevision;
|
||||
flow = (await request('/resume')).flow;
|
||||
if (revision !== stopRevision) { await suspend('新闻工作台已终止流程。'); return; }
|
||||
if (retry) {
|
||||
await save({ phase: 'logout', hops: 0, submittedAt: null, probedAt: null, invalidError: null });
|
||||
location.assign(HOME);
|
||||
@@ -291,6 +305,7 @@
|
||||
|
||||
function mount() {
|
||||
const host = document.createElement('div');
|
||||
bridgeHost = host;
|
||||
host.id = 'dww11-extension-panel';
|
||||
const root = host.attachShadow({ mode: 'closed' });
|
||||
const style = document.createElement('style');
|
||||
@@ -328,6 +343,7 @@
|
||||
const info = await request('/hello');
|
||||
panelVisibility(info.panelHidden);
|
||||
flow = info.flow;
|
||||
bridgeReady = true;
|
||||
if (flow && !info.owned) {
|
||||
flow.paused = true;
|
||||
show('存在未完成流程。可在原标签页继续;原标签关闭后可在此接续。', true);
|
||||
@@ -338,5 +354,9 @@
|
||||
await recordRestriction();
|
||||
}
|
||||
}
|
||||
init().catch(e => { if (message) { panelVisibility(false); show('扩展启动失败:' + e.message, true); } });
|
||||
document.addEventListener('dww11:auto-stop', () => {
|
||||
stopRevision++;
|
||||
if (flow && !flow.paused) void suspend('新闻工作台已终止流程。').catch(e => show(e.message, true));
|
||||
});
|
||||
init().catch(e => { bridgeReady = false; if (message) { panelVisibility(false); show('扩展启动失败:' + e.message, true); } });
|
||||
})();
|
||||
Reference in new issue
Block a user