feat: 鲜艺抠图从主仓迁入(A2 首个真实插件)

This commit is contained in:
dev committed 2026-10-17 12:40:58 +08:00
1 parent 52877add49
commit 8b4749ba1f
4 files changed
+152

No files matched your search

@@ -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"
}
}
}
+28
View File
@@ -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
```
+7
View File
@@ -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 }
}
+107
View File
@@ -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)