@shiqianjiang/ceru-plugin-cli 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/assets/core-contracts.js +1 -0
- package/assets/index.html +94 -93
- package/assets/playground.js +293 -30
- package/assets/sandbox.js +50 -19
- package/assets/style.css +16 -0
- package/dist/dependencies.d.ts +6 -0
- package/dist/dependencies.js +45 -0
- package/dist/dev.js +214 -119
- package/dist/index.js +5 -3
- package/dist/project.js +41 -16
- package/dist/socket-broker.d.ts +23 -0
- package/dist/socket-broker.js +230 -0
- package/package.json +71 -67
- package/schemas/config.schema.json +347 -0
- package/templates/connected-library/js/.vscode/ceru.code-snippets +37 -9
- package/templates/connected-library/js/package.json +2 -2
- package/templates/connected-library/js/src/index.js +7 -5
- package/templates/connected-library/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/connected-library/ts/package.json +2 -2
- package/templates/connected-library/ts/src/index.ts +7 -5
- package/templates/guest-adapter/js/.vscode/ceru.code-snippets +37 -9
- package/templates/guest-adapter/js/package.json +2 -2
- package/templates/guest-adapter/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/guest-adapter/ts/package.json +2 -2
- package/templates/importer/js/.vscode/ceru.code-snippets +37 -9
- package/templates/importer/js/README.md +1 -1
- package/templates/importer/js/ceru.plugin.json +8 -24
- package/templates/importer/js/package.json +2 -2
- package/templates/importer/js/src/index.js +28 -34
- package/templates/importer/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/importer/ts/README.md +1 -1
- package/templates/importer/ts/ceru.plugin.json +8 -24
- package/templates/importer/ts/package.json +2 -2
- package/templates/importer/ts/src/index.ts +28 -34
- package/templates/react/js/.vscode/ceru.code-snippets +37 -9
- package/templates/react/js/package.json +2 -2
- package/templates/react/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/react/ts/package.json +2 -2
- package/templates/snapshot.json +4 -2
- package/templates/source/js/.vscode/ceru.code-snippets +37 -9
- package/templates/source/js/ceru.plugin.json +9 -0
- package/templates/source/js/package.json +2 -2
- package/templates/source/js/src/index.js +29 -26
- package/templates/source/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/source/ts/ceru.plugin.json +9 -0
- package/templates/source/ts/package.json +2 -2
- package/templates/source/ts/src/index.ts +29 -26
- package/templates/vue/js/.vscode/ceru.code-snippets +37 -9
- package/templates/vue/js/package.json +2 -2
- package/templates/vue/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/vue/ts/package.json +2 -2
- package/templates/vue-tsx/js/.vscode/ceru.code-snippets +37 -9
- package/templates/vue-tsx/js/package.json +2 -2
- package/templates/vue-tsx/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/vue-tsx/ts/package.json +2 -2
- package/templates/web-dist/js/.vscode/ceru.code-snippets +37 -9
- package/templates/web-dist/js/package.json +2 -2
- package/templates/web-dist/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/web-dist/ts/package.json +2 -2
- package/templates/web-surface/js/.vscode/ceru.code-snippets +37 -9
- package/templates/web-surface/js/ceru.plugin.json +9 -0
- package/templates/web-surface/js/package.json +2 -2
- package/templates/web-surface/js/src/view.js +4 -1
- package/templates/web-surface/js/ui/search-note.css +3 -0
- package/templates/web-surface/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/web-surface/ts/ceru.plugin.json +9 -0
- package/templates/web-surface/ts/package.json +2 -2
- package/templates/web-surface/ts/src/view.ts +4 -1
- package/templates/web-surface/ts/ui/search-note.css +3 -0
package/assets/playground.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertContentPage,
|
|
3
|
+
assertLyricsDocument,
|
|
4
|
+
assertResolveResult,
|
|
5
|
+
PERMISSION_GROUPS,
|
|
6
|
+
permissionGroup,
|
|
7
|
+
} from './core-contracts.js'
|
|
1
8
|
const $ = (id) => document.getElementById(id)
|
|
2
9
|
let state,
|
|
3
10
|
revision = 0,
|
|
@@ -44,6 +51,8 @@ function post(frame, type, data) {
|
|
|
44
51
|
frame.element.contentWindow?.postMessage({ type, data, generation: frame.generation }, '*')
|
|
45
52
|
}
|
|
46
53
|
function removeFrames() {
|
|
54
|
+
void api('rpc', { method: 'sockets.closeAll' }).catch(() => {})
|
|
55
|
+
document.getElementById('host-playlist-import')?.remove()
|
|
47
56
|
for (const frame of frames.values()) frame.element.remove()
|
|
48
57
|
frames.clear()
|
|
49
58
|
registrations.clear()
|
|
@@ -52,13 +61,24 @@ function removeFrames() {
|
|
|
52
61
|
call.reject(new Error('Plugin reloaded or stopped'))
|
|
53
62
|
}
|
|
54
63
|
calls.clear()
|
|
64
|
+
currentInvocation = null
|
|
65
|
+
$('results').replaceChildren()
|
|
66
|
+
$('result-detail').textContent = ''
|
|
67
|
+
document.getElementById('media-preview')?.remove()
|
|
55
68
|
drawRegistrations()
|
|
56
69
|
}
|
|
57
|
-
function mount(moduleId, kind, container) {
|
|
70
|
+
function mount(moduleId, kind, container, mountInfo) {
|
|
58
71
|
const element = document.createElement('iframe')
|
|
59
72
|
element.setAttribute('sandbox', 'allow-scripts')
|
|
60
73
|
element.src = '/sandbox.html?module=' + encodeURIComponent(moduleId)
|
|
61
|
-
const frame = {
|
|
74
|
+
const frame = {
|
|
75
|
+
element,
|
|
76
|
+
moduleId,
|
|
77
|
+
kind,
|
|
78
|
+
mountInfo,
|
|
79
|
+
generation: crypto.randomUUID(),
|
|
80
|
+
active: false,
|
|
81
|
+
}
|
|
62
82
|
frames.set(moduleId, frame)
|
|
63
83
|
container.append(element)
|
|
64
84
|
return frame
|
|
@@ -70,9 +90,52 @@ function start() {
|
|
|
70
90
|
if (state.manifest.modules.logic) mount(state.manifest.modules.logic.entry, 'logic', $('runners'))
|
|
71
91
|
else $('status').textContent = '无后台入口'
|
|
72
92
|
if (viewId) showView(viewId)
|
|
93
|
+
updateSearchControls()
|
|
94
|
+
}
|
|
95
|
+
function hasProtocol(providerId, protocol) {
|
|
96
|
+
return state?.manifest.contributes?.providers?.some(
|
|
97
|
+
(provider) => provider.id === providerId && provider.protocols?.includes(protocol),
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
function supportsMethod(item, method) {
|
|
101
|
+
const protocol = method === 'search' ? 'music.search@1' : 'music.resolve@1'
|
|
102
|
+
return item?.kind === 'provider' && Array.isArray(item.methods) &&
|
|
103
|
+
item.methods.includes('tracks.' + method) && hasProtocol(item.id, protocol)
|
|
104
|
+
}
|
|
105
|
+
function isAvailable(item, method) {
|
|
106
|
+
return !stopped && supportsMethod(item, method) && item.frame.active &&
|
|
107
|
+
!item.frame.failed && frames.get(item.frame.moduleId) === item.frame
|
|
108
|
+
}
|
|
109
|
+
function updateSearchControls() {
|
|
110
|
+
const item = registrations.get('provider:' + $('provider').value)
|
|
111
|
+
const available = isAvailable(item, 'search')
|
|
112
|
+
$('provider').disabled = !available
|
|
113
|
+
$('query').disabled = !available
|
|
114
|
+
$('search').disabled = !available
|
|
115
|
+
$('cancel').disabled = !currentInvocation || !calls.has(currentInvocation.id)
|
|
116
|
+
const logic = [...frames.values()].find((frame) => frame.kind === 'logic')
|
|
117
|
+
let message
|
|
118
|
+
if (stopped) message = '插件已停止,点击“重新运行”后继续调试。'
|
|
119
|
+
else if (logic?.failed) message = '后台模块运行失败,请查看右侧日志并修复后重新运行。'
|
|
120
|
+
else if (!state?.manifest.contributes?.providers?.some((p) => p.protocols?.includes('music.search@1')))
|
|
121
|
+
message = '当前插件未提供搜索能力。页面插件可点击左侧“界面与兼容模块”中的页面进行预览;命令在“能力注册”中调用。'
|
|
122
|
+
else if (!logic) message = '插件声明了搜索能力,但没有后台入口;请检查 Manifest 的 modules.logic。'
|
|
123
|
+
else if (!logic.active) message = '后台模块正在启动,搜索将在注册完成后启用。'
|
|
124
|
+
else if (!available) message = '插件声明了搜索能力,但尚未注册 search 实现;请检查后台的 ctx.providers.register(...)。'
|
|
125
|
+
else message = '搜索来源已就绪。'
|
|
126
|
+
$('search-status').textContent = message
|
|
127
|
+
if (!$('results').children.length) {
|
|
128
|
+
const empty = document.createElement('div')
|
|
129
|
+
empty.className = 'empty'
|
|
130
|
+
empty.dataset.capabilityHint = 'true'
|
|
131
|
+
$('results').append(empty)
|
|
132
|
+
}
|
|
133
|
+
const empty = $('results').querySelector('[data-capability-hint]')
|
|
134
|
+
if (empty) empty.textContent = available ? '输入关键词后点击“调用搜索”。' : message
|
|
73
135
|
}
|
|
74
136
|
function drawRegistrations() {
|
|
75
137
|
$('registrations').replaceChildren()
|
|
138
|
+
const selected = $('provider').value
|
|
76
139
|
$('provider').replaceChildren()
|
|
77
140
|
for (const [key, item] of registrations) {
|
|
78
141
|
const row = document.createElement('div')
|
|
@@ -85,10 +148,18 @@ function drawRegistrations() {
|
|
|
85
148
|
img.src = iconUrl(meta?.icon?.name)
|
|
86
149
|
row.append(img)
|
|
87
150
|
row.append(document.createTextNode(meta?.name || item.id))
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
151
|
+
if (supportsMethod(item, 'search')) {
|
|
152
|
+
const option = document.createElement('option')
|
|
153
|
+
option.value = item.id
|
|
154
|
+
option.textContent = meta?.name || item.id
|
|
155
|
+
$('provider').append(option)
|
|
156
|
+
}
|
|
157
|
+
} else if (item.kind === 'playlist-importer') {
|
|
158
|
+
const button = document.createElement('button')
|
|
159
|
+
const meta = state.manifest.contributes?.playlistImporters?.find((p) => p.id === item.id)
|
|
160
|
+
button.textContent = '导入 · ' + (meta?.title || item.id)
|
|
161
|
+
button.onclick = () => openPlaylistImport(item.id)
|
|
162
|
+
row.append(button)
|
|
92
163
|
} else {
|
|
93
164
|
const button = document.createElement('button')
|
|
94
165
|
button.textContent = item.id
|
|
@@ -100,7 +171,17 @@ function drawRegistrations() {
|
|
|
100
171
|
}
|
|
101
172
|
$('registrations').append(row)
|
|
102
173
|
}
|
|
103
|
-
if (
|
|
174
|
+
if ([...$('provider').options].some((option) => option.value === selected))
|
|
175
|
+
$('provider').value = selected
|
|
176
|
+
if (!$('provider').options.length) {
|
|
177
|
+
const option = document.createElement('option')
|
|
178
|
+
option.value = ''
|
|
179
|
+
option.textContent = '无可用搜索来源'
|
|
180
|
+
$('provider').append(option)
|
|
181
|
+
}
|
|
182
|
+
if (!registrations.size)
|
|
183
|
+
$('registrations').textContent = stopped ? '插件已停止' : '等待后台模块注册命令或能力'
|
|
184
|
+
updateSearchControls()
|
|
104
185
|
}
|
|
105
186
|
function invoke(frame, kind, target, method, args) {
|
|
106
187
|
const id = 'call-' + ++sequence
|
|
@@ -109,25 +190,39 @@ function invoke(frame, kind, target, method, args) {
|
|
|
109
190
|
return new Promise((resolve, reject) => {
|
|
110
191
|
calls.set(id, {
|
|
111
192
|
frame,
|
|
112
|
-
resolve
|
|
193
|
+
resolve: (value) => {
|
|
194
|
+
try {
|
|
195
|
+
if (
|
|
196
|
+
kind === 'playlist-importer' ||
|
|
197
|
+
(kind === 'provider' &&
|
|
198
|
+
['tracks.search', 'playlists.search', 'playlists.categories', 'playlists.list', 'playlists.get', 'charts.list', 'charts.getTracks'].includes(method))
|
|
199
|
+
)
|
|
200
|
+
assertContentPage(value)
|
|
201
|
+
if (kind === 'provider' && method === 'tracks.lyrics') assertLyricsDocument(value)
|
|
202
|
+
if (kind === 'provider' && method === 'tracks.resolve') assertResolveResult(value)
|
|
203
|
+
resolve(value)
|
|
204
|
+
} catch (error) { reject(error) }
|
|
205
|
+
},
|
|
113
206
|
reject,
|
|
114
207
|
timer: setTimeout(() => {
|
|
115
208
|
calls.delete(id)
|
|
209
|
+
updateSearchControls()
|
|
116
210
|
post(frame, 'cancel', { id })
|
|
117
211
|
reject(new Error('Invocation timed out'))
|
|
118
212
|
}, 20000),
|
|
119
213
|
})
|
|
214
|
+
updateSearchControls()
|
|
120
215
|
post(frame, 'invoke', { id, kind, target, method, args })
|
|
121
216
|
})
|
|
122
217
|
}
|
|
123
218
|
function showResult(value) {
|
|
124
219
|
$('result-detail').textContent = JSON.stringify(value ?? null, null, 2)
|
|
125
220
|
document.getElementById('media-preview')?.remove()
|
|
126
|
-
if (value?.ok && value.
|
|
221
|
+
if (value?.ok && typeof value.url === 'string') {
|
|
127
222
|
const audio = document.createElement('audio')
|
|
128
223
|
audio.id = 'media-preview'
|
|
129
224
|
audio.controls = true
|
|
130
|
-
audio.src =
|
|
225
|
+
audio.src = value.url
|
|
131
226
|
$('result-detail').after(audio)
|
|
132
227
|
}
|
|
133
228
|
return value
|
|
@@ -137,6 +232,43 @@ function runAction(action, input) {
|
|
|
137
232
|
if (!item) return Promise.reject(new Error('Action is not registered: ' + action))
|
|
138
233
|
return invoke(item.frame, 'action', action, '', [input])
|
|
139
234
|
}
|
|
235
|
+
|
|
236
|
+
// A single Host-owned preview for all importers. Plugins contribute data callbacks only.
|
|
237
|
+
// The desktop application maps the same declarations to its existing import dialog.
|
|
238
|
+
function openPlaylistImport(importerId, initialValue = '') {
|
|
239
|
+
const item = registrations.get('playlist-importer:' + importerId)
|
|
240
|
+
const meta = state.manifest.contributes?.playlistImporters?.find((p) => p.id === importerId)
|
|
241
|
+
if (!item || !meta) throw new Error('Playlist importer is not registered')
|
|
242
|
+
document.getElementById('host-playlist-import')?.remove()
|
|
243
|
+
const dialog = document.createElement('dialog')
|
|
244
|
+
dialog.id = 'host-playlist-import'
|
|
245
|
+
const title = document.createElement('h3'); title.textContent = meta.title
|
|
246
|
+
const description = document.createElement('p'); description.textContent = meta.description || '粘贴歌单链接或 ID'
|
|
247
|
+
const input = document.createElement('input'); input.placeholder = meta.placeholder || '歌单链接或 ID'; input.value = initialValue
|
|
248
|
+
const load = document.createElement('button'); load.textContent = '读取歌曲'
|
|
249
|
+
const close = document.createElement('button'); close.textContent = '关闭'; close.onclick = () => dialog.close()
|
|
250
|
+
const detail = document.createElement('pre')
|
|
251
|
+
const hint = document.createElement('p')
|
|
252
|
+
hint.textContent = '独立开发 Host 可预览解析结果;保存到软件本地/云歌单需要连接澜音正式 Host。'
|
|
253
|
+
let cursor
|
|
254
|
+
let previousValue
|
|
255
|
+
load.onclick = async () => {
|
|
256
|
+
if (!input.value.trim()) { detail.textContent = '请输入歌单链接或 ID'; return }
|
|
257
|
+
if (input.value !== previousValue) cursor = undefined
|
|
258
|
+
load.disabled = true
|
|
259
|
+
try {
|
|
260
|
+
const result = await invoke(item.frame, 'playlist-importer', importerId, 'getTracks', [{ value: input.value.trim(), cursor, limit: 100 }])
|
|
261
|
+
previousValue = input.value
|
|
262
|
+
cursor = result.nextCursor
|
|
263
|
+
detail.textContent = JSON.stringify(result, null, 2)
|
|
264
|
+
load.textContent = cursor ? '读取下一页' : '重新读取'
|
|
265
|
+
} catch (error) { detail.textContent = error.message }
|
|
266
|
+
finally { load.disabled = false }
|
|
267
|
+
}
|
|
268
|
+
dialog.append(title, description, input, load, close, hint, detail)
|
|
269
|
+
document.body.append(dialog); dialog.showModal()
|
|
270
|
+
dialog.addEventListener('close', () => dialog.remove(), { once: true })
|
|
271
|
+
}
|
|
140
272
|
function renderSchema(node, values, container) {
|
|
141
273
|
if (!node || typeof node !== 'object') return
|
|
142
274
|
if (['section', 'stack', 'form', 'grid'].includes(node.type)) {
|
|
@@ -160,6 +292,13 @@ function renderSchema(node, values, container) {
|
|
|
160
292
|
container.append(button)
|
|
161
293
|
return
|
|
162
294
|
}
|
|
295
|
+
if (node.type === 'host-content') {
|
|
296
|
+
const content = document.createElement('div')
|
|
297
|
+
content.className = 'host-content-preview'
|
|
298
|
+
content.textContent = '宿主原内容(正式 Host 在这里保持现有组件和交互)'
|
|
299
|
+
container.append(content)
|
|
300
|
+
return
|
|
301
|
+
}
|
|
163
302
|
if (['text-input', 'input', 'number', 'toggle', 'host-credential'].includes(node.type)) {
|
|
164
303
|
const label = document.createElement('label')
|
|
165
304
|
label.textContent = node.label || node.bind || ''
|
|
@@ -181,7 +320,7 @@ function renderSchema(node, values, container) {
|
|
|
181
320
|
node.text || node.label || (node.bind ? String(values[node.bind] ?? '') : '[' + node.type + ']')
|
|
182
321
|
container.append(text)
|
|
183
322
|
}
|
|
184
|
-
function showView(id) {
|
|
323
|
+
function showView(id, mountInfo = { kind: 'page' }) {
|
|
185
324
|
viewId = id
|
|
186
325
|
for (const [key, frame] of frames)
|
|
187
326
|
if (frame.kind !== 'logic') {
|
|
@@ -190,7 +329,7 @@ function showView(id) {
|
|
|
190
329
|
}
|
|
191
330
|
$('preview').replaceChildren()
|
|
192
331
|
const view = state.manifest.modules.surfaces?.find((v) => v.id === id)
|
|
193
|
-
if (view?.kind === 'web') mount(view.entry, 'web', $('preview'))
|
|
332
|
+
if (view?.kind === 'web') mount(view.entry, 'web', $('preview'), mountInfo)
|
|
194
333
|
else if (view?.kind === 'schema')
|
|
195
334
|
renderSchema(
|
|
196
335
|
state.resources[view.entry]?.value?.root,
|
|
@@ -216,19 +355,63 @@ function drawStatic() {
|
|
|
216
355
|
button.onclick = () => showView(view.id)
|
|
217
356
|
$('views').append(button)
|
|
218
357
|
}
|
|
358
|
+
for (const section of state.manifest.contributes?.homeSections || []) {
|
|
359
|
+
const button = document.createElement('button')
|
|
360
|
+
button.textContent = '首页 · ' + section.title + ' · ' + section.kind
|
|
361
|
+
button.disabled = !section.view
|
|
362
|
+
if (section.view) button.onclick = () => showView(section.view)
|
|
363
|
+
else button.title = '正式 Host 将复用软件现有页面'
|
|
364
|
+
$('views').append(button)
|
|
365
|
+
}
|
|
366
|
+
for (const extension of state.manifest.contributes?.uiExtensions || []) {
|
|
367
|
+
const button = document.createElement('button')
|
|
368
|
+
button.textContent = extension.slot + ' · ' + extension.mode
|
|
369
|
+
button.onclick = () =>
|
|
370
|
+
showView(extension.view, {
|
|
371
|
+
kind: 'slot',
|
|
372
|
+
slot: extension.slot,
|
|
373
|
+
mode: extension.mode,
|
|
374
|
+
})
|
|
375
|
+
$('views').append(button)
|
|
376
|
+
}
|
|
219
377
|
$('permissions').replaceChildren()
|
|
378
|
+
const renderedGroups = new Set()
|
|
220
379
|
for (const permission of state.manifest.permissions || []) {
|
|
380
|
+
const group = permissionGroup(permission.name) || 'other'
|
|
381
|
+
if (!renderedGroups.has(group)) {
|
|
382
|
+
renderedGroups.add(group)
|
|
383
|
+
const declared = (state.manifest.permissions || []).filter(
|
|
384
|
+
(item) => (permissionGroup(item.name) || 'other') === group,
|
|
385
|
+
)
|
|
386
|
+
const header = document.createElement('div')
|
|
387
|
+
header.className = 'permission-group'
|
|
388
|
+
const title = document.createElement('strong')
|
|
389
|
+
title.textContent = PERMISSION_GROUPS[group]?.title || '其他权限'
|
|
390
|
+
const button = document.createElement('button')
|
|
391
|
+
const allGranted = declared.every((item) => state.grants[item.key])
|
|
392
|
+
button.textContent = allGranted ? '撤销整组' : '授予整组'
|
|
393
|
+
button.onclick = async () => {
|
|
394
|
+
try {
|
|
395
|
+
for (const item of declared)
|
|
396
|
+
await api('grant', {
|
|
397
|
+
key: item.key,
|
|
398
|
+
allow: !allGranted,
|
|
399
|
+
})
|
|
400
|
+
state = await api('state')
|
|
401
|
+
drawStatic()
|
|
402
|
+
} catch (error) {
|
|
403
|
+
log('error', error.message)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
header.append(title, button)
|
|
407
|
+
$('permissions').append(header)
|
|
408
|
+
}
|
|
221
409
|
const row = document.createElement('div')
|
|
222
410
|
row.className = 'permission'
|
|
223
411
|
const title = document.createElement('strong')
|
|
224
412
|
title.textContent = permission.key
|
|
225
413
|
const reason = document.createElement('small')
|
|
226
414
|
reason.textContent = permission.reason
|
|
227
|
-
const input = document.createElement('input')
|
|
228
|
-
input.placeholder = '授权的确切 origin'
|
|
229
|
-
input.value =
|
|
230
|
-
state.grants[permission.key]?.origin ||
|
|
231
|
-
(typeof permission.scope?.origin === 'string' ? permission.scope.origin : '')
|
|
232
415
|
const button = document.createElement('button')
|
|
233
416
|
button.textContent = state.grants[permission.key] ? '撤销' : '授予'
|
|
234
417
|
button.onclick = async () => {
|
|
@@ -236,7 +419,6 @@ function drawStatic() {
|
|
|
236
419
|
await api('grant', {
|
|
237
420
|
key: permission.key,
|
|
238
421
|
allow: !state.grants[permission.key],
|
|
239
|
-
origin: input.value,
|
|
240
422
|
})
|
|
241
423
|
state = await api('state')
|
|
242
424
|
drawStatic()
|
|
@@ -245,12 +427,25 @@ function drawStatic() {
|
|
|
245
427
|
}
|
|
246
428
|
}
|
|
247
429
|
row.append(title, reason)
|
|
248
|
-
if (permission.name.startsWith('network.')) row.append(input)
|
|
249
430
|
row.append(button)
|
|
250
431
|
$('permissions').append(row)
|
|
251
432
|
}
|
|
252
433
|
if (!(state.manifest.permissions || []).length)
|
|
253
434
|
$('permissions').textContent = '此插件未声明额外权限'
|
|
435
|
+
for (const style of document.querySelectorAll('style[data-plugin-global-style]')) style.remove()
|
|
436
|
+
const hasGlobalStyleGrant = (state.manifest.permissions || []).some(
|
|
437
|
+
(permission) => permission.name === 'ui.styles.global' && state.grants[permission.key],
|
|
438
|
+
)
|
|
439
|
+
if (hasGlobalStyleGrant)
|
|
440
|
+
for (const contribution of state.manifest.contributes?.styles || []) {
|
|
441
|
+
if (contribution.scope !== 'application') continue
|
|
442
|
+
const resource = state.resources[contribution.resource]
|
|
443
|
+
if (resource?.type !== 'text') continue
|
|
444
|
+
const style = document.createElement('style')
|
|
445
|
+
style.dataset.pluginGlobalStyle = contribution.id
|
|
446
|
+
style.textContent = resource.value
|
|
447
|
+
document.head.append(style)
|
|
448
|
+
}
|
|
254
449
|
$('icons').replaceChildren()
|
|
255
450
|
for (const name of Object.keys(state.catalog.icons)) {
|
|
256
451
|
const card = document.createElement('div')
|
|
@@ -283,6 +478,7 @@ window.addEventListener('message', async (event) => {
|
|
|
283
478
|
manifest: state.manifest,
|
|
284
479
|
catalog: state.catalog,
|
|
285
480
|
resources: state.resources,
|
|
481
|
+
mount: frame.mountInfo,
|
|
286
482
|
})
|
|
287
483
|
return
|
|
288
484
|
}
|
|
@@ -290,10 +486,18 @@ window.addEventListener('message', async (event) => {
|
|
|
290
486
|
const data = message.data
|
|
291
487
|
if (message.type === 'active') {
|
|
292
488
|
frame.active = true
|
|
489
|
+
frame.failed = false
|
|
293
490
|
$('status').textContent = '运行中'
|
|
294
491
|
log('info', frame.moduleId + ' activated')
|
|
492
|
+
drawRegistrations()
|
|
295
493
|
}
|
|
296
494
|
if (message.type === 'failed') {
|
|
495
|
+
frame.failed = true
|
|
496
|
+
frame.active = false
|
|
497
|
+
for (const [key, item] of registrations) {
|
|
498
|
+
if (item.frame === frame) registrations.delete(key)
|
|
499
|
+
}
|
|
500
|
+
drawRegistrations()
|
|
297
501
|
$('status').textContent = '运行失败'
|
|
298
502
|
log('error', data.message)
|
|
299
503
|
}
|
|
@@ -305,6 +509,8 @@ window.addEventListener('message', async (event) => {
|
|
|
305
509
|
const valid =
|
|
306
510
|
data.kind === 'provider'
|
|
307
511
|
? state.manifest.contributes?.providers?.some((p) => p.id === data.id)
|
|
512
|
+
: data.kind === 'playlist-importer'
|
|
513
|
+
? state.manifest.contributes?.playlistImporters?.some((p) => p.id === data.id)
|
|
308
514
|
: state.manifest.contributes?.commands?.some((p) => p.action === data.id)
|
|
309
515
|
if (valid) {
|
|
310
516
|
registrations.set(data.kind + ':' + data.id, { ...data, frame })
|
|
@@ -329,6 +535,7 @@ window.addEventListener('message', async (event) => {
|
|
|
329
535
|
if (call?.frame === frame) {
|
|
330
536
|
clearTimeout(call.timer)
|
|
331
537
|
calls.delete(data.id)
|
|
538
|
+
updateSearchControls()
|
|
332
539
|
data.error ? call.reject(new Error(data.error)) : call.resolve(data.value)
|
|
333
540
|
}
|
|
334
541
|
}
|
|
@@ -336,9 +543,55 @@ window.addEventListener('message', async (event) => {
|
|
|
336
543
|
try {
|
|
337
544
|
let value
|
|
338
545
|
if (data.method === 'surface.invoke' && frame.kind === 'web')
|
|
339
|
-
value = await runAction(data.data.action, data.data.input)
|
|
546
|
+
value = showResult(await runAction(data.data.action, data.data.input))
|
|
340
547
|
else if (frame.kind !== 'logic')
|
|
341
548
|
throw new Error('This Surface/Guest cannot call a logic Host API directly')
|
|
549
|
+
else if (data.method === 'permissions.requestGroup') {
|
|
550
|
+
const declared = (state.manifest.permissions || []).filter(
|
|
551
|
+
(permission) =>
|
|
552
|
+
permissionGroup(permission.name) === data.data.group &&
|
|
553
|
+
(!data.data.keys || data.data.keys.includes(permission.key)),
|
|
554
|
+
)
|
|
555
|
+
if (!declared.length) throw new Error('No permissions are declared in this group')
|
|
556
|
+
const allowed = confirm(
|
|
557
|
+
'开发权限组申请:' +
|
|
558
|
+
data.data.group +
|
|
559
|
+
'\n\n' +
|
|
560
|
+
declared.map((permission) => permission.reason).join('\n'),
|
|
561
|
+
)
|
|
562
|
+
if (allowed) {
|
|
563
|
+
for (const declaration of declared) {
|
|
564
|
+
await api('grant', {
|
|
565
|
+
key: declaration.key,
|
|
566
|
+
allow: true,
|
|
567
|
+
})
|
|
568
|
+
}
|
|
569
|
+
state = await api('state')
|
|
570
|
+
drawStatic()
|
|
571
|
+
}
|
|
572
|
+
value = {
|
|
573
|
+
group: data.data.group,
|
|
574
|
+
status: allowed ? 'granted' : 'denied',
|
|
575
|
+
grants: declared.map((permission) => ({
|
|
576
|
+
key: permission.key,
|
|
577
|
+
name: permission.name,
|
|
578
|
+
group: data.data.group,
|
|
579
|
+
scope: permission.scope || {},
|
|
580
|
+
status: allowed ? 'granted' : 'denied',
|
|
581
|
+
})),
|
|
582
|
+
}
|
|
583
|
+
} else if (data.method === 'ui.dialogs.confirm')
|
|
584
|
+
value = confirm(data.data.message || data.data.title || '确认操作?')
|
|
585
|
+
else if (data.method === 'ui.dialogs.prompt')
|
|
586
|
+
value = prompt(data.data.label || data.data.title || '请输入', data.data.value || '')
|
|
587
|
+
else if (data.method === 'ui.navigation.open') {
|
|
588
|
+
log('info', { navigation: data.data })
|
|
589
|
+
value = null
|
|
590
|
+
}
|
|
591
|
+
else if (data.method === 'ui.playlistImport.open') {
|
|
592
|
+
openPlaylistImport(data.data.importerId, data.data.initialValue)
|
|
593
|
+
value = null
|
|
594
|
+
}
|
|
342
595
|
else if (data.method === 'permissions.request') {
|
|
343
596
|
const declaration = state.manifest.permissions?.find((p) => p.key === data.data.key)
|
|
344
597
|
if (!declaration) value = { status: 'undeclared' }
|
|
@@ -347,15 +600,12 @@ window.addEventListener('message', async (event) => {
|
|
|
347
600
|
'开发权限申请:' +
|
|
348
601
|
declaration.key +
|
|
349
602
|
'\n' +
|
|
350
|
-
declaration.reason
|
|
351
|
-
'\n若需要动态 origin,请先在权限面板中指定。',
|
|
603
|
+
declaration.reason,
|
|
352
604
|
)
|
|
353
605
|
if (allowed) {
|
|
354
|
-
const scope = data.data.scope?.origin
|
|
355
606
|
await api('grant', {
|
|
356
607
|
key: declaration.key,
|
|
357
608
|
allow: true,
|
|
358
|
-
origin: typeof scope === 'string' ? scope : undefined,
|
|
359
609
|
})
|
|
360
610
|
state = await api('state')
|
|
361
611
|
drawStatic()
|
|
@@ -370,13 +620,17 @@ window.addEventListener('message', async (event) => {
|
|
|
370
620
|
}
|
|
371
621
|
})
|
|
372
622
|
$('search').onclick = async () => {
|
|
623
|
+
const id = $('provider').value
|
|
624
|
+
const item = registrations.get('provider:' + id)
|
|
625
|
+
if (!isAvailable(item, 'search')) {
|
|
626
|
+
updateSearchControls()
|
|
627
|
+
return
|
|
628
|
+
}
|
|
373
629
|
try {
|
|
374
|
-
const
|
|
375
|
-
item = registrations.get('provider:' + id)
|
|
376
|
-
if (!item) throw new Error('尚未注册 Provider')
|
|
377
|
-
const result = await invoke(item.frame, 'provider', id, 'search', [
|
|
630
|
+
const result = await invoke(item.frame, 'provider', id, 'tracks.search', [
|
|
378
631
|
{ query: $('query').value, kinds: ['track'], filters: {}, limit: 20 },
|
|
379
632
|
])
|
|
633
|
+
if (!isAvailable(item, 'search')) return
|
|
380
634
|
showResult(result)
|
|
381
635
|
$('results').replaceChildren()
|
|
382
636
|
for (const track of result.items || []) {
|
|
@@ -386,8 +640,11 @@ $('search').onclick = async () => {
|
|
|
386
640
|
title.textContent = track.title
|
|
387
641
|
const button = document.createElement('button')
|
|
388
642
|
button.textContent = '调用解析'
|
|
643
|
+
button.disabled = !isAvailable(item, 'resolve')
|
|
644
|
+
if (button.disabled) button.title = '此来源未提供播放解析能力'
|
|
389
645
|
button.onclick = () =>
|
|
390
|
-
|
|
646
|
+
isAvailable(item, 'resolve') &&
|
|
647
|
+
invoke(item.frame, 'provider', id, 'tracks.resolve', [track.ref, undefined])
|
|
391
648
|
.then(showResult)
|
|
392
649
|
.catch((e) => log('error', e.message))
|
|
393
650
|
row.append(title, button)
|
|
@@ -395,9 +652,10 @@ $('search').onclick = async () => {
|
|
|
395
652
|
}
|
|
396
653
|
if (!(result.items || []).length) $('results').textContent = '没有结果'
|
|
397
654
|
} catch (e) {
|
|
398
|
-
log('error', e.message)
|
|
655
|
+
if (isAvailable(item, 'search')) log('error', e.message)
|
|
399
656
|
}
|
|
400
657
|
}
|
|
658
|
+
$('provider').onchange = updateSearchControls
|
|
401
659
|
$('cancel').onclick = () => {
|
|
402
660
|
if (currentInvocation) post(currentInvocation.frame, 'cancel', { id: currentInvocation.id })
|
|
403
661
|
}
|
|
@@ -425,8 +683,13 @@ async function poll() {
|
|
|
425
683
|
state = next
|
|
426
684
|
revision = next.revision
|
|
427
685
|
drawStatic()
|
|
686
|
+
for (const warning of next.migrationWarnings || []) log('warn', warning)
|
|
428
687
|
if (!stopped) start()
|
|
429
688
|
}
|
|
689
|
+
const { events } = await api('socket-events')
|
|
690
|
+
for (const event of events) {
|
|
691
|
+
for (const frame of frames.values()) if (frame.kind === 'logic' && frame.active) post(frame, 'socket-event', event)
|
|
692
|
+
}
|
|
430
693
|
} catch (e) {
|
|
431
694
|
$('status').textContent = 'Host 已断开'
|
|
432
695
|
log('error', e.message)
|