// iwb-plugin: 鲜艺抠图 —— 调用本机抠图服务(POST {apiUrl}/rmbg?crop=)把选中节点的图抠成透明底,写回为新的参考图节点。 // 契约来源:iwb-plaus v3.1.3 的 background.js(二进制进、PNG 字节出,无 token/无 JSON)。 ;(function (global) { 'use strict' var DEFAULT_API = 'http://localhost:30092' var DEFAULT_TIMEOUT = 120000 function toPositive(v, fallback) { var n = Number(v) return Number.isFinite(n) && n > 0 ? n : fallback } function trimUrl(v) { return String(v || DEFAULT_API).trim().replace(/\/+$/, '') || DEFAULT_API } function base64ToBytes(dataUrl) { var s = String(dataUrl || '') var comma = s.indexOf(',') var bin = global.atob(comma >= 0 ? s.slice(comma + 1) : s) var out = new Uint8Array(bin.length) for (var i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i) return out } function bytesToDataUrl(buffer) { var bytes = new Uint8Array(buffer) var chunk = 0x8000 var text = '' for (var i = 0; i < bytes.length; i += chunk) { text += String.fromCharCode.apply(null, bytes.subarray(i, i + chunk)) } return 'data:image/png;base64,' + global.btoa(text) } global.IWBPluginHost.register({ id: 'xianyi-cutout', name: '鲜艺抠图', version: '1.0.3', apiVersion: 1, capabilities: ['commands', 'canvas', 'canvasWrite', 'nodeActions'], settings: { apiUrl: { type: 'text', label: '本地服务地址', desc: 'CSP 仅放行 127.0.0.1 / localhost 的 30092 端口;改到别的 host 或端口会被拦。', default: DEFAULT_API }, crop: { type: 'boolean', label: '抠图后裁掉多余透明像素', desc: '对应接口的 crop 参数,默认开启。', default: true }, timeoutMs: { type: 'number', label: '超时(毫秒)', desc: '原扩展没有超时;这里补上,避免服务无响应时永久挂起。', default: DEFAULT_TIMEOUT }, }, }, function (ctx) { async function cutoutOne(nodeId) { const image = await ctx.canvas.getNodeImage(nodeId) if (!image || !image.dataUrl) throw new Error('该节点没有可编码的图片(请选中带图的参考图或输出节点)') const apiUrl = trimUrl(ctx.settings.get('apiUrl')) const crop = ctx.settings.get('crop') ? 'true' : 'false' const timeoutMs = toPositive(ctx.settings.get('timeoutMs'), DEFAULT_TIMEOUT) const controller = typeof global.AbortController === 'function' ? new global.AbortController() : null const timer = controller ? global.setTimeout(function () { try { controller.abort() } catch (e) { /* ignore */ } }, timeoutMs) : null let response try { response = await global.fetch(apiUrl + '/rmbg?crop=' + crop, { method: 'POST', headers: { 'Content-Type': 'application/octet-stream' }, body: base64ToBytes(image.dataUrl), signal: controller ? controller.signal : undefined, }) } catch (e) { throw new Error('连不上本地抠图服务(' + apiUrl + '):' + String((e && e.message) || e)) } finally { if (timer) global.clearTimeout(timer) } if (!response.ok) { const detail = await response.text().catch(function () { return '' }) throw new Error('抠图服务返回 ' + response.status + (detail ? ':' + String(detail).slice(0, 120) : '')) } const dataUrl = bytesToDataUrl(await response.arrayBuffer()) const selected = ctx.canvas.getSelectedNode() const base = String((selected && selected.name) || 'image').replace(/\.[^.]+$/, '') || 'image' const created = ctx.canvas.createImageNode({ dataUrl: dataUrl, name: base + '_cutout.png' }) if (!created) throw new Error('写回画布失败') return created } ctx.commands.register({ id: 'cutout-selected', title: '抠图:选中节点', icon: 'scissors', run: function () { const selected = ctx.canvas.getSelectedNode() if (!selected) return Promise.reject(new Error('请先选中一个带图的节点')) return cutoutOne(selected.id) }, }) ctx.nodeActions.register({ id: 'cutout-node', title: '抠图', icon: 'scissors', appliesTo: function (node) { return Boolean(node) && node.type === 'ref' }, run: function (node) { return cutoutOne(node.id) }, }) return {} }) })(typeof window !== 'undefined' ? window : globalThis)