fix(plugins): 未注册插件默认启用 + 开关直接加载(运行时启用)+ 加载成功自愈启用状态 + 全部启用按钮 + 市场请求双通道回退
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 35s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 35s
This commit is contained in:
1 parent
06d8551107
commit
371f114d69
5 files changed
+77
-13
No files matched your search
@@ -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)
|
||||
|
||||
+5
-2
@@ -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) {
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
@@ -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' })),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user