From 8b4749ba1f013621f64bde9a26c1ab8b4350d0c4 Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 17 Oct 2026 12:40:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=B2=9C=E8=89=BA=E6=8A=A0=E5=9B=BE?= =?UTF-8?q?=E4=BB=8E=E4=B8=BB=E4=BB=93=E8=BF=81=E5=85=A5=EF=BC=88A2=20?= =?UTF-8?q?=E9=A6=96=E4=B8=AA=E7=9C=9F=E5=AE=9E=E6=8F=92=E4=BB=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ses_eb7dc3f1cffeOL2U2UlEZ4F2A2.json | 10 ++ xianyi-cutout/README.md | 28 +++++ xianyi-cutout/package.json | 7 ++ xianyi-cutout/src/index.js | 107 ++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 .omo/run-continuation/ses_eb7dc3f1cffeOL2U2UlEZ4F2A2.json create mode 100644 xianyi-cutout/README.md create mode 100644 xianyi-cutout/package.json create mode 100644 xianyi-cutout/src/index.js diff --git a/.omo/run-continuation/ses_eb7dc3f1cffeOL2U2UlEZ4F2A2.json b/.omo/run-continuation/ses_eb7dc3f1cffeOL2U2UlEZ4F2A2.json new file mode 100644 index 0000000..b83aa11 --- /dev/null +++ b/.omo/run-continuation/ses_eb7dc3f1cffeOL2U2UlEZ4F2A2.json @@ -0,0 +1,10 @@ +{ + "sessionID": "ses_eb7dc3f1cffeOL2U2UlEZ4F2A2", + "updatedAt": "2026-10-17T04:34:48.099Z", + "sources": { + "background-task": { + "state": "idle", + "updatedAt": "2026-10-17T04:34:48.099Z" + } + } +} \ No newline at end of file diff --git a/xianyi-cutout/README.md b/xianyi-cutout/README.md new file mode 100644 index 0000000..984f8a0 --- /dev/null +++ b/xianyi-cutout/README.md @@ -0,0 +1,28 @@ +# 鲜艺抠图(插件) + +调用本机 **鲜艺抠图** 服务把节点里的图抠成透明底,写回为新的参考图节点。 + +## 服务契约(对齐 iwb-plaus v3.1.3) +``` +POST {apiUrl}/rmbg?crop=true|false # 默认 http://localhost:30092 +Content-Type: application/octet-stream # 原始图片字节;无 token、无 JSON +→ 返回原始 PNG 字节 +``` +比原扩展多做的:**补齐了超时**(`AbortController` + 可配 `timeoutMs`,原版没有任何超时)。 + +## 入口 +- **节点漂浮菜单**「抠图」(选中 ref / out 节点时出现) +- 右键菜单「插件命令 → 抠图:选中节点」 + +## 设置 +| 键 | 说明 | +|---|---| +| `apiUrl` | 本地服务地址。**CSP 仅放行 `127.0.0.1` / `localhost` 的 30092 端口** | +| `crop` | 抠图后裁掉多余透明像素(对应接口 `crop` 参数) | +| `timeoutMs` | 超时毫秒(默认 120000) | + +## 发布 +```bash +cd /Users/qiqi/open/github/iwb-arch +scripts/plugins-publish.sh xianyi-cutout /Users/qiqi/open/github/iwb-plugins/xianyi-cutout/src/index.js 1.0.0 +``` diff --git a/xianyi-cutout/package.json b/xianyi-cutout/package.json new file mode 100644 index 0000000..37f1872 --- /dev/null +++ b/xianyi-cutout/package.json @@ -0,0 +1,7 @@ +{ + "name": "@iwb-plugin/xianyi-cutout", + "private": true, + "version": "1.0.0", + "description": "鲜艺抠图:调用本机 30092 的 /rmbg 把节点图抠成透明底", + "ibwPlugin": { "id": "xianyi-cutout", "entry": "src/index.js", "apiVersion": 1 } +} diff --git a/xianyi-cutout/src/index.js b/xianyi-cutout/src/index.js new file mode 100644 index 0000000..60567d9 --- /dev/null +++ b/xianyi-cutout/src/index.js @@ -0,0 +1,107 @@ +// 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.0', + 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: 'ref', + 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: 'ref', + appliesTo: function (node) { return Boolean(node) && (node.type === 'ref' || node.type === 'out') }, + run: function (node) { return cutoutOne(node.id) }, + }) + + return {} + }) +})(typeof window !== 'undefined' ? window : globalThis)