@xlinklight/browser-extension-tools 1.0.0-rc.0 → 1.0.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 +21 -21
- package/README.md +33 -33
- package/{assets → dist/assets}/shared/css/side-panel.css +94 -94
- package/dist/assets/shared/js/chrome-runtime.js +79 -0
- package/dist/assets/shared/js/extension-account-menu.js +212 -0
- package/dist/assets/shared/js/extension-i18n.js +35 -0
- package/dist/assets/shared/js/extension-session-store.js +40 -0
- package/dist/assets/shared/js/extension-tabs.js +27 -0
- package/dist/assets/shared/js/link-light-api-client.js +30 -0
- package/dist/assets/shared/js/side-panel-account.js +14 -0
- package/dist/assets/shared/js/side-panel-api.js +13 -0
- package/dist/assets/shared/js/side-panel-bootstrap.js +97 -0
- package/dist/assets/shared/js/side-panel-dom.js +14 -0
- package/dist/assets/shared/js/side-panel-i18n.js +34 -0
- package/dist/assets/shared/js/side-panel-import-preview.js +7 -0
- package/dist/assets/shared/js/side-panel-status.js +12 -0
- package/dist/assets/shared/js/side-panel-tabs.js +29 -0
- package/dist/assets/shared/js/side-panel-translations.js +30 -0
- package/dist/src/chrome-discovery.d.ts +57 -0
- package/dist/src/chrome-discovery.js +178 -0
- package/dist/src/chrome-for-testing.d.ts +66 -0
- package/dist/src/chrome-for-testing.js +157 -0
- package/dist/src/chrome-install-worker.d.ts +1 -0
- package/dist/src/chrome-install-worker.js +8 -0
- package/dist/src/cli.d.ts +41 -0
- package/dist/src/cli.js +189 -0
- package/dist/src/dev-cli.d.ts +2 -0
- package/dist/src/dev-cli.js +11 -0
- package/dist/src/dev-extension.d.ts +51 -0
- package/dist/src/dev-extension.js +307 -0
- package/dist/src/fs-error.d.ts +1 -0
- package/dist/src/fs-error.js +3 -0
- package/dist/src/package-cli.d.ts +2 -0
- package/dist/src/package-cli.js +9 -0
- package/dist/src/package.d.ts +15 -0
- package/dist/src/package.js +203 -0
- package/dist/src/profile.d.ts +34 -0
- package/dist/src/profile.js +105 -0
- package/dist/src/reload-server.d.ts +14 -0
- package/dist/src/reload-server.js +96 -0
- package/{src/run.mjs → dist/src/run.d.ts} +7 -7
- package/dist/src/run.js +7 -0
- package/dist/src/watcher.d.ts +17 -0
- package/dist/src/watcher.js +30 -0
- package/package.json +33 -11
- package/assets/shared/js/chrome-runtime.js +0 -87
- package/assets/shared/js/extension-account-menu.js +0 -239
- package/assets/shared/js/extension-i18n.js +0 -42
- package/assets/shared/js/extension-session-store.js +0 -42
- package/assets/shared/js/extension-tabs.js +0 -23
- package/assets/shared/js/link-light-api-client.js +0 -31
- package/assets/shared/js/side-panel-account.js +0 -14
- package/assets/shared/js/side-panel-api.js +0 -13
- package/assets/shared/js/side-panel-bootstrap.js +0 -127
- package/assets/shared/js/side-panel-dom.js +0 -15
- package/assets/shared/js/side-panel-i18n.js +0 -37
- package/assets/shared/js/side-panel-import-preview.js +0 -7
- package/assets/shared/js/side-panel-status.js +0 -13
- package/assets/shared/js/side-panel-tabs.js +0 -35
- package/assets/shared/js/side-panel-translations.js +0 -41
- package/src/chrome-discovery.mjs +0 -231
- package/src/chrome-for-testing.mjs +0 -203
- package/src/chrome-install-worker.mjs +0 -8
- package/src/cli.mjs +0 -171
- package/src/dev-cli.mjs +0 -9
- package/src/dev-extension.mjs +0 -350
- package/src/package-cli.mjs +0 -7
- package/src/package.mjs +0 -223
- package/src/profile.mjs +0 -144
- package/src/reload-server.mjs +0 -108
- package/src/watcher.mjs +0 -45
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 zopenge
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zopenge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
# Browser extension tools
|
|
2
|
-
|
|
3
|
-
This development package owns the reusable browser runtime assets, unpacked development preparation, profile management, reload companion and deterministic ZIP packager. Product extensions and their tests belong to the consuming repository. This package has no business extension list or default selection.
|
|
4
|
-
|
|
5
|
-
Install it as a development dependency. Both commands require an explicit source collection below the current repository and an explicit selection:
|
|
6
|
-
|
|
7
|
-
```sh
|
|
8
|
-
link-light-extension-dev --extension-root=extensions --extension=neutral-capture --app-url=http://localhost:3000 --watch-extension
|
|
9
|
-
link-light-extension-package --extension-root=extensions --extension=neutral-capture
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
`--extensions=one,two` and `--extensions=all` select multiple extensions from that same explicit collection. `--with-next` is rejected; the backend is an explicit independent application URL. The existing Chrome discovery and optional Chrome for Testing installation controls remain available.
|
|
13
|
-
|
|
14
|
-
Each extension directory owns its manifest and every other product asset. The tools copy every owned file, including additional workflow files and binary icons; the reserved top-level `shared` directory comes only from this package. Linked source entries are rejected. Public resources resolve through `@xlinklight/browser-extension-tools/shared/js/<file>.js` and `shared/css/<file>.css`; consumers do not read another repository's source.
|
|
15
|
-
|
|
16
|
-
The packager reads `config/local/extension.local.json`:
|
|
17
|
-
|
|
18
|
-
```json
|
|
19
|
-
{
|
|
20
|
-
"origin": "https://application.example",
|
|
21
|
-
"pluginToken": "local-secret",
|
|
22
|
-
"outputDir": ".tmp/browser-extensions/package/unpacked",
|
|
23
|
-
"zipFile": ".tmp/browser-extensions/package/extension.zip"
|
|
24
|
-
}
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
Outputs must stay below this repository's `.tmp/` or `dist/`. A single selection uses the configured paths exactly. Multiple selections use `<outputDir>/<name>/` and `<zipFile stem>-<name>.zip`. The manifest and configuration are generated for the explicit HTTPS origin; the ZIP has deterministic entry order and metadata. Source configuration is never changed.
|
|
28
|
-
|
|
29
|
-
Development output uses fixed `.tmp/browser-extensions/<name>/unpacked` directories and `.tmp/browser-extensions/profiles/<name>` (or `multiple`) profiles. Chrome for Testing uses `.tmp/browser-extensions/chrome-for-testing`. Repeated preparation replaces the same output; the visible build label derives from source bytes. Profiles remain until an explicit `--reset-profile` request. Watchers, reload servers and installer cancellation retain their owned cleanup behavior.
|
|
30
|
-
|
|
31
|
-
Public JavaScript entrypoints are `/dev`, `/cli`, `/package` and `/installer`. `packageChromeExtension({rootDir, extensionRoot, extensionNames, config?, logger?})` and `prepareDevExtension({rootDir, extensionRoot, extensionName, appUrl, reloadPort?, signal?})` also support controlled callers with explicit inputs.
|
|
32
|
-
|
|
33
|
-
Run `pnpm --filter @xlinklight/browser-extension-tools test` for filesystem and public API controls. Root extension tooling tests cover browser launch selection, profiles, cancellation and reload behavior without downloading a browser.
|
|
1
|
+
# Browser extension tools
|
|
2
|
+
|
|
3
|
+
This development package owns the reusable browser runtime assets, unpacked development preparation, profile management, reload companion and deterministic ZIP packager. Product extensions and their tests belong to the consuming repository. This package has no business extension list or default selection.
|
|
4
|
+
|
|
5
|
+
Install it as a development dependency. Both commands require an explicit source collection below the current repository and an explicit selection:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
link-light-extension-dev --extension-root=extensions --extension=neutral-capture --app-url=http://localhost:3000 --watch-extension
|
|
9
|
+
link-light-extension-package --extension-root=extensions --extension=neutral-capture
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`--extensions=one,two` and `--extensions=all` select multiple extensions from that same explicit collection. `--with-next` is rejected; the backend is an explicit independent application URL. The existing Chrome discovery and optional Chrome for Testing installation controls remain available.
|
|
13
|
+
|
|
14
|
+
Each extension directory owns its manifest and every other product asset. The tools copy every owned file, including additional workflow files and binary icons; the reserved top-level `shared` directory comes only from this package. Linked source entries are rejected. Public resources resolve through `@xlinklight/browser-extension-tools/shared/js/<file>.js` and `shared/css/<file>.css`; consumers do not read another repository's source.
|
|
15
|
+
|
|
16
|
+
The packager reads `config/local/extension.local.json`:
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
{
|
|
20
|
+
"origin": "https://application.example",
|
|
21
|
+
"pluginToken": "local-secret",
|
|
22
|
+
"outputDir": ".tmp/browser-extensions/package/unpacked",
|
|
23
|
+
"zipFile": ".tmp/browser-extensions/package/extension.zip"
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Outputs must stay below this repository's `.tmp/` or `dist/`. A single selection uses the configured paths exactly. Multiple selections use `<outputDir>/<name>/` and `<zipFile stem>-<name>.zip`. The manifest and configuration are generated for the explicit HTTPS origin; the ZIP has deterministic entry order and metadata. Source configuration is never changed.
|
|
28
|
+
|
|
29
|
+
Development output uses fixed `.tmp/browser-extensions/<name>/unpacked` directories and `.tmp/browser-extensions/profiles/<name>` (or `multiple`) profiles. Chrome for Testing uses `.tmp/browser-extensions/chrome-for-testing`. Repeated preparation replaces the same output; the visible build label derives from source bytes. Profiles remain until an explicit `--reset-profile` request. Watchers, reload servers and installer cancellation retain their owned cleanup behavior.
|
|
30
|
+
|
|
31
|
+
Public JavaScript entrypoints are `/dev`, `/cli`, `/package` and `/installer`. `packageChromeExtension({rootDir, extensionRoot, extensionNames, config?, logger?})` and `prepareDevExtension({rootDir, extensionRoot, extensionName, appUrl, reloadPort?, signal?})` also support controlled callers with explicit inputs.
|
|
32
|
+
|
|
33
|
+
Run `pnpm --filter @xlinklight/browser-extension-tools test` for filesystem and public API controls. Root extension tooling tests cover browser launch selection, profiles, cancellation and reload behavior without downloading a browser.
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
:root {
|
|
2
|
-
color-scheme: light;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
* {
|
|
6
|
-
box-sizing: border-box;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
body {
|
|
10
|
-
margin: 0;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
button,
|
|
14
|
-
input,
|
|
15
|
-
select,
|
|
16
|
-
textarea {
|
|
17
|
-
font: inherit;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
.ll-tab-page[hidden],
|
|
21
|
-
.ll-user-menu[hidden] {
|
|
22
|
-
display: none;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
.ll-account {
|
|
26
|
-
display: grid;
|
|
27
|
-
place-items: center;
|
|
28
|
-
width: 38px;
|
|
29
|
-
height: 38px;
|
|
30
|
-
margin-top: auto;
|
|
31
|
-
border: 0;
|
|
32
|
-
border-radius: 999px;
|
|
33
|
-
color: #ffffff;
|
|
34
|
-
background: var(--ll-account-color, #0f766e);
|
|
35
|
-
cursor: pointer;
|
|
36
|
-
font-size: 12px;
|
|
37
|
-
font-weight: 800;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
.ll-user-menu {
|
|
41
|
-
position: fixed;
|
|
42
|
-
right: 62px;
|
|
43
|
-
bottom: 14px;
|
|
44
|
-
z-index: 10;
|
|
45
|
-
display: grid;
|
|
46
|
-
gap: 8px;
|
|
47
|
-
width: min(260px, calc(100vw - 84px));
|
|
48
|
-
padding: 12px;
|
|
49
|
-
border: 1px solid #e2e8f0;
|
|
50
|
-
border-radius: 14px;
|
|
51
|
-
background: #ffffff;
|
|
52
|
-
box-shadow: 0 18px 45px rgb(15 23 42 / 18%);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
.ll-user-profile {
|
|
56
|
-
display: grid;
|
|
57
|
-
gap: 2px;
|
|
58
|
-
padding-bottom: 8px;
|
|
59
|
-
border-bottom: 1px solid #eef2f7;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
.ll-user-email {
|
|
63
|
-
color: #0f172a;
|
|
64
|
-
font-size: 13px;
|
|
65
|
-
font-weight: 700;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
.ll-user-workspace,
|
|
69
|
-
.ll-user-plan {
|
|
70
|
-
color: #64748b;
|
|
71
|
-
font-size: 12px;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
.ll-user-plan {
|
|
75
|
-
padding: 9px;
|
|
76
|
-
border-radius: 10px;
|
|
77
|
-
color: var(--ll-account-plan-color, #0f766e);
|
|
78
|
-
background: var(--ll-account-plan-bg, #ecfeff);
|
|
79
|
-
font-weight: 700;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
.ll-menu-action {
|
|
83
|
-
min-height: 32px;
|
|
84
|
-
border: 0;
|
|
85
|
-
border-radius: 8px;
|
|
86
|
-
color: #172033;
|
|
87
|
-
background: transparent;
|
|
88
|
-
text-align: left;
|
|
89
|
-
cursor: pointer;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
.ll-menu-action:hover {
|
|
93
|
-
background: #f8fafc;
|
|
94
|
-
}
|
|
1
|
+
:root {
|
|
2
|
+
color-scheme: light;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
* {
|
|
6
|
+
box-sizing: border-box;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
body {
|
|
10
|
+
margin: 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
button,
|
|
14
|
+
input,
|
|
15
|
+
select,
|
|
16
|
+
textarea {
|
|
17
|
+
font: inherit;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.ll-tab-page[hidden],
|
|
21
|
+
.ll-user-menu[hidden] {
|
|
22
|
+
display: none;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.ll-account {
|
|
26
|
+
display: grid;
|
|
27
|
+
place-items: center;
|
|
28
|
+
width: 38px;
|
|
29
|
+
height: 38px;
|
|
30
|
+
margin-top: auto;
|
|
31
|
+
border: 0;
|
|
32
|
+
border-radius: 999px;
|
|
33
|
+
color: #ffffff;
|
|
34
|
+
background: var(--ll-account-color, #0f766e);
|
|
35
|
+
cursor: pointer;
|
|
36
|
+
font-size: 12px;
|
|
37
|
+
font-weight: 800;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.ll-user-menu {
|
|
41
|
+
position: fixed;
|
|
42
|
+
right: 62px;
|
|
43
|
+
bottom: 14px;
|
|
44
|
+
z-index: 10;
|
|
45
|
+
display: grid;
|
|
46
|
+
gap: 8px;
|
|
47
|
+
width: min(260px, calc(100vw - 84px));
|
|
48
|
+
padding: 12px;
|
|
49
|
+
border: 1px solid #e2e8f0;
|
|
50
|
+
border-radius: 14px;
|
|
51
|
+
background: #ffffff;
|
|
52
|
+
box-shadow: 0 18px 45px rgb(15 23 42 / 18%);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.ll-user-profile {
|
|
56
|
+
display: grid;
|
|
57
|
+
gap: 2px;
|
|
58
|
+
padding-bottom: 8px;
|
|
59
|
+
border-bottom: 1px solid #eef2f7;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.ll-user-email {
|
|
63
|
+
color: #0f172a;
|
|
64
|
+
font-size: 13px;
|
|
65
|
+
font-weight: 700;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.ll-user-workspace,
|
|
69
|
+
.ll-user-plan {
|
|
70
|
+
color: #64748b;
|
|
71
|
+
font-size: 12px;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.ll-user-plan {
|
|
75
|
+
padding: 9px;
|
|
76
|
+
border-radius: 10px;
|
|
77
|
+
color: var(--ll-account-plan-color, #0f766e);
|
|
78
|
+
background: var(--ll-account-plan-bg, #ecfeff);
|
|
79
|
+
font-weight: 700;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.ll-menu-action {
|
|
83
|
+
min-height: 32px;
|
|
84
|
+
border: 0;
|
|
85
|
+
border-radius: 8px;
|
|
86
|
+
color: #172033;
|
|
87
|
+
background: transparent;
|
|
88
|
+
text-align: left;
|
|
89
|
+
cursor: pointer;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.ll-menu-action:hover {
|
|
93
|
+
background: #f8fafc;
|
|
94
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightChromeRuntime(global) {
|
|
3
|
+
function normalizeOrigin(value) {
|
|
4
|
+
return String(value || "http://localhost:3000").replace(/\/+$/, "");
|
|
5
|
+
}
|
|
6
|
+
function normalizeLocale(locale) {
|
|
7
|
+
const normalized = String(locale || "").toLowerCase();
|
|
8
|
+
if (normalized.startsWith("en"))
|
|
9
|
+
return "en-US";
|
|
10
|
+
return "zh-CN";
|
|
11
|
+
}
|
|
12
|
+
function createChromeRuntime(options = {}) {
|
|
13
|
+
const chromeApi = options.chromeApi || global.chrome;
|
|
14
|
+
const config = options.config || global.LINK_LIGHT_CONFIG || {};
|
|
15
|
+
const origin = normalizeOrigin(config.origin);
|
|
16
|
+
const pluginToken = String(config.pluginToken || "");
|
|
17
|
+
const storage = {
|
|
18
|
+
async get(keys) {
|
|
19
|
+
return (await chromeApi?.storage?.local?.get?.(keys)) || {};
|
|
20
|
+
},
|
|
21
|
+
async set(values) {
|
|
22
|
+
await chromeApi?.storage?.local?.set?.(values);
|
|
23
|
+
},
|
|
24
|
+
async remove(keys) {
|
|
25
|
+
await chromeApi?.storage?.local?.remove?.(keys);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
async function loadLocaleMessages(directory) {
|
|
29
|
+
const url = chromeApi?.runtime?.getURL
|
|
30
|
+
? chromeApi.runtime.getURL(`_locales/${directory}/messages.json`)
|
|
31
|
+
: `_locales/${directory}/messages.json`;
|
|
32
|
+
const response = await global.fetch(url);
|
|
33
|
+
return response.json();
|
|
34
|
+
}
|
|
35
|
+
async function sendMessage(message) {
|
|
36
|
+
return chromeApi?.runtime?.sendMessage?.(message);
|
|
37
|
+
}
|
|
38
|
+
async function requestOrigins(origins) {
|
|
39
|
+
if (!chromeApi?.permissions?.request)
|
|
40
|
+
return true;
|
|
41
|
+
return chromeApi.permissions.request({ origins });
|
|
42
|
+
}
|
|
43
|
+
async function containsOrigins(origins) {
|
|
44
|
+
if (!chromeApi?.permissions?.contains)
|
|
45
|
+
return false;
|
|
46
|
+
return chromeApi.permissions.contains({ origins });
|
|
47
|
+
}
|
|
48
|
+
function openPopup(url, options = {}) {
|
|
49
|
+
const width = options.width || 1280;
|
|
50
|
+
const height = options.height || 820;
|
|
51
|
+
if (chromeApi?.windows?.create) {
|
|
52
|
+
chromeApi.windows.create({ url, type: "popup", width, height }).catch(() => {
|
|
53
|
+
global.open(url, "_blank", "noopener");
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
global.open(url, "_blank", "noopener");
|
|
58
|
+
}
|
|
59
|
+
function getUILocale() {
|
|
60
|
+
return normalizeLocale(chromeApi?.i18n?.getMessage?.("@@ui_locale"));
|
|
61
|
+
}
|
|
62
|
+
function getExtensionUrl(path) {
|
|
63
|
+
return chromeApi?.runtime?.getURL ? chromeApi.runtime.getURL(path) : path;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
containsOrigins,
|
|
67
|
+
getExtensionUrl,
|
|
68
|
+
getUILocale,
|
|
69
|
+
loadLocaleMessages,
|
|
70
|
+
openPopup,
|
|
71
|
+
origin,
|
|
72
|
+
pluginToken,
|
|
73
|
+
requestOrigins,
|
|
74
|
+
sendMessage,
|
|
75
|
+
storage,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
Object.assign(global, { LinkLightChromeRuntime: { createChromeRuntime } });
|
|
79
|
+
})(globalThis);
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightAccountMenu(global) {
|
|
3
|
+
const defaultKeys = {
|
|
4
|
+
token: "linkLightSessionToken",
|
|
5
|
+
expiresAt: "linkLightSessionExpiresAt",
|
|
6
|
+
user: "linkLightUserSummary",
|
|
7
|
+
workspace: "linkLightWorkspaceSummary",
|
|
8
|
+
};
|
|
9
|
+
const pairingStorageKey = "linkLightPairing";
|
|
10
|
+
const pairingPollIntervalMs = 2000;
|
|
11
|
+
function createExtensionAccountMenu({ runtime, sessionStore = global.LinkLightSessionStore.createExtensionSessionStore(runtime, defaultKeys), documentRef = global.document, t = (key) => key, getLocale = () => "zh-CN", setStatus = () => undefined, onStateChange = () => undefined, onSignedOut = () => undefined, openUrl = (url) => global.open(url, "_blank", "noopener"), deviceName = "Chrome Side Panel", }) {
|
|
12
|
+
const accountButton = documentRef.querySelector("#ll-account-button");
|
|
13
|
+
const userMenu = documentRef.querySelector("#ll-user-menu");
|
|
14
|
+
const userEmail = documentRef.querySelector("#ll-user-email");
|
|
15
|
+
const userWorkspace = documentRef.querySelector("#ll-user-workspace");
|
|
16
|
+
const userPlan = documentRef.querySelector("#ll-user-plan");
|
|
17
|
+
const signInButton = documentRef.querySelector("#ll-account-sign-in");
|
|
18
|
+
const accountOpenButton = documentRef.querySelector("#ll-account-open");
|
|
19
|
+
const accountDevicesButton = documentRef.querySelector("#ll-account-devices");
|
|
20
|
+
const accountHelpButton = documentRef.querySelector("#ll-account-help");
|
|
21
|
+
const accountFeedbackButton = documentRef.querySelector("#ll-account-feedback");
|
|
22
|
+
const accountLogoutButton = documentRef.querySelector("#ll-account-logout");
|
|
23
|
+
let accountState = { token: "", expiresAt: "", user: null, workspace: null };
|
|
24
|
+
function setMenuExpanded(expanded) {
|
|
25
|
+
if (userMenu)
|
|
26
|
+
userMenu.hidden = !expanded;
|
|
27
|
+
accountButton?.setAttribute("aria-expanded", expanded ? "true" : "false");
|
|
28
|
+
}
|
|
29
|
+
function openMenu() {
|
|
30
|
+
setMenuExpanded(true);
|
|
31
|
+
}
|
|
32
|
+
function closeMenu() {
|
|
33
|
+
setMenuExpanded(false);
|
|
34
|
+
}
|
|
35
|
+
function toggleMenu() {
|
|
36
|
+
if (userMenu && !userMenu.hidden)
|
|
37
|
+
closeMenu();
|
|
38
|
+
else
|
|
39
|
+
openMenu();
|
|
40
|
+
}
|
|
41
|
+
function isMenuEventTarget(target) {
|
|
42
|
+
return Boolean(target
|
|
43
|
+
&& (accountButton?.contains(target) || userMenu?.contains(target)));
|
|
44
|
+
}
|
|
45
|
+
function handleDocumentClick(event) {
|
|
46
|
+
if (!isMenuEventTarget(event.target))
|
|
47
|
+
closeMenu();
|
|
48
|
+
}
|
|
49
|
+
function handleDocumentKeydown(event) {
|
|
50
|
+
if (event.key === "Escape")
|
|
51
|
+
closeMenu();
|
|
52
|
+
}
|
|
53
|
+
function accountUrl(path = "") {
|
|
54
|
+
return `${runtime.origin}/${getLocale()}${path}`;
|
|
55
|
+
}
|
|
56
|
+
function isSignedIn() {
|
|
57
|
+
return sessionStore.isSignedIn(accountState);
|
|
58
|
+
}
|
|
59
|
+
function headers() {
|
|
60
|
+
return sessionStore.headers(accountState);
|
|
61
|
+
}
|
|
62
|
+
function getAccountState() {
|
|
63
|
+
return accountState;
|
|
64
|
+
}
|
|
65
|
+
function render() {
|
|
66
|
+
const signedIn = isSignedIn();
|
|
67
|
+
if (accountButton) {
|
|
68
|
+
accountButton.textContent = signedIn ? "L" : "?";
|
|
69
|
+
if (!accountButton.hasAttribute("aria-expanded"))
|
|
70
|
+
accountButton.setAttribute("aria-expanded", "false");
|
|
71
|
+
}
|
|
72
|
+
if (userEmail) {
|
|
73
|
+
userEmail.textContent = signedIn
|
|
74
|
+
? accountState.user?.email || accountState.user?.id || t("accountMenuSignedIn")
|
|
75
|
+
: t("accountMenuSignedOut");
|
|
76
|
+
}
|
|
77
|
+
if (userWorkspace)
|
|
78
|
+
userWorkspace.textContent = signedIn ? accountState.workspace?.name || accountState.workspace?.id || "" : "";
|
|
79
|
+
if (userPlan)
|
|
80
|
+
userPlan.textContent = signedIn ? t("accountMenuPlan") : t("accountMenuSignInRequired");
|
|
81
|
+
if (signInButton)
|
|
82
|
+
signInButton.hidden = signedIn;
|
|
83
|
+
if (accountOpenButton)
|
|
84
|
+
accountOpenButton.hidden = !signedIn;
|
|
85
|
+
if (accountDevicesButton)
|
|
86
|
+
accountDevicesButton.hidden = !signedIn;
|
|
87
|
+
if (accountLogoutButton)
|
|
88
|
+
accountLogoutButton.hidden = !signedIn;
|
|
89
|
+
onStateChange({ signedIn, state: accountState });
|
|
90
|
+
}
|
|
91
|
+
async function readAccountState() {
|
|
92
|
+
accountState = await sessionStore.read();
|
|
93
|
+
render();
|
|
94
|
+
return accountState;
|
|
95
|
+
}
|
|
96
|
+
async function saveApprovedSession(data) {
|
|
97
|
+
accountState = {
|
|
98
|
+
token: String(data.sessionToken || ""),
|
|
99
|
+
expiresAt: String(data.expiresAt || ""),
|
|
100
|
+
user: data.user || null,
|
|
101
|
+
workspace: data.workspace || null,
|
|
102
|
+
};
|
|
103
|
+
await sessionStore.save(accountState);
|
|
104
|
+
render();
|
|
105
|
+
}
|
|
106
|
+
async function startPairing() {
|
|
107
|
+
setStatus("statusPairingStarted");
|
|
108
|
+
const response = await global.fetch(`${runtime.origin}/api/auth/extension/pairings`, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: { "Content-Type": "application/json" },
|
|
111
|
+
body: JSON.stringify({ deviceName, locale: getLocale() }),
|
|
112
|
+
});
|
|
113
|
+
const data = await response.json();
|
|
114
|
+
if (!response.ok)
|
|
115
|
+
throw new Error(data?.error?.code || "errorPairingFailed");
|
|
116
|
+
await runtime.storage.set({ [pairingStorageKey]: data });
|
|
117
|
+
openUrl(data.verificationUrl);
|
|
118
|
+
await pollExtensionPairingUntilApproved(data);
|
|
119
|
+
}
|
|
120
|
+
async function pollExtensionPairing(pairing) {
|
|
121
|
+
const response = await global.fetch(`${runtime.origin}/api/auth/extension/pairings/${pairing.pairingId}`, {
|
|
122
|
+
headers: { Authorization: `Bearer ${pairing.pairingToken}` },
|
|
123
|
+
});
|
|
124
|
+
const data = await response.json();
|
|
125
|
+
if (response.ok && data.status === "approved") {
|
|
126
|
+
await saveApprovedSession(data);
|
|
127
|
+
await runtime.storage.remove([pairingStorageKey]);
|
|
128
|
+
setStatus("statusPairingApproved");
|
|
129
|
+
return data;
|
|
130
|
+
}
|
|
131
|
+
setStatus("statusPairingPending");
|
|
132
|
+
return data;
|
|
133
|
+
}
|
|
134
|
+
async function pollExtensionPairingUntilApproved(pairing) {
|
|
135
|
+
const expiresAt = Date.parse(pairing.expiresAt);
|
|
136
|
+
while (Number.isNaN(expiresAt) || Date.now() < expiresAt) {
|
|
137
|
+
const result = await pollExtensionPairing(pairing);
|
|
138
|
+
if (result?.status === "approved")
|
|
139
|
+
return;
|
|
140
|
+
await waitForPairingPoll();
|
|
141
|
+
}
|
|
142
|
+
await runtime.storage.remove([pairingStorageKey]);
|
|
143
|
+
setStatus("statusPairingExpired");
|
|
144
|
+
}
|
|
145
|
+
function waitForPairingPoll() {
|
|
146
|
+
return new Promise((resolve) => global.setTimeout(resolve, pairingPollIntervalMs));
|
|
147
|
+
}
|
|
148
|
+
async function logout() {
|
|
149
|
+
if (accountState.token) {
|
|
150
|
+
await global.fetch(`${runtime.origin}/api/auth/extension/sessions/revoke`, {
|
|
151
|
+
method: "POST",
|
|
152
|
+
headers: { Authorization: `Bearer ${accountState.token}` },
|
|
153
|
+
}).catch(() => null);
|
|
154
|
+
}
|
|
155
|
+
await sessionStore.clear([pairingStorageKey]);
|
|
156
|
+
accountState = { token: "", expiresAt: "", user: null, workspace: null };
|
|
157
|
+
onSignedOut();
|
|
158
|
+
render();
|
|
159
|
+
setStatus("statusSignedOut");
|
|
160
|
+
}
|
|
161
|
+
function initializeActions() {
|
|
162
|
+
closeMenu();
|
|
163
|
+
accountButton?.addEventListener("click", (event) => {
|
|
164
|
+
event.stopPropagation();
|
|
165
|
+
toggleMenu();
|
|
166
|
+
});
|
|
167
|
+
signInButton?.addEventListener("click", async () => {
|
|
168
|
+
closeMenu();
|
|
169
|
+
try {
|
|
170
|
+
await startPairing();
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
setStatus(error instanceof Error && t(error.message) !== error.message ? error.message : "errorPairingFailed");
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
accountOpenButton?.addEventListener("click", () => {
|
|
177
|
+
closeMenu();
|
|
178
|
+
openUrl(accountUrl("/account"));
|
|
179
|
+
});
|
|
180
|
+
accountDevicesButton?.addEventListener("click", () => {
|
|
181
|
+
closeMenu();
|
|
182
|
+
openUrl(accountUrl("/account/devices"));
|
|
183
|
+
});
|
|
184
|
+
accountHelpButton?.addEventListener("click", () => {
|
|
185
|
+
closeMenu();
|
|
186
|
+
openUrl(accountUrl(""));
|
|
187
|
+
});
|
|
188
|
+
accountFeedbackButton?.addEventListener("click", () => {
|
|
189
|
+
closeMenu();
|
|
190
|
+
openUrl(accountUrl("/account"));
|
|
191
|
+
});
|
|
192
|
+
accountLogoutButton?.addEventListener("click", async () => {
|
|
193
|
+
closeMenu();
|
|
194
|
+
await logout();
|
|
195
|
+
});
|
|
196
|
+
documentRef.addEventListener("click", handleDocumentClick);
|
|
197
|
+
documentRef.addEventListener("keydown", handleDocumentKeydown);
|
|
198
|
+
global.addEventListener("blur", closeMenu);
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
getAccountState,
|
|
202
|
+
headers,
|
|
203
|
+
initializeActions,
|
|
204
|
+
isSignedIn,
|
|
205
|
+
logout,
|
|
206
|
+
readAccountState,
|
|
207
|
+
render,
|
|
208
|
+
startPairing,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
Object.assign(global, { LinkLightAccountMenu: { createExtensionAccountMenu } });
|
|
212
|
+
})(globalThis);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightExtensionI18n(global) {
|
|
3
|
+
function createExtensionI18n(runtime, options = {}) {
|
|
4
|
+
const fallbackLocale = options.fallbackLocale || "zh-CN";
|
|
5
|
+
const localeDirectories = options.localeDirectories || { "zh-CN": "zh_CN", "en-US": "en" };
|
|
6
|
+
let messages = {};
|
|
7
|
+
function normalizeLanguagePreference(value) {
|
|
8
|
+
if (value === "zh-CN" || value === "en-US")
|
|
9
|
+
return value;
|
|
10
|
+
return "auto";
|
|
11
|
+
}
|
|
12
|
+
function resolveLocale(preference) {
|
|
13
|
+
if (preference === "zh-CN" || preference === "en-US")
|
|
14
|
+
return preference;
|
|
15
|
+
return runtime.getUILocale ? runtime.getUILocale() : fallbackLocale;
|
|
16
|
+
}
|
|
17
|
+
async function loadMessages(locale) {
|
|
18
|
+
const directory = localeDirectories[locale] ?? localeDirectories[fallbackLocale];
|
|
19
|
+
const catalog = await runtime.loadLocaleMessages(directory);
|
|
20
|
+
messages = Object.fromEntries(Object.entries(catalog).map(([key, value]) => [key, typeof value?.message === "string" ? value.message : ""]));
|
|
21
|
+
return messages;
|
|
22
|
+
}
|
|
23
|
+
function interpolate(template, values = {}) {
|
|
24
|
+
return String(template || "").replace(/\{(\w+)\}/g, (_match, key) => String(values[key] ?? ""));
|
|
25
|
+
}
|
|
26
|
+
function t(key, values = {}) {
|
|
27
|
+
return interpolate(messages[key] ?? key, values);
|
|
28
|
+
}
|
|
29
|
+
function hasMessage(key) {
|
|
30
|
+
return Object.prototype.hasOwnProperty.call(messages, key);
|
|
31
|
+
}
|
|
32
|
+
return { hasMessage, loadMessages, normalizeLanguagePreference, resolveLocale, t };
|
|
33
|
+
}
|
|
34
|
+
Object.assign(global, { LinkLightExtensionI18n: { createExtensionI18n } });
|
|
35
|
+
})(globalThis);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSessionStore(global) {
|
|
3
|
+
function createExtensionSessionStore(runtime, keys) {
|
|
4
|
+
async function read() {
|
|
5
|
+
const result = await runtime.storage.get(Object.values(keys));
|
|
6
|
+
return {
|
|
7
|
+
expiresAt: String(result?.[keys.expiresAt] || ""),
|
|
8
|
+
token: String(result?.[keys.token] || ""),
|
|
9
|
+
user: keys.user ? result?.[keys.user] || null : null,
|
|
10
|
+
workspace: keys.workspace ? result?.[keys.workspace] || null : null,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
async function save(state) {
|
|
14
|
+
const values = {
|
|
15
|
+
[keys.token]: state.token || "",
|
|
16
|
+
[keys.expiresAt]: state.expiresAt || "",
|
|
17
|
+
};
|
|
18
|
+
if (keys.user)
|
|
19
|
+
values[keys.user] = state.user || null;
|
|
20
|
+
if (keys.workspace)
|
|
21
|
+
values[keys.workspace] = state.workspace || null;
|
|
22
|
+
await runtime.storage.set(values);
|
|
23
|
+
}
|
|
24
|
+
async function clear(extraKeys = []) {
|
|
25
|
+
await runtime.storage.remove([...Object.values(keys), ...extraKeys]);
|
|
26
|
+
}
|
|
27
|
+
function isSignedIn(state) {
|
|
28
|
+
return Boolean(state?.token && state?.expiresAt && new Date(state.expiresAt).getTime() > Date.now());
|
|
29
|
+
}
|
|
30
|
+
function headers(state) {
|
|
31
|
+
const requestHeaders = { "Content-Type": "application/json" };
|
|
32
|
+
if (!isSignedIn(state))
|
|
33
|
+
throw new Error("authentication_required");
|
|
34
|
+
requestHeaders.Authorization = `Bearer ${state.token}`;
|
|
35
|
+
return requestHeaders;
|
|
36
|
+
}
|
|
37
|
+
return { clear, headers, isSignedIn, read, save };
|
|
38
|
+
}
|
|
39
|
+
global.LinkLightSessionStore = { createExtensionSessionStore };
|
|
40
|
+
})(globalThis);
|