From 371f114d69488b9a07ec5ed0b04f9ad96f0c49aa Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 16:34:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(plugins):=20=E6=9C=AA=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E9=BB=98=E8=AE=A4=E5=90=AF=E7=94=A8=20+=20?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E7=9B=B4=E6=8E=A5=E5=8A=A0=E8=BD=BD=EF=BC=88?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E5=90=AF=E7=94=A8=EF=BC=89+=20?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E6=88=90=E5=8A=9F=E8=87=AA=E6=84=88=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E7=8A=B6=E6=80=81=20+=20=E5=85=A8=E9=83=A8=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E6=8C=89=E9=92=AE=20+=20=E5=B8=82=E5=9C=BA=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=8C=E9=80=9A=E9=81=93=E5=9B=9E=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 20 +++++++++++++++++ src/plugin-host.js | 7 ++++-- src/runtime-plugin-loader.js | 4 +++- src/runtime-plugin-market.js | 17 +++++++++++---- src/ui-settings-sections.js | 42 ++++++++++++++++++++++++++++++------ 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/app.js b/app.js index 39a6247..d4f4a47 100644 --- a/app.js +++ b/app.js @@ -8539,6 +8539,26 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => return L.loadLocal(record) } + // 运行期启用(插件管理里把「未加载」的插件打开开关时调用):先加载脚本,再写启用状态 + async function wEnablePluginRuntime(id) { + try { + const rec = (wRuntimePlugins || []).find(function (r) { return r && r.id === id }) + if (!rec) return { ok: false, error: '插件清单里没有该 id' } + if (rec.status === 'loaded') { + const r0 = PluginHost && typeof PluginHost.setEnabled === 'function' ? PluginHost.setEnabled(id, true) : null + return r0 && r0.ok ? { ok: true } : { ok: false, error: (r0 && r0.error) || '启用失败' } + } + let loaded = false + if (rec.source === 'local') loaded = await wLoadLocalPluginEntry(rec) + else if (rec.source === 'external') loaded = await wLoadExternalPluginEntry(rec) + else loaded = await wLoadPluginEntry(rec) + if (!loaded) return { ok: false, error: rec.error || '脚本加载失败' } + const r = PluginHost && typeof PluginHost.setEnabled === 'function' ? PluginHost.setEnabled(id, true) : null + return r && r.ok ? { ok: true } : { ok: false, error: (r && r.error) || '启用失败' } + } catch (e) { return { ok: false, error: String((e && e.message) || e) } } + } + try { window.IWBPluginRuntime = { enable: wEnablePluginRuntime } } catch (e) { /* ignore */ } + // 按需加载(插件管理里勾选 idle 行):下载脚本 → 注册 → 显式启用;失败时返回错误供 UI 回滚勾选并提示。 async function wEnsurePluginLoaded(id) { const record = wRuntimePlugins.find((r) => r.id === id) diff --git a/src/plugin-host.js b/src/plugin-host.js index 57387b9..3a9bb21 100644 --- a/src/plugin-host.js +++ b/src/plugin-host.js @@ -229,8 +229,11 @@ function isEnabled(id) { if (plugins.has(id)) return Boolean(recordOf(id).enabled) - // 未注册:直接读持久化开关(不得走 recordOf——那会为陌生 id 创建 state 条目) - return Boolean(state[id] && state[id].enabled) + // 未注册:有持久化记录就按记录;**没有记录 ⇒ 默认启用** + // (否则启动早期 state 还没读到时,所有插件都会被判成「已停用」而进 idle) + const rec = state[id] + if (!rec) return true + return Boolean(rec.enabled) } function init(nextDeps) { diff --git a/src/runtime-plugin-loader.js b/src/runtime-plugin-loader.js index 08a478e..d8234a9 100644 --- a/src/runtime-plugin-loader.js +++ b/src/runtime-plugin-loader.js @@ -46,7 +46,9 @@ const H = host() const registered = !!(H && typeof H.list === 'function' && H.list().some(function (p) { return p.id === record.id })) record.status = registered ? 'loaded' : 'not-registered' - if (registered && typeof H.isEnabled === 'function' && H.isEnabled(record.id)) H.setEnabled(record.id, true) + // 自愈:只要脚本成功注册,就把启用状态写回 true + // (此前只在「已经启用」时写,导致状态里残留的 false 永远翻不回来,界面显示已加载但开关是关的) + if (registered) { try { H.setEnabled(record.id, true) } catch (e) { /* ignore */ } } return record.status === 'loaded' } diff --git a/src/runtime-plugin-market.js b/src/runtime-plugin-market.js index 115c979..0d84f9e 100644 --- a/src/runtime-plugin-market.js +++ b/src/runtime-plugin-market.js @@ -13,11 +13,20 @@ return (F && F.plugins) ? F.plugins : null } - // 桌面端优先走 Tauri 原生 HTTP(WKWebView 在代理/fake-IP 环境下 fetch 会 Load failed) - function marketFetch (url) { + // 桌面端优先走 Tauri 原生 HTTP(WKWebView 在代理/fake-IP 环境下 fetch 会 Load failed); + // 原生失败时再退回 WebView fetch(有些 Windows 公司网络/代理只放行后者),两个错误都带上,便于定位。 + async function marketFetch (url) { const T = (global.__TAURI__ && global.__TAURI__.http) ? global.__TAURI__.http : null - if (T && typeof T.fetch === 'function') return T.fetch(url) - return global.fetch(url, { cache: 'no-store' }) + let nativeErr = '' + if (T && typeof T.fetch === 'function') { + try { return await T.fetch(url) } catch (e) { nativeErr = String((e && e.message) || e) } + } + try { + return await global.fetch(url, { cache: 'no-store' }) + } catch (e2) { + const webErr = String((e2 && e2.message) || e2) + throw new Error('无法访问 ' + url + (nativeErr ? ('(原生 HTTP:' + nativeErr + ')') : '') + '(网页请求:' + webErr + ')') + } } async function list () { diff --git a/src/ui-settings-sections.js b/src/ui-settings-sections.js index 36b6b11..2eb5eb1 100644 --- a/src/ui-settings-sections.js +++ b/src/ui-settings-sections.js @@ -159,10 +159,22 @@ if (kind === 'install') setRestart(true) } finally { setBusy('') } } - const toggle = function (id, on) { - const r = PluginHost.setEnabled(id, on) - setPluginsRev(function (v) { return v + 1 }) - if (!r.ok) setMsg('切换失败:' + r.error) + const toggle = async function (id, on, registered) { + setBusy(id) + try { + // 打开一个「未加载」的插件:先加载脚本,再写启用状态(否则只是记了个开关,插件依然不生效) + if (on && !registered && typeof window !== 'undefined' && window.IWBPluginRuntime) { + const r1 = await window.IWBPluginRuntime.enable(id) + if (!r1 || !r1.ok) { setMsg('启用失败:' + ((r1 && r1.error) || '未知错误')); return } + setMsg('已启用') + return + } + const r = PluginHost.setEnabled(id, on) + if (!r.ok) setMsg('切换失败:' + r.error) + } finally { + setPluginsRev(function (v) { return v + 1 }) + setBusy('') + } } const kw = q.trim().toLowerCase() const hit = function (name, desc) { return !kw || (String(name) + ' ' + String(desc || '')).toLowerCase().indexOf(kw) >= 0 } @@ -243,7 +255,10 @@ const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null let manifest = [] try { manifest = await F.plugins.manifest() } catch (e) { manifest = String(e && e.message) } + let marketInfo = '' + try { const mm = await D.market.list(); marketInfo = { ok: true, count: (mm || []).length } } catch (e) { marketInfo = { ok: false, error: String((e && e.message) || e) } } const payload = { + market: marketInfo, dir: (F && F.dir) ? F.dir() : '', devMode: pluginDevGet(), manifest: manifest, @@ -257,6 +272,21 @@ }, }, '复制诊断'), ), + el('div', { className: 'iwb-row' }, + el('span', { className: 'iwb-hint', style: { flex: '1 1 auto' } }, '开关灰色或显示「未加载」时,点右侧按钮会当场加载并启用'), + el('button', { className: 'iwb-btn iwb-btn-sm', disabled: !!busy, onClick: async function () { + const list = installedRows.filter(function (p) { return !p.registered }) + if (!list.length) { setMsg('已安装的插件都已加载'); return } + setBusy('__all__') + let ok = 0 + for (const p of list) { + try { const r = await window.IWBPluginRuntime.enable(p.id); if (r && r.ok) ok++ } catch (e) { /* ignore */ } + } + setBusy('') + setPluginsRev(function (v) { return v + 1 }) + setMsg('已启用 ' + ok + '/' + list.length + ' 个插件') + } }, busy === '__all__' ? '处理中…' : '全部启用'), + ), el('div', { className: 'iwb-section-sub' }, '已安装插件'), el('div', { className: 'iwb-row' }, el('input', { className: 'iwb-input iwb-grow', placeholder: '搜索已安装的插件…', value: q, onChange: function (e) { setQ(e.target.value) } }), @@ -284,8 +314,8 @@ el('button', { className: 'iwb-btn iwb-btn-sm', disabled: !!busy, onClick: function () { act('uninstall', p.id) } }, '移除'), el('label', { className: 'iwb-switch', title: p.registered ? '启用 / 停用' : '未加载,无法启用' }, el('input', { - type: 'checkbox', checked: p.enabled, disabled: !p.registered || !p.compatible, - onChange: function (e) { toggle(p.id, e.target.checked) }, + type: 'checkbox', checked: p.enabled, disabled: !p.compatible, + onChange: function (e) { toggle(p.id, e.target.checked, p.registered) }, }), el('span', { className: 'iwb-switch-track' }, el('span', { className: 'iwb-switch-dot' })), ),