@sublane/core 0.0.0-stage → 0.1.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/LICENSE +215 -0
- package/README.md +8 -2
- package/package.json +16 -4
- package/src/contract.ts +104 -0
- package/src/index.ts +8 -0
- package/src/runtime.ts +182 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
Copyright 2026 Rocke1001feller
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
|
14
|
+
|
|
15
|
+
Apache License
|
|
16
|
+
Version 2.0, January 2004
|
|
17
|
+
http://www.apache.org/licenses/
|
|
18
|
+
|
|
19
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
20
|
+
|
|
21
|
+
1. Definitions.
|
|
22
|
+
|
|
23
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
24
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
25
|
+
|
|
26
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
27
|
+
the copyright owner that is granting the License.
|
|
28
|
+
|
|
29
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
30
|
+
other entities that control, are controlled by, or are under common
|
|
31
|
+
control with that entity. For the purposes of this definition,
|
|
32
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
33
|
+
direction or management of such entity, whether by contract or
|
|
34
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
35
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
36
|
+
|
|
37
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
38
|
+
exercising permissions granted by this License.
|
|
39
|
+
|
|
40
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
41
|
+
including but not limited to software source code, documentation
|
|
42
|
+
source, and configuration files.
|
|
43
|
+
|
|
44
|
+
"Object" form shall mean any form resulting from mechanical
|
|
45
|
+
transformation or translation of a Source form, including but
|
|
46
|
+
not limited to compiled object code, generated documentation,
|
|
47
|
+
and conversions to other media types.
|
|
48
|
+
|
|
49
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
50
|
+
Object form, made available under the License, as indicated by a
|
|
51
|
+
copyright notice that is included in or attached to the work
|
|
52
|
+
(an example is provided in the Appendix below).
|
|
53
|
+
|
|
54
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
55
|
+
form, that is based on (or derived from) the Work and for which the
|
|
56
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
57
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
58
|
+
of this License, Derivative Works shall not include works that remain
|
|
59
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
60
|
+
the Work and Derivative Works thereof.
|
|
61
|
+
|
|
62
|
+
"Contribution" shall mean any work of authorship, including
|
|
63
|
+
the original version of the Work and any modifications or additions
|
|
64
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
65
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
66
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
67
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
68
|
+
means any form of electronic, verbal, or written communication sent
|
|
69
|
+
to the Licensor or its representatives, including but not limited to
|
|
70
|
+
communication on electronic mailing lists, source code control systems,
|
|
71
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
72
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
73
|
+
excluding communication that is conspicuously marked or otherwise
|
|
74
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
75
|
+
|
|
76
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
77
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
78
|
+
subsequently incorporated within the Work.
|
|
79
|
+
|
|
80
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
81
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
82
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
83
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
84
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
85
|
+
Work and such Derivative Works in Source or Object form.
|
|
86
|
+
|
|
87
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
88
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
89
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
90
|
+
(except as stated in this section) patent license to make, have made,
|
|
91
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
92
|
+
where such license applies only to those patent claims licensable
|
|
93
|
+
by such Contributor that are necessarily infringed by their
|
|
94
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
95
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
96
|
+
institute patent litigation against any entity (including a
|
|
97
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
98
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
99
|
+
or contributory patent infringement, then any patent licenses
|
|
100
|
+
granted to You under this License for that Work shall terminate
|
|
101
|
+
as of the date such litigation is filed.
|
|
102
|
+
|
|
103
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
104
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
105
|
+
modifications, and in Source or Object form, provided that You
|
|
106
|
+
meet the following conditions:
|
|
107
|
+
|
|
108
|
+
(a) You must give any other recipients of the Work or
|
|
109
|
+
Derivative Works a copy of this License; and
|
|
110
|
+
|
|
111
|
+
(b) You must cause any modified files to carry prominent notices
|
|
112
|
+
stating that You changed the files; and
|
|
113
|
+
|
|
114
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
115
|
+
that You distribute, all copyright, patent, trademark, and
|
|
116
|
+
attribution notices from the Source form of the Work,
|
|
117
|
+
excluding those notices that do not pertain to any part of
|
|
118
|
+
the Derivative Works; and
|
|
119
|
+
|
|
120
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
121
|
+
distribution, then any Derivative Works that You distribute must
|
|
122
|
+
include a readable copy of the attribution notices contained
|
|
123
|
+
within such NOTICE file, excluding those notices that do not
|
|
124
|
+
pertain to any part of the Derivative Works, in at least one
|
|
125
|
+
of the following places: within a NOTICE text file distributed
|
|
126
|
+
as part of the Derivative Works; within the Source form or
|
|
127
|
+
documentation, if provided along with the Derivative Works; or,
|
|
128
|
+
within a display generated by the Derivative Works, if and
|
|
129
|
+
wherever such third-party notices normally appear. The contents
|
|
130
|
+
of the NOTICE file are for informational purposes only and
|
|
131
|
+
do not modify the License. You may add Your own attribution
|
|
132
|
+
notices within Derivative Works that You distribute, alongside
|
|
133
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
134
|
+
that such additional attribution notices cannot be construed
|
|
135
|
+
as modifying the License.
|
|
136
|
+
|
|
137
|
+
You may add Your own copyright statement to Your modifications and
|
|
138
|
+
may provide additional or different license terms and conditions
|
|
139
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
140
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
141
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
142
|
+
the conditions stated in this License.
|
|
143
|
+
|
|
144
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
145
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
146
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
147
|
+
this License, without any additional terms or conditions.
|
|
148
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
149
|
+
the terms of any separate license agreement you may have executed
|
|
150
|
+
with Licensor regarding such Contributions.
|
|
151
|
+
|
|
152
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
153
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
154
|
+
except as required for reasonable and customary use in describing the
|
|
155
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
156
|
+
|
|
157
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
158
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
159
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
160
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
161
|
+
implied, including, without limitation, any warranties or conditions
|
|
162
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
163
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
164
|
+
appropriateness of using or redistributing the Work and assume any
|
|
165
|
+
risks associated with Your exercise of permissions under this License.
|
|
166
|
+
|
|
167
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
168
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
169
|
+
unless required by applicable law (such as deliberate and grossly
|
|
170
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
171
|
+
liable to You for damages, including any direct, indirect, special,
|
|
172
|
+
incidental, or consequential damages of any character arising as a
|
|
173
|
+
result of this License or out of the use or inability to use the
|
|
174
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
175
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
176
|
+
other commercial damages or losses), even if such Contributor
|
|
177
|
+
has been advised of the possibility of such damages.
|
|
178
|
+
|
|
179
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
180
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
181
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
182
|
+
or other liability obligations and/or rights consistent with this
|
|
183
|
+
License. However, in accepting such obligations, You may act only
|
|
184
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
185
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
186
|
+
defend, and hold each Contributor harmless for any liability
|
|
187
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
188
|
+
of your accepting any such warranty or additional liability.
|
|
189
|
+
|
|
190
|
+
END OF TERMS AND CONDITIONS
|
|
191
|
+
|
|
192
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
193
|
+
|
|
194
|
+
To apply the Apache License to your work, attach the following
|
|
195
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
196
|
+
replaced with your own identifying information. (Don't include
|
|
197
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
198
|
+
comment syntax for the file format. We also recommend that a
|
|
199
|
+
file or class name and description of purpose be included on the
|
|
200
|
+
same "printed page" as the copyright notice for easier
|
|
201
|
+
identification within third-party archives.
|
|
202
|
+
|
|
203
|
+
Copyright [yyyy] [name of copyright owner]
|
|
204
|
+
|
|
205
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
206
|
+
you may not use this file except in compliance with the License.
|
|
207
|
+
You may obtain a copy of the License at
|
|
208
|
+
|
|
209
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
210
|
+
|
|
211
|
+
Unless required by applicable law or agreed to in writing, software
|
|
212
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
213
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
214
|
+
See the License for the specific language governing permissions and
|
|
215
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sublane/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sublane 框架核心——视频字幕翻译能力的宿主契约层。Apache-2.0,零 vendor 代码。
|
|
4
|
+
|
|
5
|
+
给任意 WXT 扩展提供:插件契约(`VideoSubtitlePlugin` 五件套)、实例生命周期(`createVideoSubtitleFramework` → `start()`)、同屏车道分配、配置命名空间(`sublane:plugins.videoSubtitle`)。
|
|
6
|
+
|
|
7
|
+
- 总览与 quick start:[Sublane 仓库 README](https://github.com/ai-baymax-dabai/sublane/tree/master/video-subtitle-translation)
|
|
8
|
+
- 接入指南:[docs/integration.md](https://github.com/ai-baymax-dabai/sublane/blob/master/video-subtitle-translation/docs/integration.md)
|
|
9
|
+
- 公共 API(semver 承诺):仓库 README "Public API" 节——导出清单被 `release-gates.test.ts` 冻结
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sublane/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"import": "./src/index.ts",
|
|
10
|
+
"default": "./src/index.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./src/index.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"!src/**/*.test.ts"
|
|
17
|
+
]
|
|
18
|
+
}
|
package/src/contract.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/contract.ts
|
|
3
|
+
* 文件职责:定义 video-subtitle-translation 插件契约——框架与供应商之间的全部承诺,不含任何实现。
|
|
4
|
+
* 主要内容:供应商标识、平台与车道类型、能力旗标、设置 schema 数据形状、运行时配置、挂载上下文、
|
|
5
|
+
* 内容侧插件与实例接口、background 侧接口、存储端口。
|
|
6
|
+
* 模块边界:纯类型与字面量,零依赖零副作用;框架不解释 capabilities 内容,settings 语义归各供应商 adapter。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type SupplierId = string
|
|
10
|
+
|
|
11
|
+
export type VideoPlatform =
|
|
12
|
+
| 'youtube' | 'youtube-embed' | 'youtube-shorts' | 'youtube-live'
|
|
13
|
+
| 'x' | 'netflix'
|
|
14
|
+
| 'meet' | 'teams' | 'zoom' | 'udemy' | 'disney'
|
|
15
|
+
| (string & {})
|
|
16
|
+
|
|
17
|
+
/** 车道分配:框架对同屏空间仲裁的结果,供应商必须服从;像素换算归 adapter */
|
|
18
|
+
export interface LaneAssignment {
|
|
19
|
+
index: number
|
|
20
|
+
count: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface VideoSubtitleCapabilities {
|
|
24
|
+
platforms: VideoPlatform[]
|
|
25
|
+
/** AI 转写来源;'cloud' = 供应商私有云,'local' = 浏览器内本地模型,false = 无 */
|
|
26
|
+
aiSubtitles: 'cloud' | 'local' | false
|
|
27
|
+
displayModes: Array<'bilingual' | 'originalOnly' | 'translationOnly'>
|
|
28
|
+
bilingualDownload: boolean
|
|
29
|
+
sidebar: boolean
|
|
30
|
+
manualTimingOffset: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type SettingsField =
|
|
34
|
+
| { key: string; type: 'boolean'; label: string; default: boolean }
|
|
35
|
+
| { key: string; type: 'enum'; label: string; options: Array<{ value: string; label: string }>; default: string }
|
|
36
|
+
| { key: string; type: 'number'; label: string; min?: number; max?: number; step?: number; default: number }
|
|
37
|
+
| { key: string; type: 'string'; label: string; default: string; secret?: boolean }
|
|
38
|
+
| { key: string; type: 'color'; label: string; default: string }
|
|
39
|
+
|
|
40
|
+
export interface SettingsSchema {
|
|
41
|
+
sections: Array<{ key: string; label: string; fields: SettingsField[] }>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 框架下发给实例的运行时配置(命名空间已由框架解析完毕) */
|
|
45
|
+
export interface SupplierRuntimeConfig {
|
|
46
|
+
enabled: boolean
|
|
47
|
+
lane: LaneAssignment
|
|
48
|
+
settings: Record<string, unknown>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 框架承诺给实例的全部环境;contentScript 为结构化最小类型,任何提供 onInvalidated 的 WXT 上下文均可满足 */
|
|
52
|
+
export interface PluginMountContext {
|
|
53
|
+
contentScript: { onInvalidated(cb: () => void): void }
|
|
54
|
+
config: SupplierRuntimeConfig
|
|
55
|
+
/** 供应商把自有 UI 内的用户修改写回框架的唯一通道;框架负责落盘与广播 */
|
|
56
|
+
emitConfigPatch: (patch: Record<string, unknown>) => void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface VideoSubtitlePluginInstance {
|
|
60
|
+
mount(): void | Promise<void>
|
|
61
|
+
unmount(): void | Promise<void>
|
|
62
|
+
setConfig(config: SupplierRuntimeConfig): void
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 供应商对宿主 manifest 的声明式需求;宿主装配 manifest 时取并集(构建期读取,声明模块必须零浏览器依赖) */
|
|
66
|
+
export interface HostRequirements {
|
|
67
|
+
permissions?: string[]
|
|
68
|
+
/** 供应商运行期直连的外部端点(翻译服务等);宿主映射到 manifest.host_permissions */
|
|
69
|
+
hostPermissions?: string[]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 内容侧插件定义——供应商包对宿主的唯一形状 */
|
|
73
|
+
export interface VideoSubtitlePlugin {
|
|
74
|
+
id: SupplierId
|
|
75
|
+
capabilities: VideoSubtitleCapabilities
|
|
76
|
+
settingsSchema: SettingsSchema
|
|
77
|
+
/** 供应商运行所需的宿主 manifest 权限等;缺此声明时宿主无从推导,只能靠运行时抛错发现(存储权限事故即此类) */
|
|
78
|
+
hostRequirements?: HostRequirements
|
|
79
|
+
/** 供应商需要的额外内容脚本(MAIN-world 桥等);描述性数据,由宿主 manifest 装配 */
|
|
80
|
+
contentScriptRequirements?: Array<{
|
|
81
|
+
kind: 'main-world-bridge'
|
|
82
|
+
entry: string
|
|
83
|
+
matches: string[]
|
|
84
|
+
}>
|
|
85
|
+
createInstance(ctx: PluginMountContext): VideoSubtitlePluginInstance
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** background 侧:一个扩展只有一个 service worker,供应商在此登记私产后端 */
|
|
89
|
+
export interface VideoSubtitlePluginBackground {
|
|
90
|
+
/** 宿主 SW 启动时调用一次;返回清理函数 */
|
|
91
|
+
setup(): () => void
|
|
92
|
+
setupOffscreen?(): () => void
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 框架存储端口——browser.storage.local 的极小投影,测试可注入假件 */
|
|
96
|
+
export interface FrameworkStoragePort {
|
|
97
|
+
get(key: string): Promise<unknown>
|
|
98
|
+
set(key: string, value: unknown): Promise<void>
|
|
99
|
+
/** 返回取消监听函数 */
|
|
100
|
+
onChange(cb: (key: string) => void): () => void
|
|
101
|
+
/** 可选:读取超时后立即重建底层通道(SW 冷启动/休眠后旧 Port 可能变僵尸——
|
|
102
|
+
* 等对端 onDisconnect 未必到来)。框架在重试前调用。 */
|
|
103
|
+
reset?(): void
|
|
104
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/index.ts
|
|
3
|
+
* 文件职责:框架包的公共出口——契约类型与运行时,宿主扩展的唯一导入面。
|
|
4
|
+
* 主要内容:重导出 contract 全部类型与 runtime 的 createVideoSubtitleFramework/unionMatches/FRAMEWORK_STORAGE_KEY。
|
|
5
|
+
* 模块边界:宿主只见本出口;内部实现文件不直接暴露。
|
|
6
|
+
*/
|
|
7
|
+
export * from './contract.ts'
|
|
8
|
+
export * from './runtime.ts'
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/runtime.ts
|
|
3
|
+
* 文件职责:框架运行时——读取命名空间配置、按启用序装配供应商实例、下发车道、响应配置变更与上下文失效。
|
|
4
|
+
* 主要内容:createVideoSubtitleFramework 四职责(读配置/生命周期/配置响应/车道仲裁)与 unionMatches 平台匹配表;
|
|
5
|
+
* 配置形状 { version, suppliers: { [id]: { enabled, settings } } },存储键 sublane:plugins.videoSubtitle。
|
|
6
|
+
* 模块边界:零 DOM、零字幕业务逻辑(宪法 NO-3);只编排实例生命周期,渲染/翻译/抓取全部归供应商。
|
|
7
|
+
*/
|
|
8
|
+
import type {
|
|
9
|
+
FrameworkStoragePort, PluginMountContext, SupplierRuntimeConfig,
|
|
10
|
+
VideoSubtitlePlugin, VideoSubtitlePluginInstance,
|
|
11
|
+
} from './contract.ts'
|
|
12
|
+
|
|
13
|
+
export const FRAMEWORK_STORAGE_KEY = 'sublane:plugins.videoSubtitle'
|
|
14
|
+
|
|
15
|
+
interface FrameworkConfig {
|
|
16
|
+
version: 1
|
|
17
|
+
suppliers: Record<string, { enabled: boolean; settings: Record<string, unknown> }>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 平台 → 内容脚本匹配;纯数据表,宿主经 unionMatches 取并集。
|
|
21
|
+
* 表必须覆盖两家供应商上游平台面的并集(read-frog: yt/xcom/netflix;fluent-read: yt/x +
|
|
22
|
+
* meet/teams/zoom/udemy/disney),缺行即该平台物理不注入(2026-10-29 审计教训:L1 注入面)。 */
|
|
23
|
+
export const PLATFORM_MATCHES: Record<string, string[]> = {
|
|
24
|
+
youtube: ['*://*.youtube.com/*', '*://youtube.com/*'],
|
|
25
|
+
'youtube-shorts': ['*://*.youtube.com/*', '*://youtube.com/*'],
|
|
26
|
+
'youtube-embed': ['*://*.youtube-nocookie.com/*'],
|
|
27
|
+
x: ['*://*.x.com/*', '*://x.com/*', '*://*.twitter.com/*', '*://twitter.com/*'],
|
|
28
|
+
netflix: ['*://www.netflix.com/*'],
|
|
29
|
+
meet: ['*://meet.google.com/*'],
|
|
30
|
+
teams: ['*://teams.microsoft.com/*', '*://teams.live.com/*', '*://teams.cloud.microsoft/*'],
|
|
31
|
+
zoom: ['*://*.zoom.us/*', '*://zoom.us/*', '*://*.zoom.com/*', '*://zoom.com/*'],
|
|
32
|
+
udemy: ['*://*.udemy.com/*', '*://udemy.com/*'],
|
|
33
|
+
disney: ['*://*.disneyplus.com/*', '*://disneyplus.com/*'],
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function unionMatches(plugins: Array<Pick<VideoSubtitlePlugin, 'capabilities'>>): string[] {
|
|
37
|
+
const matches = new Set<string>()
|
|
38
|
+
for (const plugin of plugins) {
|
|
39
|
+
for (const platform of plugin.capabilities.platforms) {
|
|
40
|
+
for (const pattern of PLATFORM_MATCHES[platform] ?? []) matches.add(pattern)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [...matches]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface CreateFrameworkOptions {
|
|
47
|
+
ctx: { onInvalidated(cb: () => void): void }
|
|
48
|
+
plugins: VideoSubtitlePlugin[]
|
|
49
|
+
storage: FrameworkStoragePort
|
|
50
|
+
log?: (msg: string) => void
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createVideoSubtitleFramework({ ctx, plugins, storage: storagePort, log = () => {} }: CreateFrameworkOptions) {
|
|
54
|
+
const running = new Map<string, { instance: VideoSubtitlePluginInstance; config: SupplierRuntimeConfig }>()
|
|
55
|
+
let stopped = false
|
|
56
|
+
/** 读取降级:超时/拒绝耗尽后拿到的是兜底空配置(真机实证 SW 卡死时页面会永久空挂,
|
|
57
|
+
* 只能靠存储变更事件才恢复——而卡死场景恰恰没有事件。降阶后有限退避重读,SW 恢复
|
|
58
|
+
* 即自动点亮,无需用户刷新)。 */
|
|
59
|
+
let readDegraded = false
|
|
60
|
+
|
|
61
|
+
async function readConfig(): Promise<FrameworkConfig> {
|
|
62
|
+
// 诊断标记:仅浏览器环境写 dataset(框架零 DOM 约束;node:test 无 document)
|
|
63
|
+
const mark = (name: string) => {
|
|
64
|
+
if (typeof document === 'undefined') return
|
|
65
|
+
const el = document.documentElement
|
|
66
|
+
el.dataset.sublaneBoot = `${el.dataset.sublaneBoot ?? ''}|rc:${name}`.slice(-400)
|
|
67
|
+
}
|
|
68
|
+
mark('get-begin')
|
|
69
|
+
// 存储端口是可靠性单点(中继 Port 可能因 SW 冷启动/休眠变僵尸:请求无应答且不触发
|
|
70
|
+
// onDisconnect;或 SW 断线瞬间请求在途被 reject)。单次读取限时 1.5s,超时或拒绝均
|
|
71
|
+
// reset() 立即重建通道再重试,共 3 次——拒绝若逃逸会经 onChange→applyConfig 路径变
|
|
72
|
+
// uncaught(真机 Errors 页实证),此处按可重试吸收;最坏 ~4.5s 内拿到配置或判空继续,
|
|
73
|
+
// storagePort.onChange 在端口恢复后仍会触发 applyConfig 补挂载(鲁棒:不信任任何未验证假设)。
|
|
74
|
+
for (let attempt = 1; attempt <= 3; attempt += 1) {
|
|
75
|
+
const saved = (await Promise.race([
|
|
76
|
+
storagePort.get(FRAMEWORK_STORAGE_KEY).catch((error: Error) => {
|
|
77
|
+
mark(`get-rejected:${attempt}:${error?.message?.slice(0, 24) ?? 'unknown'}`)
|
|
78
|
+
return 'REJECTED' as const
|
|
79
|
+
}),
|
|
80
|
+
new Promise<'TIMEOUT'>((resolve) => setTimeout(() => resolve('TIMEOUT'), 1500)),
|
|
81
|
+
])) as FrameworkConfig | 'TIMEOUT' | 'REJECTED' | undefined
|
|
82
|
+
if (saved === 'TIMEOUT' || saved === 'REJECTED') {
|
|
83
|
+
if (saved === 'TIMEOUT') mark(`get-timeout:${attempt}`)
|
|
84
|
+
storagePort.reset?.()
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
mark(`get-done:${saved ? Object.keys(saved.suppliers ?? {}).join('+') || 'empty' : 'null'}`)
|
|
88
|
+
readDegraded = false
|
|
89
|
+
if (!saved || typeof saved !== 'object') return { version: 1, suppliers: {} }
|
|
90
|
+
return saved
|
|
91
|
+
}
|
|
92
|
+
mark('get-exhausted')
|
|
93
|
+
readDegraded = true
|
|
94
|
+
return { version: 1, suppliers: {} }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function mountInstance(plugin: VideoSubtitlePlugin, config: SupplierRuntimeConfig) {
|
|
98
|
+
const ctxForInstance: PluginMountContext = {
|
|
99
|
+
contentScript: ctx,
|
|
100
|
+
config,
|
|
101
|
+
emitConfigPatch: (patch) => {
|
|
102
|
+
void (async () => {
|
|
103
|
+
const saved = await readConfig()
|
|
104
|
+
const entry = saved.suppliers[plugin.id] ?? { enabled: true, settings: {} }
|
|
105
|
+
entry.settings = { ...entry.settings, ...patch }
|
|
106
|
+
saved.suppliers[plugin.id] = entry
|
|
107
|
+
await storagePort.set(FRAMEWORK_STORAGE_KEY, saved)
|
|
108
|
+
})().catch((error) => log(`[${plugin.id}] emitConfigPatch 落盘失败:${error.message}`))
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
const instance = plugin.createInstance(ctxForInstance)
|
|
112
|
+
running.set(plugin.id, { instance, config })
|
|
113
|
+
void Promise.resolve(instance.mount()).catch((error) => {
|
|
114
|
+
log(`[${plugin.id}] mount 失败:${error?.message ?? error}`)
|
|
115
|
+
running.delete(plugin.id)
|
|
116
|
+
})
|
|
117
|
+
return instance
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function applyConfig(): Promise<void> {
|
|
121
|
+
if (stopped) return
|
|
122
|
+
const saved = await readConfig()
|
|
123
|
+
const enabled = plugins.filter((p) => saved.suppliers[p.id]?.enabled === true)
|
|
124
|
+
const total = enabled.length
|
|
125
|
+
|
|
126
|
+
for (const [id, run] of running) {
|
|
127
|
+
const stillEnabled = enabled.some((p) => p.id === id)
|
|
128
|
+
if (!stillEnabled) {
|
|
129
|
+
void Promise.resolve(run.instance.unmount()).catch((error) => log(`[${id}] unmount 失败:${error?.message ?? error}`))
|
|
130
|
+
running.delete(id)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
enabled.forEach((plugin, index) => {
|
|
134
|
+
const entry = saved.suppliers[plugin.id]!
|
|
135
|
+
const next: SupplierRuntimeConfig = {
|
|
136
|
+
enabled: true,
|
|
137
|
+
lane: { index, count: total },
|
|
138
|
+
settings: entry.settings ?? {},
|
|
139
|
+
}
|
|
140
|
+
const run = running.get(plugin.id)
|
|
141
|
+
if (!run) {
|
|
142
|
+
mountInstance(plugin, next)
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
const laneChanged = run.config.lane.index !== next.lane.index || run.config.lane.count !== next.lane.count
|
|
146
|
+
const settingsChanged = JSON.stringify(run.config.settings) !== JSON.stringify(next.settings)
|
|
147
|
+
if (laneChanged || settingsChanged) {
|
|
148
|
+
run.config = next
|
|
149
|
+
try { run.instance.setConfig(next) } catch (error) { log(`[${plugin.id}] setConfig 失败:${error?.message ?? error}`) }
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 降级自愈:读配置耗尽(兜底空配置)后按退避重读,成功即停(正常路径零开销)。 */
|
|
155
|
+
async function recoverDegradedRead(): Promise<void> {
|
|
156
|
+
const delays = [3000, 6000, 12000, 24000, 48000]
|
|
157
|
+
for (const delay of delays) {
|
|
158
|
+
await new Promise((resolve) => setTimeout(resolve, delay))
|
|
159
|
+
if (stopped || !readDegraded) return
|
|
160
|
+
mark('recovery-retry')
|
|
161
|
+
await applyConfig().catch(() => undefined)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
async start(): Promise<void> {
|
|
167
|
+
ctx.onInvalidated(() => {
|
|
168
|
+
stopped = true
|
|
169
|
+
for (const [, run] of running) {
|
|
170
|
+
void Promise.resolve(run.instance.unmount()).catch(() => undefined)
|
|
171
|
+
}
|
|
172
|
+
running.clear()
|
|
173
|
+
})
|
|
174
|
+
await applyConfig()
|
|
175
|
+
storagePort.onChange((key) => {
|
|
176
|
+
if (key !== FRAMEWORK_STORAGE_KEY) return
|
|
177
|
+
void applyConfig()
|
|
178
|
+
})
|
|
179
|
+
void recoverDegradedRead()
|
|
180
|
+
},
|
|
181
|
+
}
|
|
182
|
+
}
|