feat(plugin): 插件目录化 M1 —— 目录插件加载 + 旧单文件兼容 v3.25.41
- loader 新增 loadFolder():manifest → main.js(逐文件 sha256,内置信随包 index.json)→ blob 注入;
样式文本暂存,激活时注入、停用时移除(配合 host 新增 onActivate/onDeactivate 钩子)
- storage-file 增加 plugins.listDirs/readFile/writeFile/writeBinary/mkdir/removeDir(按段校验,禁 ..)
- app.js:三路合并支持目录条目(dir/main/style/files),失败隔离 + 按 id 排序
- 修复:无 iwb_plugins 记录时插件永不激活(首次安装面板显示「未加载」)
- 备份模块兼容目录形态;市场暂时跳过目录插件(M3 zip 后恢复)
- 试点:image-annotate 转为 plugins/image-annotate/{manifest.json,main.js}
- 方案文档 docs/PLUGIN-ARCHITECTURE-PLAN.md 一并入库
This commit is contained in:
1 parent
8dbc0c0603
commit
7ed3ba6738
17 files changed
+673
-22
No files matched your search
@@ -29,6 +29,108 @@
|
||||
return global.btoa(bin) === expect
|
||||
}
|
||||
|
||||
async function sha256Hex (text) {
|
||||
const buf = await global.crypto.subtle.digest('SHA-256', new global.TextEncoder().encode(String(text)))
|
||||
return Array.from(new Uint8Array(buf)).map(function (b) { return b.toString(16).padStart(2, '0') }).join('')
|
||||
}
|
||||
|
||||
// 目录插件(v3.25.41 / M1):一个插件一个目录
|
||||
// <内置>/plugins/<id>/{manifest.json,main.js,styles.css}
|
||||
// <资料目录>/plugins/<id>/{同}
|
||||
// 与单文件插件共用同一套「自注册」契约,区别只是多读一个 manifest 和可选 styles.css。
|
||||
const styleNodes = {}
|
||||
const pendingStyles = {}
|
||||
function applyStyle (id, css) {
|
||||
if (!css) return
|
||||
try {
|
||||
const doc = global.document
|
||||
if (!doc) return
|
||||
if (styleNodes[id] && styleNodes[id].parentNode) styleNodes[id].parentNode.removeChild(styleNodes[id])
|
||||
const node = doc.createElement('style')
|
||||
node.setAttribute('data-plugin', String(id))
|
||||
node.textContent = String(css)
|
||||
doc.head.appendChild(node)
|
||||
styleNodes[id] = node
|
||||
} catch (e) { /* 样式失败不影响脚本 */ }
|
||||
}
|
||||
function applyStyleFor (id) {
|
||||
const css = pendingStyles[id]
|
||||
if (css) applyStyle(id, css)
|
||||
}
|
||||
function removeStyle (id) {
|
||||
try { const n = styleNodes[id]; if (n && n.parentNode) n.parentNode.removeChild(n); delete styleNodes[id] } catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
async function readFolderFile (record, name) {
|
||||
if (record.source === 'external') {
|
||||
const src = source()
|
||||
if (!src || typeof src.readFile !== 'function') throw new Error('外部插件目录不可用')
|
||||
return await src.readFile(record.dir + '/' + name)
|
||||
}
|
||||
const resp = await global.fetch('/plugins/' + record.dir + '/' + name, { cache: 'no-store' })
|
||||
if (!resp.ok) throw new Error('读取插件文件失败:' + name + '(HTTP ' + resp.status + ')')
|
||||
return await resp.text()
|
||||
}
|
||||
|
||||
// 扫描 <资料目录>/plugins/*/manifest.json(无索引也能识别手工放入的目录插件)
|
||||
async function listFolderEntries () {
|
||||
const src = source()
|
||||
if (!src || typeof src.listDirs !== 'function') return []
|
||||
const out = []
|
||||
let dirs = []
|
||||
try { dirs = await src.listDirs() } catch (e) { return [] }
|
||||
for (const d of dirs) {
|
||||
const id = String((d && d.id) || '')
|
||||
if (!id) continue
|
||||
try {
|
||||
const m = JSON.parse(await src.readFile(id + '/manifest.json'))
|
||||
if (!m || String(m.id || '') !== id) continue
|
||||
out.push({ id: id, dir: id, main: m.main || 'main.js', style: m.style || '', version: m.version || '', files: m.files || null, source: 'external' })
|
||||
} catch (e) { /* 不是插件目录 */ }
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
async function loadFolder (record) {
|
||||
record.status = 'loading'
|
||||
try {
|
||||
// 目录里的 manifest.json 是入口/样式的真源:内置索引只带 hash,字段缺省时向 manifest 补
|
||||
if (record.source !== 'external') {
|
||||
try {
|
||||
const m = JSON.parse(await readFolderFile(record, 'manifest.json'))
|
||||
if (m && typeof m === 'object') {
|
||||
if (typeof m.main === 'string' && m.main) record.main = m.main
|
||||
if (typeof m.style === 'string' && m.style) record.style = m.style
|
||||
if (!record.version && m.version) record.version = String(m.version)
|
||||
}
|
||||
} catch (e) { /* 无 manifest 时按索引里的 main */ }
|
||||
}
|
||||
const main = String(record.main || 'main.js')
|
||||
const code = await readFolderFile(record, main)
|
||||
// 逐文件 sha256:内置以随包 index.json 的值为准(外部/市场安装同理),不一致直接拒载
|
||||
const expect = record.hashes && record.hashes[main]
|
||||
if (expect) {
|
||||
const actual = await sha256Hex(code)
|
||||
if (actual !== String(expect).toLowerCase()) throw new Error('完整性校验失败(' + main + ' 的 sha256 不符)')
|
||||
}
|
||||
if (record.style) {
|
||||
// 只在插件**激活**时注入样式(停用状态不该污染宿主 UI);文本先暂存,激活由宿主触发
|
||||
try { pendingStyles[record.id] = await readFolderFile(record, String(record.style)) } catch (e) { /* 样式缺失不阻断 */ }
|
||||
}
|
||||
const url = global.URL.createObjectURL(new global.Blob([code], { type: 'text/javascript' }))
|
||||
try {
|
||||
await inject({ src: url, error: '插件脚本执行失败(' + record.id + ')' })
|
||||
} finally {
|
||||
setTimeout(function () { try { global.URL.revokeObjectURL(url) } catch (e) { /* ignore */ } }, 30000)
|
||||
}
|
||||
return markResult(record)
|
||||
} catch (err) {
|
||||
record.status = 'error'
|
||||
record.error = String((err && err.message) || err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 注入一段脚本并等它执行完(插件在加载期自我注册)
|
||||
function inject (attrs) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
@@ -114,8 +216,13 @@
|
||||
global.IWBRuntimePluginLoader = {
|
||||
source: source,
|
||||
verifyCode: verifyCode,
|
||||
sha256Hex: sha256Hex,
|
||||
loadBuiltin: loadBuiltin,
|
||||
loadExternal: loadExternal,
|
||||
loadLocal: loadLocal
|
||||
loadLocal: loadLocal,
|
||||
loadFolder: loadFolder,
|
||||
listFolderEntries: listFolderEntries,
|
||||
applyStyleFor: applyStyleFor,
|
||||
removeStyle: removeStyle
|
||||
}
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
Reference in new issue
Block a user