@shiqianjiang/ceru-plugin-cli 0.3.16 → 3.0.0

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.
Files changed (302) hide show
  1. package/README.md +26 -7
  2. package/assets/compatibility.json +18 -0
  3. package/dist/compatibility.d.ts +24 -0
  4. package/dist/compatibility.js +119 -0
  5. package/dist/component.d.ts +6 -0
  6. package/dist/component.js +60 -0
  7. package/dist/dev-music.d.ts +19 -0
  8. package/dist/dev-music.js +81 -0
  9. package/dist/dev-plugin-services.d.ts +10 -0
  10. package/dist/dev-plugin-services.js +67 -0
  11. package/dist/dev.d.ts +1 -9
  12. package/dist/dev.js +53 -713
  13. package/dist/frameworks.d.ts +5 -2
  14. package/dist/frameworks.js +43 -6
  15. package/dist/index.d.ts +1 -1
  16. package/dist/index.js +54 -23
  17. package/dist/init.js +3 -3
  18. package/dist/migrate.d.ts +3 -0
  19. package/dist/migrate.js +109 -0
  20. package/dist/project.d.ts +5 -2
  21. package/dist/project.js +131 -43
  22. package/dist/test-hooks.d.ts +1 -0
  23. package/dist/test-hooks.js +209 -0
  24. package/dist/test-services.d.ts +4 -0
  25. package/dist/test-services.js +54 -0
  26. package/dist/watch-vue-types.d.ts +17 -0
  27. package/dist/watch-vue-types.js +145 -0
  28. package/package.json +10 -6
  29. package/schemas/config.schema.json +954 -452
  30. package/templates/account-qrcode/js/.vscode/extensions.json +3 -0
  31. package/templates/account-qrcode/js/.vscode/launch.json +19 -0
  32. package/templates/account-qrcode/js/.vscode/settings.json +20 -0
  33. package/templates/account-qrcode/js/.vscode/tasks.json +43 -0
  34. package/templates/account-qrcode/js/README.md +15 -0
  35. package/templates/account-qrcode/js/_gitignore +6 -0
  36. package/templates/account-qrcode/js/ceru-plugin.code-workspace +8 -0
  37. package/templates/account-qrcode/js/ceru.plugin.json +62 -0
  38. package/templates/account-qrcode/js/package.json +24 -0
  39. package/templates/account-qrcode/js/src/Login.vue +25 -0
  40. package/templates/account-qrcode/js/src/index.js +25 -0
  41. package/templates/account-qrcode/js/src/login-html.js +23 -0
  42. package/templates/account-qrcode/js/tsconfig.json +17 -0
  43. package/templates/account-qrcode/ts/.vscode/extensions.json +3 -0
  44. package/templates/account-qrcode/ts/.vscode/launch.json +19 -0
  45. package/templates/account-qrcode/ts/.vscode/settings.json +20 -0
  46. package/templates/account-qrcode/ts/.vscode/tasks.json +43 -0
  47. package/templates/account-qrcode/ts/README.md +15 -0
  48. package/templates/account-qrcode/ts/_gitignore +6 -0
  49. package/templates/account-qrcode/ts/ceru-plugin.code-workspace +8 -0
  50. package/templates/account-qrcode/ts/ceru.plugin.json +62 -0
  51. package/templates/account-qrcode/ts/package.json +24 -0
  52. package/templates/account-qrcode/ts/src/Login.vue +25 -0
  53. package/templates/account-qrcode/ts/src/index.ts +25 -0
  54. package/templates/account-qrcode/ts/src/login-html.ts +23 -0
  55. package/templates/account-qrcode/ts/tsconfig.json +17 -0
  56. package/templates/connected-library/js/.vscode/launch.json +12 -29
  57. package/templates/connected-library/js/.vscode/tasks.json +2 -3
  58. package/templates/connected-library/js/README.md +26 -12
  59. package/templates/connected-library/js/ceru.plugin.json +122 -50
  60. package/templates/connected-library/js/package.json +5 -5
  61. package/templates/connected-library/js/src/index.js +46 -5
  62. package/templates/connected-library/js/src/settings.js +28 -0
  63. package/templates/connected-library/ts/.vscode/launch.json +12 -29
  64. package/templates/connected-library/ts/.vscode/tasks.json +2 -3
  65. package/templates/connected-library/ts/README.md +26 -12
  66. package/templates/connected-library/ts/ceru.plugin.json +122 -50
  67. package/templates/connected-library/ts/package.json +5 -5
  68. package/templates/connected-library/ts/src/index.ts +46 -5
  69. package/templates/connected-library/ts/src/settings.ts +28 -0
  70. package/templates/encrypted-source/README.md +86 -0
  71. package/templates/encrypted-source/js/.prettierrc.json +5 -0
  72. package/templates/encrypted-source/js/.vscode/ceru.code-snippets +55 -0
  73. package/templates/encrypted-source/js/.vscode/extensions.json +3 -0
  74. package/templates/encrypted-source/js/.vscode/launch.json +15 -0
  75. package/templates/encrypted-source/js/.vscode/settings.json +20 -0
  76. package/templates/encrypted-source/js/.vscode/tasks.json +40 -0
  77. package/templates/encrypted-source/js/DEMO.md +77 -0
  78. package/templates/encrypted-source/js/README.md +13 -0
  79. package/templates/encrypted-source/js/_gitignore +6 -0
  80. package/templates/encrypted-source/js/ceru-plugin.code-workspace +8 -0
  81. package/templates/encrypted-source/js/ceru.plugin.json +95 -0
  82. package/templates/encrypted-source/js/package.json +26 -0
  83. package/templates/encrypted-source/js/resources/xor-byte.wasm +0 -0
  84. package/templates/encrypted-source/js/resources/xor-byte.wat +5 -0
  85. package/templates/encrypted-source/js/src/demo.js +200 -0
  86. package/templates/encrypted-source/js/src/fixtures.js +118 -0
  87. package/templates/encrypted-source/js/src/index.js +4 -0
  88. package/templates/encrypted-source/js/tests/demo.test.mjs +357 -0
  89. package/templates/encrypted-source/js/tsconfig.json +16 -0
  90. package/templates/encrypted-source/ts/.prettierrc.json +5 -0
  91. package/templates/encrypted-source/ts/.vscode/ceru.code-snippets +55 -0
  92. package/templates/encrypted-source/ts/.vscode/extensions.json +3 -0
  93. package/templates/encrypted-source/ts/.vscode/launch.json +15 -0
  94. package/templates/encrypted-source/ts/.vscode/settings.json +20 -0
  95. package/templates/encrypted-source/ts/.vscode/tasks.json +40 -0
  96. package/templates/encrypted-source/ts/DEMO.md +77 -0
  97. package/templates/encrypted-source/ts/README.md +13 -0
  98. package/templates/encrypted-source/ts/_gitignore +6 -0
  99. package/templates/encrypted-source/ts/ceru-plugin.code-workspace +8 -0
  100. package/templates/encrypted-source/ts/ceru.plugin.json +95 -0
  101. package/templates/encrypted-source/ts/package.json +26 -0
  102. package/templates/encrypted-source/ts/resources/xor-byte.wasm +0 -0
  103. package/templates/encrypted-source/ts/resources/xor-byte.wat +5 -0
  104. package/templates/encrypted-source/ts/src/demo.ts +212 -0
  105. package/templates/encrypted-source/ts/src/fixtures.ts +125 -0
  106. package/templates/encrypted-source/ts/src/index.ts +4 -0
  107. package/templates/encrypted-source/ts/tests/demo.test.mjs +357 -0
  108. package/templates/encrypted-source/ts/tsconfig.json +17 -0
  109. package/templates/guest-adapter/js/.vscode/launch.json +12 -29
  110. package/templates/guest-adapter/js/.vscode/tasks.json +2 -3
  111. package/templates/guest-adapter/js/README.md +10 -10
  112. package/templates/guest-adapter/js/ceru.plugin.json +67 -62
  113. package/templates/guest-adapter/js/package.json +5 -5
  114. package/templates/guest-adapter/js/src/index.js +1 -1
  115. package/templates/guest-adapter/js/src/settings.js +8 -0
  116. package/templates/guest-adapter/ts/.vscode/launch.json +12 -29
  117. package/templates/guest-adapter/ts/.vscode/tasks.json +2 -3
  118. package/templates/guest-adapter/ts/README.md +10 -10
  119. package/templates/guest-adapter/ts/ceru.plugin.json +67 -62
  120. package/templates/guest-adapter/ts/package.json +5 -5
  121. package/templates/guest-adapter/ts/src/index.ts +1 -1
  122. package/templates/guest-adapter/ts/src/settings.ts +8 -0
  123. package/templates/importer/js/.vscode/launch.json +12 -29
  124. package/templates/importer/js/.vscode/tasks.json +2 -3
  125. package/templates/importer/js/README.md +10 -10
  126. package/templates/importer/js/ceru.plugin.json +23 -21
  127. package/templates/importer/js/package.json +5 -5
  128. package/templates/importer/ts/.vscode/launch.json +12 -29
  129. package/templates/importer/ts/.vscode/tasks.json +2 -3
  130. package/templates/importer/ts/README.md +10 -10
  131. package/templates/importer/ts/ceru.plugin.json +23 -21
  132. package/templates/importer/ts/package.json +5 -5
  133. package/templates/page-extension/js/.vscode/extensions.json +3 -0
  134. package/templates/page-extension/js/.vscode/launch.json +19 -0
  135. package/templates/page-extension/js/.vscode/settings.json +20 -0
  136. package/templates/page-extension/js/.vscode/tasks.json +43 -0
  137. package/templates/page-extension/js/README.md +9 -0
  138. package/templates/page-extension/js/_gitignore +6 -0
  139. package/templates/page-extension/js/ceru-plugin.code-workspace +8 -0
  140. package/templates/page-extension/js/ceru.plugin.json +94 -0
  141. package/templates/page-extension/js/package.json +24 -0
  142. package/templates/page-extension/js/src/Open.vue +6 -0
  143. package/templates/page-extension/js/src/Search.vue +62 -0
  144. package/templates/page-extension/js/src/index.js +54 -0
  145. package/templates/page-extension/js/tsconfig.json +17 -0
  146. package/templates/page-extension/ts/.vscode/extensions.json +3 -0
  147. package/templates/page-extension/ts/.vscode/launch.json +19 -0
  148. package/templates/page-extension/ts/.vscode/settings.json +20 -0
  149. package/templates/page-extension/ts/.vscode/tasks.json +43 -0
  150. package/templates/page-extension/ts/README.md +9 -0
  151. package/templates/page-extension/ts/_gitignore +6 -0
  152. package/templates/page-extension/ts/ceru-plugin.code-workspace +8 -0
  153. package/templates/page-extension/ts/ceru.plugin.json +94 -0
  154. package/templates/page-extension/ts/package.json +24 -0
  155. package/templates/page-extension/ts/src/Open.vue +6 -0
  156. package/templates/page-extension/ts/src/Search.vue +62 -0
  157. package/templates/page-extension/ts/src/index.ts +54 -0
  158. package/templates/page-extension/ts/tsconfig.json +17 -0
  159. package/templates/playback-hook/js/.prettierrc.json +5 -0
  160. package/templates/playback-hook/js/.vscode/extensions.json +3 -0
  161. package/templates/playback-hook/js/.vscode/launch.json +19 -0
  162. package/templates/playback-hook/js/.vscode/settings.json +20 -0
  163. package/templates/playback-hook/js/.vscode/tasks.json +50 -0
  164. package/templates/playback-hook/js/README.md +24 -0
  165. package/templates/playback-hook/js/_gitignore +6 -0
  166. package/templates/playback-hook/js/ceru-plugin.code-workspace +8 -0
  167. package/templates/playback-hook/js/ceru.plugin.json +36 -0
  168. package/templates/playback-hook/js/package.json +23 -0
  169. package/templates/playback-hook/js/src/index.js +25 -0
  170. package/templates/playback-hook/js/tests/hooks.json +316 -0
  171. package/templates/playback-hook/js/tsconfig.json +16 -0
  172. package/templates/playback-hook/ts/.prettierrc.json +5 -0
  173. package/templates/playback-hook/ts/.vscode/extensions.json +3 -0
  174. package/templates/playback-hook/ts/.vscode/launch.json +19 -0
  175. package/templates/playback-hook/ts/.vscode/settings.json +20 -0
  176. package/templates/playback-hook/ts/.vscode/tasks.json +50 -0
  177. package/templates/playback-hook/ts/README.md +24 -0
  178. package/templates/playback-hook/ts/_gitignore +6 -0
  179. package/templates/playback-hook/ts/ceru-plugin.code-workspace +8 -0
  180. package/templates/playback-hook/ts/ceru.plugin.json +36 -0
  181. package/templates/playback-hook/ts/package.json +23 -0
  182. package/templates/playback-hook/ts/src/index.ts +25 -0
  183. package/templates/playback-hook/ts/tests/hooks.json +316 -0
  184. package/templates/playback-hook/ts/tsconfig.json +16 -0
  185. package/templates/player-skin/js/.vscode/extensions.json +3 -0
  186. package/templates/player-skin/js/.vscode/launch.json +19 -0
  187. package/templates/player-skin/js/.vscode/settings.json +20 -0
  188. package/templates/player-skin/js/.vscode/tasks.json +43 -0
  189. package/templates/player-skin/js/README.md +11 -0
  190. package/templates/player-skin/js/_gitignore +6 -0
  191. package/templates/player-skin/js/assets/studio-preview.svg +15 -0
  192. package/templates/player-skin/js/ceru-plugin.code-workspace +8 -0
  193. package/templates/player-skin/js/ceru.plugin.json +77 -0
  194. package/templates/player-skin/js/package.json +24 -0
  195. package/templates/player-skin/js/src/Artwork.vue +9 -0
  196. package/templates/player-skin/js/src/Background.vue +15 -0
  197. package/templates/player-skin/js/src/BuiltinLyrics.vue +15 -0
  198. package/templates/player-skin/js/src/Lyrics.vue +28 -0
  199. package/templates/player-skin/js/tsconfig.json +17 -0
  200. package/templates/player-skin/ts/.vscode/extensions.json +3 -0
  201. package/templates/player-skin/ts/.vscode/launch.json +19 -0
  202. package/templates/player-skin/ts/.vscode/settings.json +20 -0
  203. package/templates/player-skin/ts/.vscode/tasks.json +43 -0
  204. package/templates/player-skin/ts/README.md +11 -0
  205. package/templates/player-skin/ts/_gitignore +6 -0
  206. package/templates/player-skin/ts/assets/studio-preview.svg +15 -0
  207. package/templates/player-skin/ts/ceru-plugin.code-workspace +8 -0
  208. package/templates/player-skin/ts/ceru.plugin.json +77 -0
  209. package/templates/player-skin/ts/package.json +24 -0
  210. package/templates/player-skin/ts/src/Artwork.vue +9 -0
  211. package/templates/player-skin/ts/src/Background.vue +15 -0
  212. package/templates/player-skin/ts/src/BuiltinLyrics.vue +15 -0
  213. package/templates/player-skin/ts/src/Lyrics.vue +28 -0
  214. package/templates/player-skin/ts/tsconfig.json +17 -0
  215. package/templates/react/js/.vscode/launch.json +12 -29
  216. package/templates/react/js/.vscode/tasks.json +2 -3
  217. package/templates/react/js/README.md +10 -10
  218. package/templates/react/js/ceru.plugin.json +27 -22
  219. package/templates/react/js/package.json +5 -5
  220. package/templates/react/js/src/index.js +1 -1
  221. package/templates/react/ts/.vscode/launch.json +12 -29
  222. package/templates/react/ts/.vscode/tasks.json +2 -3
  223. package/templates/react/ts/README.md +10 -10
  224. package/templates/react/ts/ceru.plugin.json +27 -22
  225. package/templates/react/ts/package.json +5 -5
  226. package/templates/react/ts/src/index.ts +1 -1
  227. package/templates/snapshot.json +152 -6
  228. package/templates/source/js/.vscode/launch.json +12 -29
  229. package/templates/source/js/.vscode/tasks.json +2 -3
  230. package/templates/source/js/README.md +12 -10
  231. package/templates/source/js/ceru.plugin.json +44 -26
  232. package/templates/source/js/package.json +5 -5
  233. package/templates/source/js/src/index.js +10 -8
  234. package/templates/source/ts/.vscode/launch.json +12 -29
  235. package/templates/source/ts/.vscode/tasks.json +2 -3
  236. package/templates/source/ts/README.md +12 -10
  237. package/templates/source/ts/ceru.plugin.json +44 -26
  238. package/templates/source/ts/package.json +5 -5
  239. package/templates/source/ts/src/index.ts +10 -8
  240. package/templates/vue/js/.vscode/launch.json +12 -29
  241. package/templates/vue/js/.vscode/tasks.json +2 -3
  242. package/templates/vue/js/README.md +10 -10
  243. package/templates/vue/js/ceru.plugin.json +27 -22
  244. package/templates/vue/js/package.json +5 -5
  245. package/templates/vue/js/src/index.js +1 -1
  246. package/templates/vue/ts/.vscode/launch.json +12 -29
  247. package/templates/vue/ts/.vscode/tasks.json +2 -3
  248. package/templates/vue/ts/README.md +10 -10
  249. package/templates/vue/ts/ceru.plugin.json +27 -22
  250. package/templates/vue/ts/package.json +5 -5
  251. package/templates/vue/ts/src/index.ts +1 -1
  252. package/templates/vue-tsx/js/.vscode/launch.json +12 -29
  253. package/templates/vue-tsx/js/.vscode/tasks.json +2 -3
  254. package/templates/vue-tsx/js/README.md +10 -10
  255. package/templates/vue-tsx/js/ceru.plugin.json +27 -22
  256. package/templates/vue-tsx/js/package.json +5 -5
  257. package/templates/vue-tsx/js/src/index.js +1 -1
  258. package/templates/vue-tsx/ts/.vscode/launch.json +12 -29
  259. package/templates/vue-tsx/ts/.vscode/tasks.json +2 -3
  260. package/templates/vue-tsx/ts/README.md +10 -10
  261. package/templates/vue-tsx/ts/ceru.plugin.json +27 -22
  262. package/templates/vue-tsx/ts/package.json +5 -5
  263. package/templates/vue-tsx/ts/src/index.ts +1 -1
  264. package/templates/web-dist/js/.vscode/launch.json +12 -29
  265. package/templates/web-dist/js/.vscode/tasks.json +2 -3
  266. package/templates/web-dist/js/README.md +10 -10
  267. package/templates/web-dist/js/ceru.plugin.json +27 -22
  268. package/templates/web-dist/js/package.json +5 -5
  269. package/templates/web-dist/js/src/index.js +1 -1
  270. package/templates/web-dist/ts/.vscode/launch.json +12 -29
  271. package/templates/web-dist/ts/.vscode/tasks.json +2 -3
  272. package/templates/web-dist/ts/README.md +10 -10
  273. package/templates/web-dist/ts/ceru.plugin.json +27 -22
  274. package/templates/web-dist/ts/package.json +5 -5
  275. package/templates/web-dist/ts/src/index.ts +1 -1
  276. package/templates/web-surface/js/.vscode/launch.json +12 -29
  277. package/templates/web-surface/js/.vscode/tasks.json +2 -3
  278. package/templates/web-surface/js/README.md +10 -10
  279. package/templates/web-surface/js/ceru.plugin.json +35 -35
  280. package/templates/web-surface/js/package.json +5 -5
  281. package/templates/web-surface/js/src/index.js +1 -1
  282. package/templates/web-surface/ts/.vscode/launch.json +12 -29
  283. package/templates/web-surface/ts/.vscode/tasks.json +2 -3
  284. package/templates/web-surface/ts/README.md +10 -10
  285. package/templates/web-surface/ts/ceru.plugin.json +35 -35
  286. package/templates/web-surface/ts/package.json +5 -5
  287. package/templates/web-surface/ts/src/index.ts +1 -1
  288. package/assets/core-contracts.js +0 -1
  289. package/assets/electron-app/main.cjs +0 -1
  290. package/assets/electron-app/package.json +0 -8
  291. package/assets/electron.cjs +0 -42
  292. package/assets/index.html +0 -94
  293. package/assets/native-view.js +0 -197
  294. package/assets/playground.js +0 -883
  295. package/assets/sandbox.js +0 -67
  296. package/assets/style.css +0 -337
  297. package/dist/socket-broker.d.ts +0 -23
  298. package/dist/socket-broker.js +0 -229
  299. package/templates/connected-library/js/ui/settings.json +0 -24
  300. package/templates/connected-library/ts/ui/settings.json +0 -24
  301. package/templates/guest-adapter/js/ui/settings.json +0 -14
  302. package/templates/guest-adapter/ts/ui/settings.json +0 -14
@@ -1,883 +0,0 @@
1
- import {
2
- assertContentPage,
3
- assertAccountSummary,
4
- assertNavigationRequest,
5
- assertLyricsDocument,
6
- assertResolveResult,
7
- PERMISSION_GROUPS,
8
- permissionGroup,
9
- SurfaceSession,
10
- } from './core-contracts.js'
11
- import { NativeSurfacePreview, PlaylistPagePreview } from './native-view.js'
12
- const $ = (id) => document.getElementById(id)
13
- let state,
14
- revision = 0,
15
- sequence = 0,
16
- stopped = false,
17
- viewId = null,
18
- currentInvocation
19
- let viewSession
20
- let nativePreview
21
- let viewMountInfo
22
- let playlistPagePreview
23
- const PLAYLIST_PAGE = Symbol('Host playlist page')
24
- let viewTransition = Promise.resolve()
25
- const frames = new Map(),
26
- calls = new Map(),
27
- registrations = new Map(),
28
- surfaceStates = new Map()
29
- const accountButtons = new Map()
30
- const accountLogoutButtons = new Map()
31
- const api = async (path, data) => {
32
- const response = await fetch('/api/' + path, {
33
- method: data === undefined ? 'GET' : 'POST',
34
- headers: {
35
- 'x-ceru-dev-token': window.CERU_DEV_TOKEN,
36
- ...(data === undefined ? {} : { 'Content-Type': 'application/json' }),
37
- },
38
- body: data === undefined ? undefined : JSON.stringify(data),
39
- })
40
- const result = await response.json()
41
- if (!response.ok) throw new Error(result.error || 'Host request failed')
42
- return result
43
- }
44
- function log(level, message) {
45
- const el = document.createElement('div')
46
- el.className = 'log ' + level
47
- const time = document.createElement('span')
48
- time.className = 'time'
49
- time.textContent = new Date().toLocaleTimeString() + ' '
50
- el.append(
51
- time,
52
- document.createTextNode(
53
- typeof message === 'string' ? message : JSON.stringify(message, null, 2),
54
- ),
55
- )
56
- $('logs').prepend(el)
57
- while ($('logs').children.length > 300) $('logs').lastChild.remove()
58
- }
59
- const iconUrl = (name) =>
60
- 'data:image/svg+xml;base64,' +
61
- btoa(unescape(encodeURIComponent(state.catalog.icons[name] || state.catalog.icons['music-note'])))
62
- function post(frame, type, data) {
63
- frame.element.contentWindow?.postMessage({ type, data, generation: frame.generation }, '*')
64
- }
65
- function removeFrames() {
66
- void playlistPagePreview?.close().catch(error => log('error', error.message))
67
- playlistPagePreview = undefined
68
- nativePreview?.dispose()
69
- nativePreview = undefined
70
- $('preview').replaceChildren()
71
- void viewSession?.close().catch(error => log('error', error.message))
72
- viewSession = undefined
73
- void api('rpc', { method: 'sockets.closeAll' }).catch(() => {})
74
- document.getElementById('host-playlist-import')?.remove()
75
- for (const frame of frames.values()) { post(frame, 'dispose'); frame.element.remove() }
76
- frames.clear()
77
- registrations.clear()
78
- for (const call of calls.values()) {
79
- clearTimeout(call.timer)
80
- call.reject(new Error('Plugin reloaded or stopped'))
81
- }
82
- calls.clear()
83
- surfaceStates.clear()
84
- currentInvocation = null
85
- $('results').replaceChildren()
86
- $('result-detail').textContent = ''
87
- document.getElementById('media-preview')?.remove()
88
- drawRegistrations()
89
- }
90
- function mount(moduleId, kind, container, mountInfo) {
91
- const element = document.createElement('iframe')
92
- element.setAttribute('sandbox', 'allow-scripts')
93
- element.src = '/sandbox.html?module=' + encodeURIComponent(moduleId)
94
- const frame = {
95
- element,
96
- moduleId,
97
- kind,
98
- mountInfo,
99
- generation: crypto.randomUUID(),
100
- active: false,
101
- }
102
- frames.set(moduleId, frame)
103
- container.append(element)
104
- return frame
105
- }
106
- function start() {
107
- stopped = false
108
- removeFrames()
109
- $('status').textContent = '加载中'
110
- if (state.manifest.modules.logic) mount(state.manifest.modules.logic.entry, 'logic', $('runners'))
111
- else $('status').textContent = '无后台入口'
112
- // Views open only after logic activation, when their actions have registered.
113
- updateSearchControls()
114
- }
115
- function hasProtocol(providerId, protocol) {
116
- return state?.manifest.contributes?.providers?.some(
117
- (provider) => provider.id === providerId && provider.protocols?.includes(protocol),
118
- )
119
- }
120
- function supportsMethod(item, method) {
121
- const protocol = method === 'search' ? 'music.search@1' : 'music.resolve@1'
122
- return item?.kind === 'provider' && Array.isArray(item.methods) &&
123
- item.methods.includes('tracks.' + method) && hasProtocol(item.id, protocol)
124
- }
125
- function isAvailable(item, method) {
126
- return !stopped && supportsMethod(item, method) && item.frame.active &&
127
- !item.frame.failed && frames.get(item.frame.moduleId) === item.frame
128
- }
129
- function updateSearchControls() {
130
- const item = registrations.get('provider:' + $('provider').value)
131
- const available = isAvailable(item, 'search')
132
- $('provider').disabled = !available
133
- $('query').disabled = !available
134
- $('search').disabled = !available
135
- $('cancel').disabled = !currentInvocation || !calls.has(currentInvocation.id)
136
- const logic = [...frames.values()].find((frame) => frame.kind === 'logic')
137
- let message
138
- if (stopped) message = '插件已停止,点击“重新运行”后继续调试。'
139
- else if (logic?.failed) message = '后台模块运行失败,请查看右侧日志并修复后重新运行。'
140
- else if (!state?.manifest.contributes?.providers?.some((p) => p.protocols?.includes('music.search@1')))
141
- message = '当前插件未提供搜索能力。页面插件可点击左侧“界面与兼容模块”中的页面进行预览;命令在“能力注册”中调用。'
142
- else if (!logic) message = '插件声明了搜索能力,但没有后台入口;请检查 Manifest 的 modules.logic。'
143
- else if (!logic.active) message = '后台模块正在启动,搜索将在注册完成后启用。'
144
- else if (!available) message = '插件声明了搜索能力,但尚未注册 search 实现;请检查后台的 ctx.providers.register(...)。'
145
- else message = '搜索来源已就绪。'
146
- $('search-status').textContent = message
147
- if (!$('results').children.length) {
148
- const empty = document.createElement('div')
149
- empty.className = 'empty'
150
- empty.dataset.capabilityHint = 'true'
151
- $('results').append(empty)
152
- }
153
- const empty = $('results').querySelector('[data-capability-hint]')
154
- if (empty) empty.textContent = available ? '输入关键词后点击“调用搜索”。' : message
155
- }
156
- function drawRegistrations() {
157
- $('registrations').replaceChildren()
158
- const selected = $('provider').value
159
- $('provider').replaceChildren()
160
- for (const [key, item] of registrations) {
161
- const row = document.createElement('div')
162
- row.className = 'item'
163
- if (item.kind === 'provider') {
164
- const meta = state.manifest.contributes?.providers?.find((p) => p.id === item.id)
165
- const img = document.createElement('img')
166
- img.width = 22
167
- img.height = 22
168
- img.src = iconUrl(meta?.icon?.name)
169
- row.append(img)
170
- row.append(document.createTextNode(meta?.name || item.id))
171
- if (supportsMethod(item, 'search')) {
172
- const option = document.createElement('option')
173
- option.value = item.id
174
- option.textContent = meta?.name || item.id
175
- $('provider').append(option)
176
- }
177
- } else if (item.kind === 'playlist-importer') {
178
- const button = document.createElement('button')
179
- const meta = state.manifest.contributes?.playlistImporters?.find((p) => p.id === item.id)
180
- button.textContent = '导入 · ' + (meta?.title || item.id)
181
- button.onclick = () => openPlaylistImport(item.id)
182
- row.append(button)
183
- } else {
184
- const button = document.createElement('button')
185
- button.textContent = item.id
186
- button.onclick = () =>
187
- invoke(item.frame, 'action', item.id, '', [{}])
188
- .then(showResult)
189
- .catch((e) => log('error', e.message))
190
- row.append(button)
191
- }
192
- $('registrations').append(row)
193
- }
194
- if ([...$('provider').options].some((option) => option.value === selected))
195
- $('provider').value = selected
196
- if (!$('provider').options.length) {
197
- const option = document.createElement('option')
198
- option.value = ''
199
- option.textContent = '无可用搜索来源'
200
- $('provider').append(option)
201
- }
202
- if (!registrations.size)
203
- $('registrations').textContent = stopped ? '插件已停止' : '等待后台模块注册命令或能力'
204
- updateSearchControls()
205
- }
206
- function invoke(frame, kind, target, method, args, signal) {
207
- const id = 'call-' + ++sequence
208
- currentInvocation = { frame, id }
209
- log('info', { call: kind === 'action' ? target : target + '.' + method })
210
- return new Promise((resolve, reject) => {
211
- const cancel = () => {
212
- const call = calls.get(id)
213
- if (!call) return
214
- clearTimeout(call.timer)
215
- calls.delete(id)
216
- post(frame, 'cancel', { id })
217
- reject(new Error('Surface invocation cancelled'))
218
- }
219
- signal?.addEventListener('abort', cancel, { once: true })
220
- calls.set(id, {
221
- frame,
222
- resolve: (value) => {
223
- signal?.removeEventListener('abort', cancel)
224
- try {
225
- if (
226
- kind === 'playlist-importer' ||
227
- (kind === 'provider' &&
228
- ['tracks.search', 'playlists.search', 'playlists.categories', 'playlists.list', 'playlists.get', 'charts.list', 'charts.getTracks'].includes(method))
229
- )
230
- assertContentPage(value)
231
- if (kind === 'provider' && method === 'tracks.lyrics') assertLyricsDocument(value)
232
- if (kind === 'provider' && method === 'tracks.resolve') assertResolveResult(value)
233
- resolve(value)
234
- } catch (error) { reject(error) }
235
- },
236
- reject: error => { signal?.removeEventListener('abort', cancel); reject(error) },
237
- timer: setTimeout(() => {
238
- calls.delete(id)
239
- updateSearchControls()
240
- post(frame, 'cancel', { id })
241
- signal?.removeEventListener('abort', cancel)
242
- reject(new Error('Invocation timed out'))
243
- }, 20000),
244
- })
245
- if (signal?.aborted) { cancel(); return }
246
- updateSearchControls()
247
- post(frame, 'invoke', { id, kind, target, method, args })
248
- })
249
- }
250
- function showResult(value) {
251
- $('result-detail').textContent = JSON.stringify(value ?? null, null, 2)
252
- document.getElementById('media-preview')?.remove()
253
- if (value?.ok && typeof value.url === 'string') {
254
- const audio = document.createElement('audio')
255
- audio.id = 'media-preview'
256
- audio.controls = true
257
- audio.src = value.url
258
- $('result-detail').after(audio)
259
- }
260
- return value
261
- }
262
- function runAction(action, input = {}, signal) {
263
- const item = registrations.get('action:' + action)
264
- if (!item) return Promise.reject(new Error('Action is not registered: ' + action))
265
- return invoke(item.frame, 'action', action, '', [input], signal)
266
- }
267
-
268
- // A single Host-owned preview for all importers. Plugins contribute data callbacks only.
269
- // The desktop application maps the same declarations to its existing import dialog.
270
- function openPlaylistImport(importerId, initialValue = '') {
271
- const item = registrations.get('playlist-importer:' + importerId)
272
- const meta = state.manifest.contributes?.playlistImporters?.find((p) => p.id === importerId)
273
- if (!item || !meta) throw new Error('Playlist importer is not registered')
274
- document.getElementById('host-playlist-import')?.remove()
275
- const dialog = document.createElement('dialog')
276
- dialog.id = 'host-playlist-import'
277
- const title = document.createElement('h3'); title.textContent = meta.title
278
- const description = document.createElement('p'); description.textContent = meta.description || '粘贴歌单链接或 ID'
279
- const input = document.createElement('input'); input.placeholder = meta.placeholder || '歌单链接或 ID'; input.value = initialValue
280
- const examples = document.createElement('ul')
281
- for (const example of meta.examples || []) { const li = document.createElement('li'); li.textContent = example.label + ':' + example.value; examples.append(li) }
282
- for (const note of meta.instructions || []) { const li = document.createElement('li'); li.textContent = note; examples.append(li) }
283
- const load = document.createElement('button'); load.textContent = '读取歌曲'
284
- const close = document.createElement('button'); close.textContent = '关闭'; close.onclick = () => dialog.close()
285
- const detail = document.createElement('pre')
286
- const hint = document.createElement('p')
287
- hint.textContent = '独立开发 Host 可预览解析结果;保存到软件本地/云歌单需要连接澜音正式 Host。'
288
- let cursor
289
- let previousValue
290
- load.onclick = async () => {
291
- if (!input.value.trim()) { detail.textContent = '请输入歌单链接或 ID'; return }
292
- if (input.value !== previousValue) cursor = undefined
293
- load.disabled = true
294
- try {
295
- const result = await invoke(item.frame, 'playlist-importer', importerId, 'getTracks', [{ value: input.value.trim(), cursor, limit: 100 }])
296
- previousValue = input.value
297
- cursor = result.nextCursor
298
- detail.textContent = JSON.stringify(result, null, 2)
299
- load.textContent = cursor ? '读取下一页' : '重新读取'
300
- } catch (error) { detail.textContent = error.message }
301
- finally { load.disabled = false }
302
- }
303
- dialog.append(title, description, input, examples, load, close, hint, detail)
304
- document.body.append(dialog); dialog.showModal()
305
- dialog.addEventListener('close', () => dialog.remove(), { once: true })
306
- }
307
- function renderSchema(node, values, container) {
308
- if (!node || typeof node !== 'object') return
309
- if (['section', 'stack', 'form', 'grid'].includes(node.type)) {
310
- const group = document.createElement('div')
311
- if (node.title) {
312
- const title = document.createElement('h3')
313
- title.textContent = node.title
314
- group.append(title)
315
- }
316
- for (const child of (node.children || []).slice(0, 100)) renderSchema(child, values, group)
317
- container.append(group)
318
- return
319
- }
320
- if (node.type === 'button') {
321
- const button = document.createElement('button')
322
- button.textContent = node.label || '执行'
323
- button.onclick = () =>
324
- runAction(node.action, values)
325
- .then(showResult)
326
- .catch((e) => log('error', e.message))
327
- container.append(button)
328
- return
329
- }
330
- if (node.type === 'host-content') {
331
- const content = document.createElement('div')
332
- content.className = 'host-content-preview'
333
- content.textContent = '宿主原内容(正式 Host 在这里保持现有组件和交互)'
334
- container.append(content)
335
- return
336
- }
337
- if (['text-input', 'input', 'number', 'toggle', 'host-credential'].includes(node.type)) {
338
- const label = document.createElement('label')
339
- label.textContent = node.label || node.bind || ''
340
- const input = document.createElement('input')
341
- input.type = node.type === 'number' ? 'number' : node.type === 'toggle' ? 'checkbox' : 'text'
342
- if (node.type === 'host-credential') {
343
- input.disabled = true
344
- input.placeholder = '凭据保险箱由生产 Host 提供'
345
- } else input.value = values[node.bind] ?? ''
346
- input.oninput = () => {
347
- values[node.bind] = input.type === 'checkbox' ? input.checked : input.value
348
- }
349
- label.append(input)
350
- container.append(label)
351
- return
352
- }
353
- const text = document.createElement('p')
354
- text.textContent =
355
- node.text || node.label || (node.bind ? String(values[node.bind] ?? '') : '[' + node.type + ']')
356
- container.append(text)
357
- }
358
- function showView(id, mountInfo = { kind: 'page' }) {
359
- viewTransition = viewTransition.catch(() => {}).then(async () => {
360
- await closeView()
361
- if (stopped) return
362
- viewMountInfo = mountInfo
363
- const logicEntry = state.manifest.modules.logic?.entry
364
- if (logicEntry && !frames.get(logicEntry)?.active) {
365
- viewId = id
366
- $('preview').textContent = '正在启动插件…'
367
- return
368
- }
369
- mountView(id, mountInfo)
370
- })
371
- return viewTransition.catch(error => log('error', error.message))
372
- }
373
- async function closeView() {
374
- const previousPlaylistPage = playlistPagePreview
375
- playlistPagePreview = undefined
376
- nativePreview?.dispose()
377
- nativePreview = undefined
378
- const previous = viewSession
379
- viewSession = undefined
380
- viewId = null
381
- for (const [key, frame] of frames) if (frame.kind !== 'logic') {
382
- post(frame, 'dispose')
383
- frame.element.remove()
384
- frames.delete(key)
385
- }
386
- $('preview').replaceChildren()
387
- await previous?.close()
388
- await previousPlaylistPage?.close()
389
- }
390
- function mountView(id, mountInfo) {
391
- viewId = id
392
- for (const [key, frame] of frames)
393
- if (frame.kind !== 'logic') {
394
- frame.element.remove()
395
- frames.delete(key)
396
- }
397
- $('preview').replaceChildren()
398
- if (id === PLAYLIST_PAGE) {
399
- playlistPagePreview = new PlaylistPagePreview($('preview'), state.manifest, runAction, error => log('error', error.message))
400
- void playlistPagePreview.open(mountInfo.sectionId).catch(error => log('error', error.message))
401
- return
402
- }
403
- const view = state.manifest.modules.surfaces?.find((v) => v.id === id)
404
- if (view?.kind === 'web') {
405
- viewSession = new SurfaceSession(view, state.manifest, runAction)
406
- const frame = mount(view.entry, 'web', $('preview'), mountInfo)
407
- frame.surfaceId = id
408
- frame.session = viewSession
409
- if (view.presentation?.kind === 'modal') {
410
- frame.element.style.width = (view.presentation.size ?? 480) + 'px'
411
- frame.element.style.maxWidth = '100%'
412
- frame.element.style.display = 'block'
413
- frame.element.style.marginInline = 'auto'
414
- frame.element.scrollIntoView({ block: 'center' })
415
- }
416
- }
417
- else if (view?.kind === 'native') {
418
- viewSession = new SurfaceSession(view, state.manifest, runAction)
419
- nativePreview = new NativeSurfacePreview(
420
- $('preview'), viewSession,
421
- new Set(state.manifest.contributes?.commands?.map(command => command.action)),
422
- error => log('error', error.message),
423
- )
424
- void nativePreview.open()
425
- }
426
- else if (view?.kind === 'schema')
427
- renderSchema(
428
- state.resources[view.entry]?.value?.root,
429
- { ...(surfaceStates.get(id) || {}) },
430
- $('preview'),
431
- )
432
- else {
433
- const adapter = state.manifest.contributes?.guestAdapters?.find((v) => v.id === id)
434
- if (adapter) mount(adapter.bootstrap, 'guest', $('preview'))
435
- }
436
- }
437
- function drawStatic() {
438
- $('plugin-name').textContent = state.manifest.name
439
- $('plugin-meta').textContent =
440
- state.manifest.id + ' · v' + state.manifest.version + ' · revision ' + revision
441
- $('views').replaceChildren()
442
- const playlistPage = document.createElement('button')
443
- playlistPage.textContent = '歌单页 · 本地与云歌单'
444
- playlistPage.onclick = () => showView(PLAYLIST_PAGE)
445
- $('views').append(playlistPage)
446
- for (const section of [...(state.manifest.contributes?.playlistSections ?? [])].sort((a, b) => (a.order ?? 0) - (b.order ?? 0))) {
447
- const button = document.createElement('button')
448
- button.textContent = '歌单页 · ' + section.title
449
- button.onclick = () => showView(PLAYLIST_PAGE, { kind: 'page', sectionId: section.id })
450
- $('views').append(button)
451
- }
452
- accountButtons.clear()
453
- accountLogoutButtons.clear()
454
- for (const account of state.manifest.contributes?.accountItems ?? []) {
455
- const item = document.createElement('div')
456
- item.className = 'account-item'
457
- const button = document.createElement('button')
458
- button.textContent = '账号 · ' + account.title
459
- button.onclick = () => showView(account.view)
460
- button.disabled = true
461
- accountButtons.set(account.id, button)
462
- item.append(button)
463
- if (account.logoutAction) {
464
- const logout = document.createElement('button')
465
- logout.textContent = '退出登录'
466
- logout.className = 'account-logout'
467
- logout.hidden = true
468
- logout.onclick = async () => {
469
- logout.disabled = true
470
- try { await runAction(account.logoutAction); await refreshAccountItems(account.view) }
471
- catch (error) { log('error', error.message) }
472
- finally { logout.disabled = false }
473
- }
474
- accountLogoutButtons.set(account.id, logout)
475
- item.append(logout)
476
- }
477
- $('views').append(item)
478
- }
479
- for (const view of [
480
- ...(state.manifest.modules.surfaces || []),
481
- ...(state.manifest.contributes?.guestAdapters || []),
482
- ]) {
483
- const button = document.createElement('button')
484
- button.textContent = view.id + (view.kind ? ' · ' + view.kind : ' · guest bootstrap')
485
- button.onclick = () => showView(view.id)
486
- $('views').append(button)
487
- }
488
- for (const section of state.manifest.contributes?.homeSections || []) {
489
- const button = document.createElement('button')
490
- button.textContent = '首页 · ' + section.title + ' · ' + section.kind
491
- button.disabled = !section.view
492
- if (section.view) button.onclick = () => showView(section.view)
493
- else button.title = '正式 Host 将复用软件现有页面'
494
- $('views').append(button)
495
- }
496
- for (const extension of state.manifest.contributes?.uiExtensions || []) {
497
- const button = document.createElement('button')
498
- button.textContent = extension.slot + ' · ' + extension.mode
499
- button.onclick = () =>
500
- showView(extension.view, {
501
- kind: 'slot',
502
- slot: extension.slot,
503
- mode: extension.mode,
504
- })
505
- $('views').append(button)
506
- }
507
- $('permissions').replaceChildren()
508
- const renderedGroups = new Set()
509
- for (const permission of state.manifest.permissions || []) {
510
- const group = permissionGroup(permission.name) || 'other'
511
- if (!renderedGroups.has(group)) {
512
- renderedGroups.add(group)
513
- const declared = (state.manifest.permissions || []).filter(
514
- (item) => (permissionGroup(item.name) || 'other') === group,
515
- )
516
- const header = document.createElement('div')
517
- header.className = 'permission-group'
518
- const title = document.createElement('strong')
519
- title.textContent = PERMISSION_GROUPS[group]?.title || '其他权限'
520
- const button = document.createElement('button')
521
- const allGranted = declared.every((item) => state.grants[item.key])
522
- button.textContent = allGranted ? '撤销整组' : '授予整组'
523
- button.onclick = async () => {
524
- try {
525
- for (const item of declared)
526
- await api('grant', {
527
- key: item.key,
528
- allow: !allGranted,
529
- })
530
- state = await api('state')
531
- drawStatic()
532
- } catch (error) {
533
- log('error', error.message)
534
- }
535
- }
536
- header.append(title, button)
537
- $('permissions').append(header)
538
- }
539
- const row = document.createElement('div')
540
- row.className = 'permission'
541
- const title = document.createElement('strong')
542
- title.textContent = permission.key
543
- const reason = document.createElement('small')
544
- reason.textContent = permission.reason
545
- const button = document.createElement('button')
546
- button.textContent = state.grants[permission.key] ? '撤销' : '授予'
547
- button.onclick = async () => {
548
- try {
549
- await api('grant', {
550
- key: permission.key,
551
- allow: !state.grants[permission.key],
552
- })
553
- state = await api('state')
554
- drawStatic()
555
- } catch (e) {
556
- log('error', e.message)
557
- }
558
- }
559
- row.append(title, reason)
560
- row.append(button)
561
- $('permissions').append(row)
562
- }
563
- if (!(state.manifest.permissions || []).length)
564
- $('permissions').textContent = '此插件未声明额外权限'
565
- for (const style of document.querySelectorAll('style[data-plugin-global-style]')) style.remove()
566
- const hasGlobalStyleGrant = (state.manifest.permissions || []).some(
567
- (permission) => permission.name === 'ui.styles.global' && state.grants[permission.key],
568
- )
569
- if (hasGlobalStyleGrant)
570
- for (const contribution of state.manifest.contributes?.styles || []) {
571
- if (contribution.scope !== 'application') continue
572
- const resource = state.resources[contribution.resource]
573
- if (resource?.type !== 'text') continue
574
- const style = document.createElement('style')
575
- style.dataset.pluginGlobalStyle = contribution.id
576
- style.textContent = resource.value
577
- document.head.append(style)
578
- }
579
- $('icons').replaceChildren()
580
- for (const name of Object.keys(state.catalog.icons)) {
581
- const card = document.createElement('div')
582
- card.className = 'icon-card'
583
- const img = document.createElement('img')
584
- img.src = iconUrl(name)
585
- card.append(img, document.createTextNode(name))
586
- $('icons').append(card)
587
- }
588
- $('static-assets').replaceChildren()
589
- for (const [name, url] of Object.entries(state.catalog.assets)) {
590
- const card = document.createElement('div'),
591
- image = document.createElement('img'),
592
- label = document.createElement('div')
593
- image.src = url
594
- label.textContent = name
595
- label.className = 'hint'
596
- card.append(image, label)
597
- $('static-assets').append(card)
598
- }
599
- }
600
- async function refreshAccountItems(surfaceId) {
601
- await Promise.all((state.manifest.contributes?.accountItems ?? []).map(async account => {
602
- if ((surfaceId && account.view !== surfaceId) || !registrations.has('action:' + account.action)) return
603
- const button = accountButtons.get(account.id)
604
- if (!button) return
605
- try {
606
- const summary = await runAction(account.action)
607
- assertAccountSummary(summary)
608
- button.textContent = '账号 · ' + summary.displayName + (summary.badge ? ' · ' + summary.badge : '')
609
- button.disabled = false
610
- const logout = accountLogoutButtons.get(account.id)
611
- if (logout) logout.hidden = !summary.signedIn
612
- } catch (error) {
613
- button.textContent = '账号 · ' + account.title
614
- button.disabled = false
615
- log('error', error.message)
616
- }
617
- }))
618
- }
619
- window.addEventListener('message', async (event) => {
620
- const frame = [...frames.values()].find((f) => f.element.contentWindow === event.source)
621
- if (!frame || !event.data?.type) return
622
- const message = event.data
623
- if (message.type === 'ready') {
624
- post(frame, 'init', {
625
- generation: frame.generation,
626
- kind: frame.kind,
627
- manifest: frame.kind === 'web' ? { ...state.manifest, config: {} } : state.manifest,
628
- catalog: state.catalog,
629
- resources: state.resources,
630
- mount: frame.mountInfo,
631
- })
632
- return
633
- }
634
- if (message.generation !== frame.generation) return
635
- const data = message.data
636
- if (message.type === 'resize' && frame.kind === 'web' &&
637
- Number.isFinite(data?.height) && data.height >= 1 && data.height <= 16384) {
638
- frame.element.style.minHeight = '0'
639
- frame.element.style.height = Math.min(Math.max(120, data.height), Math.max(160, window.innerHeight - 160)) + 'px'
640
- }
641
- if (message.type === 'active') {
642
- frame.active = true
643
- frame.failed = false
644
- $('status').textContent = '运行中'
645
- log('info', frame.moduleId + ' activated')
646
- drawRegistrations()
647
- if (frame.kind === 'web') {
648
- post(frame, 'state', surfaceStates.get(frame.surfaceId) ?? {})
649
- void frame.session.open().catch(error => log('error', error.message))
650
- } else if (frame.kind === 'logic') {
651
- void refreshAccountItems()
652
- if (viewId) void showView(viewId, viewMountInfo)
653
- }
654
- }
655
- if (message.type === 'failed') {
656
- frame.failed = true
657
- frame.active = false
658
- for (const [key, item] of registrations) {
659
- if (item.frame === frame) registrations.delete(key)
660
- }
661
- drawRegistrations()
662
- $('status').textContent = '运行失败'
663
- log('error', data.message)
664
- }
665
- if (message.type === 'log') log(data.level, data.values)
666
- if (message.type === 'notify') log(data.level, data.message)
667
- if (message.type === 'open-view' && frame.kind === 'logic') void showView(data.surfaceId)
668
- if (message.type === 'close-view' &&
669
- ((frame.kind === 'logic' && data.surfaceId === viewId) ||
670
- (frame.kind === 'web' && frame.surfaceId === viewId)))
671
- void closeView().catch(error => log('error', error.message))
672
- if (message.type === 'register') {
673
- if (frame.kind !== 'logic') return
674
- const valid =
675
- data.kind === 'provider'
676
- ? state.manifest.contributes?.providers?.some((p) => p.id === data.id)
677
- : data.kind === 'playlist-importer'
678
- ? state.manifest.contributes?.playlistImporters?.some((p) => p.id === data.id)
679
- : data.kind === 'lyric-converter' ? state.manifest.contributes?.lyricConverters?.some((p) => p.id === data.id)
680
- : state.manifest.contributes?.commands?.some((p) => p.action === data.id)
681
- if (valid) {
682
- registrations.set(data.kind + ':' + data.id, { ...data, frame })
683
- drawRegistrations()
684
- }
685
- }
686
- if (message.type === 'unregister') {
687
- registrations.delete(data.kind + ':' + data.id)
688
- drawRegistrations()
689
- }
690
- if (message.type === 'state' && frame.kind === 'logic') {
691
- const changed = JSON.stringify(surfaceStates.get(data.surfaceId)) !== JSON.stringify(data.state)
692
- surfaceStates.set(data.surfaceId, data.state)
693
- if (changed) void playlistPagePreview?.refresh(data.surfaceId)
694
- if (changed) void refreshAccountItems(data.surfaceId)
695
- if (changed && viewId === data.surfaceId) void nativePreview?.refresh()
696
- for (const view of frames.values()) if (view.kind === 'web' && view.surfaceId === data.surfaceId) post(view, 'state', data.state)
697
- if (
698
- viewId === data.surfaceId &&
699
- state.manifest.modules.surfaces?.find((v) => v.id === viewId)?.kind === 'schema'
700
- )
701
- showView(viewId)
702
- }
703
- if (message.type === 'invoke-result') {
704
- const call = calls.get(data.id)
705
- if (call?.frame === frame) {
706
- clearTimeout(call.timer)
707
- calls.delete(data.id)
708
- updateSearchControls()
709
- data.error ? call.reject(new Error(data.error)) : call.resolve(data.value)
710
- }
711
- }
712
- if (message.type === 'rpc') {
713
- try {
714
- let value
715
- if (data.method === 'surface.invoke' && frame.kind === 'web')
716
- value = await frame.session.invoke(data.data.action, data.data.input ?? {})
717
- else if (frame.kind !== 'logic')
718
- throw new Error('This Surface/Guest cannot call a logic Host API directly')
719
- else if (data.method === 'permissions.requestGroup') {
720
- const declared = (state.manifest.permissions || []).filter(
721
- (permission) =>
722
- permissionGroup(permission.name) === data.data.group &&
723
- (!data.data.keys || data.data.keys.includes(permission.key)),
724
- )
725
- if (!declared.length) throw new Error('No permissions are declared in this group')
726
- const allowed = confirm(
727
- '开发权限组申请:' +
728
- data.data.group +
729
- '\n\n' +
730
- declared.map((permission) => permission.reason).join('\n'),
731
- )
732
- if (allowed) {
733
- for (const declaration of declared) {
734
- await api('grant', {
735
- key: declaration.key,
736
- allow: true,
737
- })
738
- }
739
- state = await api('state')
740
- drawStatic()
741
- }
742
- value = {
743
- group: data.data.group,
744
- status: allowed ? 'granted' : 'denied',
745
- grants: declared.map((permission) => ({
746
- key: permission.key,
747
- name: permission.name,
748
- group: data.data.group,
749
- scope: permission.scope || {},
750
- status: allowed ? 'granted' : 'denied',
751
- })),
752
- }
753
- } else if (data.method === 'ui.dialogs.confirm')
754
- value = confirm(data.data.message || data.data.title || '确认操作?')
755
- else if (data.method === 'ui.dialogs.prompt')
756
- value = prompt(data.data.label || data.data.title || '请输入', data.data.value || '')
757
- else if (data.method === 'ui.navigation.open') {
758
- assertNavigationRequest(data.data, state.manifest)
759
- log('info', { navigation: data.data })
760
- if (data.data.page === 'playlist' && !data.data.ref)
761
- void showView(PLAYLIST_PAGE, { kind: 'page', sectionId: data.data.sectionId })
762
- value = null
763
- }
764
- else if (data.method === 'ui.playlistImport.open') {
765
- openPlaylistImport(data.data.importerId, data.data.initialValue)
766
- value = null
767
- }
768
- else if (data.method === 'permissions.request') {
769
- const declaration = state.manifest.permissions?.find((p) => p.key === data.data.key)
770
- if (!declaration) value = { status: 'undeclared' }
771
- else {
772
- const allowed = confirm(
773
- '开发权限申请:' +
774
- declaration.key +
775
- '\n' +
776
- declaration.reason,
777
- )
778
- if (allowed) {
779
- await api('grant', {
780
- key: declaration.key,
781
- allow: true,
782
- })
783
- state = await api('state')
784
- drawStatic()
785
- }
786
- value = { status: allowed ? 'granted' : 'denied' }
787
- }
788
- } else value = (await api('rpc', { method: data.method, data: data.data })).value
789
- post(frame, 'rpc-result', { id: data.id, value })
790
- } catch (e) {
791
- post(frame, 'rpc-result', { id: data.id, error: e.message })
792
- }
793
- }
794
- })
795
- $('search').onclick = async () => {
796
- const id = $('provider').value
797
- const item = registrations.get('provider:' + id)
798
- if (!isAvailable(item, 'search')) {
799
- updateSearchControls()
800
- return
801
- }
802
- try {
803
- const result = await invoke(item.frame, 'provider', id, 'tracks.search', [
804
- { query: $('query').value, kinds: ['track'], filters: {}, limit: 20 },
805
- ])
806
- if (!isAvailable(item, 'search')) return
807
- showResult(result)
808
- $('results').replaceChildren()
809
- for (const track of result.items || []) {
810
- const row = document.createElement('div')
811
- row.className = 'track'
812
- const title = document.createElement('div')
813
- title.textContent = track.title
814
- const button = document.createElement('button')
815
- button.textContent = '调用解析'
816
- button.disabled = !isAvailable(item, 'resolve')
817
- if (button.disabled) button.title = '此来源未提供播放解析能力'
818
- button.onclick = () =>
819
- isAvailable(item, 'resolve') &&
820
- invoke(item.frame, 'provider', id, 'tracks.resolve', [track.ref, undefined])
821
- .then(showResult)
822
- .catch((e) => log('error', e.message))
823
- row.append(title, button)
824
- $('results').append(row)
825
- }
826
- if (!(result.items || []).length) $('results').textContent = '没有结果'
827
- } catch (e) {
828
- if (isAvailable(item, 'search')) log('error', e.message)
829
- }
830
- }
831
- $('provider').onchange = updateSearchControls
832
- $('cancel').onclick = () => {
833
- if (currentInvocation) post(currentInvocation.frame, 'cancel', { id: currentInvocation.id })
834
- }
835
- $('restart').onclick = start
836
- $('stop').onclick = () => {
837
- stopped = true
838
- removeFrames()
839
- $('status').textContent = '已停止'
840
- }
841
- $('clear-log').onclick = () => $('logs').replaceChildren()
842
- $('close-view').onclick = () => { void closeView().catch(error => log('error', error.message)) }
843
- $('apply-config').onclick = async () => {
844
- try {
845
- await api('config', JSON.parse($('config').value))
846
- start()
847
- } catch (e) {
848
- log('error', e.message)
849
- }
850
- }
851
- async function poll() {
852
- try {
853
- const next = await api('state')
854
- $('build-error').classList.toggle('hidden', !next.error)
855
- $('build-error').textContent = next.error || ''
856
- if (next.revision !== revision) {
857
- state = next
858
- revision = next.revision
859
- drawStatic()
860
- for (const warning of next.migrationWarnings || []) log('warn', warning)
861
- if (!stopped) start()
862
- }
863
- const { events } = await api('socket-events')
864
- for (const event of events) {
865
- for (const frame of frames.values()) if (frame.kind === 'logic' && frame.active) post(frame, 'socket-event', event)
866
- }
867
- } catch (e) {
868
- $('status').textContent = 'Host 已断开'
869
- log('error', e.message)
870
- }
871
- }
872
- window.__ceruDev = {
873
- get state() {
874
- return state
875
- },
876
- get registrations() {
877
- return [...registrations.keys()]
878
- },
879
- runAction,
880
- restart: start,
881
- }
882
- poll()
883
- setInterval(poll, 750)