@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
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightExtensionTabs(global) {
|
|
3
|
+
function initializeSidePanelTabs({ documentRef = global.document, getTab, onActivate }) {
|
|
4
|
+
for (const button of documentRef.querySelectorAll("[data-tab]")) {
|
|
5
|
+
if (button.dataset.disabled === "true")
|
|
6
|
+
continue;
|
|
7
|
+
const tab = getTab(button.dataset.tab);
|
|
8
|
+
if (!tab)
|
|
9
|
+
continue;
|
|
10
|
+
button.addEventListener("click", () => onActivate(tab.id));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function activateTab(tabId, { documentRef = global.document } = {}) {
|
|
14
|
+
for (const button of documentRef.querySelectorAll("[data-tab]")) {
|
|
15
|
+
if (button.dataset.disabled === "true")
|
|
16
|
+
continue;
|
|
17
|
+
if (button.dataset.tab === tabId)
|
|
18
|
+
button.setAttribute("aria-current", "page");
|
|
19
|
+
else
|
|
20
|
+
button.removeAttribute("aria-current");
|
|
21
|
+
}
|
|
22
|
+
for (const panel of documentRef.querySelectorAll("[data-tab-panel]")) {
|
|
23
|
+
panel.hidden = panel.dataset.tabPanel !== tabId;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
Object.assign(global, { LinkLightExtensionTabs: { activateTab, initializeSidePanelTabs } });
|
|
27
|
+
})(globalThis);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightApiClient(global) {
|
|
3
|
+
function createLinkLightApiClient(runtime, sessionStore, getAccountState) {
|
|
4
|
+
function resolveUrl(path) {
|
|
5
|
+
if (/^https?:\/\//.test(path))
|
|
6
|
+
return path;
|
|
7
|
+
return `${runtime.origin}${path.startsWith("/") ? path : `/${path}`}`;
|
|
8
|
+
}
|
|
9
|
+
function headers(extraHeaders = {}) {
|
|
10
|
+
const state = typeof getAccountState === "function" ? getAccountState() : null;
|
|
11
|
+
return {
|
|
12
|
+
...(state && sessionStore ? sessionStore.headers(state) : { "Content-Type": "application/json" }),
|
|
13
|
+
...(runtime.pluginToken ? { "x-link-light-plugin-token": runtime.pluginToken } : {}),
|
|
14
|
+
...extraHeaders,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async function request(path, init = {}, options = {}) {
|
|
18
|
+
const response = await global.fetch(resolveUrl(path), {
|
|
19
|
+
...init,
|
|
20
|
+
headers: options.auth === false ? { "Content-Type": "application/json", ...(init.headers || {}) } : headers(init.headers),
|
|
21
|
+
});
|
|
22
|
+
const data = await response.json();
|
|
23
|
+
if (!response.ok)
|
|
24
|
+
throw new Error(data?.error?.code || options.fallbackError || "link_light_request_failed");
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
return { headers, request, resolveUrl };
|
|
28
|
+
}
|
|
29
|
+
Object.assign(global, { LinkLightApiClient: { createLinkLightApiClient } });
|
|
30
|
+
})(globalThis);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelAccount(global) {
|
|
3
|
+
function isStoredSessionSignedIn(accountState, now = Date.now()) {
|
|
4
|
+
return Boolean(accountState.token && accountState.expiresAt && new Date(accountState.expiresAt).getTime() > now);
|
|
5
|
+
}
|
|
6
|
+
function getStoredSessionHeaders(accountState) {
|
|
7
|
+
const headers = { "Content-Type": "application/json" };
|
|
8
|
+
if (!isStoredSessionSignedIn(accountState))
|
|
9
|
+
throw new Error("authentication_required");
|
|
10
|
+
headers.Authorization = `Bearer ${accountState.token}`;
|
|
11
|
+
return headers;
|
|
12
|
+
}
|
|
13
|
+
Object.assign(global, { LinkLightSidePanelAccount: { getStoredSessionHeaders, isStoredSessionSignedIn } });
|
|
14
|
+
})(globalThis);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelApi(global) {
|
|
3
|
+
async function requestJson({ origin, path, headers, body }) {
|
|
4
|
+
const response = await global.fetch(`${origin}${path}`, {
|
|
5
|
+
method: "POST",
|
|
6
|
+
headers,
|
|
7
|
+
body: JSON.stringify(body),
|
|
8
|
+
});
|
|
9
|
+
const data = await response.json();
|
|
10
|
+
return { data, response };
|
|
11
|
+
}
|
|
12
|
+
Object.assign(global, { LinkLightSidePanelApi: { requestJson } });
|
|
13
|
+
})(globalThis);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelBootstrap(global) {
|
|
3
|
+
function createSidePanelBootstrap({ extensionRuntime, languagePreferenceStorageKey, localeDirectories, fallbackLocale = "zh-CN", sidePanelTabs, elements, initialStatusKey = "statusWaiting", onAfterStaticTranslations, onDisabledTab, onTabActivated, onLanguageChanged, }) {
|
|
4
|
+
const { interpolate, loadLocaleMessageMap, normalizeLanguagePreference, resolveLocale } = global.LinkLightSidePanelI18n;
|
|
5
|
+
const { updateCount, updateStatus } = global.LinkLightSidePanelStatus;
|
|
6
|
+
const { applySidePanelTranslations } = global.LinkLightSidePanelTranslations;
|
|
7
|
+
const { activateSidePanelTab, initializeSidePanelTabs: initializeSharedSidePanelTabs, } = global.LinkLightSidePanelTabs;
|
|
8
|
+
let messages = {};
|
|
9
|
+
let languagePreference = "auto";
|
|
10
|
+
let activeLocale = fallbackLocale;
|
|
11
|
+
const activeStatus = { key: initialStatusKey, values: {} };
|
|
12
|
+
function t(key, values = {}) {
|
|
13
|
+
return interpolate(messages[key] ?? key, values);
|
|
14
|
+
}
|
|
15
|
+
async function readLanguagePreference() {
|
|
16
|
+
const result = await extensionRuntime.storage.get([languagePreferenceStorageKey]);
|
|
17
|
+
return normalizeLanguagePreference(result?.[languagePreferenceStorageKey]);
|
|
18
|
+
}
|
|
19
|
+
async function saveLanguagePreference(preference) {
|
|
20
|
+
await extensionRuntime.storage.set({ [languagePreferenceStorageKey]: normalizeLanguagePreference(preference) });
|
|
21
|
+
}
|
|
22
|
+
async function loadMessages(locale) {
|
|
23
|
+
messages = await loadLocaleMessageMap({ runtime: extensionRuntime, locale, localeDirectories, fallbackLocale });
|
|
24
|
+
}
|
|
25
|
+
function applyTranslations() {
|
|
26
|
+
applySidePanelTranslations({
|
|
27
|
+
t,
|
|
28
|
+
locale: activeLocale,
|
|
29
|
+
languageInput: elements.languageInput,
|
|
30
|
+
languagePreference,
|
|
31
|
+
safetyNote: elements.safetyNote,
|
|
32
|
+
onAfterStaticTranslations,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function setStatus(key, values = {}) {
|
|
36
|
+
updateStatus({ state: activeStatus, key, values, statusText: elements.statusText, t });
|
|
37
|
+
}
|
|
38
|
+
function setCount(value) {
|
|
39
|
+
updateCount({ count: elements.count, value, t });
|
|
40
|
+
}
|
|
41
|
+
function getErrorStatusKey(error, fallbackKey) {
|
|
42
|
+
const key = error instanceof Error ? error.message : "";
|
|
43
|
+
return messages[key] ? key : fallbackKey;
|
|
44
|
+
}
|
|
45
|
+
function activateTab(tabId) {
|
|
46
|
+
activateSidePanelTab(tabId);
|
|
47
|
+
}
|
|
48
|
+
function initializeSidePanelTabs() {
|
|
49
|
+
initializeSharedSidePanelTabs({
|
|
50
|
+
tabs: sidePanelTabs,
|
|
51
|
+
onActivate: (tab) => {
|
|
52
|
+
activateTab(tab.id);
|
|
53
|
+
onTabActivated?.(tab, api);
|
|
54
|
+
},
|
|
55
|
+
onDisabled: (tab) => {
|
|
56
|
+
if (typeof onDisabledTab === "function") {
|
|
57
|
+
onDisabledTab(tab, api);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
setStatus("statusFeatureComingSoon", { feature: t(tab.labelKey) });
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async function initializeI18n() {
|
|
65
|
+
languagePreference = await readLanguagePreference();
|
|
66
|
+
activeLocale = resolveLocale({ preference: languagePreference, runtime: extensionRuntime, fallbackLocale });
|
|
67
|
+
await loadMessages(activeLocale);
|
|
68
|
+
applyTranslations();
|
|
69
|
+
setStatus(initialStatusKey);
|
|
70
|
+
}
|
|
71
|
+
async function handleLanguagePreferenceChange() {
|
|
72
|
+
languagePreference = normalizeLanguagePreference(elements.languageInput.value);
|
|
73
|
+
await saveLanguagePreference(languagePreference);
|
|
74
|
+
activeLocale = resolveLocale({ preference: languagePreference, runtime: extensionRuntime, fallbackLocale });
|
|
75
|
+
await loadMessages(activeLocale);
|
|
76
|
+
applyTranslations();
|
|
77
|
+
onLanguageChanged?.(api);
|
|
78
|
+
}
|
|
79
|
+
const api = {
|
|
80
|
+
activateTab,
|
|
81
|
+
applyTranslations,
|
|
82
|
+
getActiveLocale: () => activeLocale,
|
|
83
|
+
getCurrentStatus: () => activeStatus,
|
|
84
|
+
getErrorStatusKey,
|
|
85
|
+
getLanguagePreference: () => languagePreference,
|
|
86
|
+
getMessages: () => messages,
|
|
87
|
+
handleLanguagePreferenceChange,
|
|
88
|
+
initializeI18n,
|
|
89
|
+
initializeSidePanelTabs,
|
|
90
|
+
setCount,
|
|
91
|
+
setStatus,
|
|
92
|
+
t,
|
|
93
|
+
};
|
|
94
|
+
return api;
|
|
95
|
+
}
|
|
96
|
+
Object.assign(global, { LinkLightSidePanelBootstrap: { createSidePanelBootstrap } });
|
|
97
|
+
})(globalThis);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelDom(global) {
|
|
3
|
+
function appendText(parent, className, text) {
|
|
4
|
+
const element = global.document.createElement("div");
|
|
5
|
+
element.className = className;
|
|
6
|
+
element.textContent = text;
|
|
7
|
+
parent.append(element);
|
|
8
|
+
return element;
|
|
9
|
+
}
|
|
10
|
+
function setBusy(button, busy) {
|
|
11
|
+
button.disabled = busy;
|
|
12
|
+
}
|
|
13
|
+
Object.assign(global, { LinkLightSidePanelDom: { appendText, setBusy } });
|
|
14
|
+
})(globalThis);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelI18n(global) {
|
|
3
|
+
function normalizeChromeLocale(locale, fallbackLocale = "zh-CN") {
|
|
4
|
+
const normalized = String(locale || "").toLowerCase();
|
|
5
|
+
if (normalized.startsWith("en"))
|
|
6
|
+
return "en-US";
|
|
7
|
+
return fallbackLocale;
|
|
8
|
+
}
|
|
9
|
+
function normalizeLanguagePreference(value) {
|
|
10
|
+
if (value === "zh-CN" || value === "en-US")
|
|
11
|
+
return value;
|
|
12
|
+
return "auto";
|
|
13
|
+
}
|
|
14
|
+
function resolveLocale({ preference, runtime, fallbackLocale = "zh-CN" }) {
|
|
15
|
+
if (preference === "zh-CN" || preference === "en-US")
|
|
16
|
+
return preference;
|
|
17
|
+
return normalizeChromeLocale(runtime.getUILocale(), fallbackLocale);
|
|
18
|
+
}
|
|
19
|
+
function interpolate(template, values = {}) {
|
|
20
|
+
return String(template || "").replace(/\{(\w+)\}/g, (_match, key) => String(values[key] ?? ""));
|
|
21
|
+
}
|
|
22
|
+
async function loadLocaleMessageMap({ runtime, locale, localeDirectories, fallbackLocale = "zh-CN" }) {
|
|
23
|
+
const directory = localeDirectories[locale] ?? localeDirectories[fallbackLocale];
|
|
24
|
+
const catalog = await runtime.loadLocaleMessages(directory);
|
|
25
|
+
return Object.fromEntries(Object.entries(catalog).map(([key, value]) => [key, typeof value?.message === "string" ? value.message : ""]));
|
|
26
|
+
}
|
|
27
|
+
global.LinkLightSidePanelI18n = {
|
|
28
|
+
interpolate,
|
|
29
|
+
loadLocaleMessageMap,
|
|
30
|
+
normalizeChromeLocale,
|
|
31
|
+
normalizeLanguagePreference,
|
|
32
|
+
resolveLocale,
|
|
33
|
+
};
|
|
34
|
+
})(globalThis);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelImportPreview(global) {
|
|
3
|
+
function filterImportableDrafts(drafts, predicate) {
|
|
4
|
+
return (drafts || []).filter(predicate);
|
|
5
|
+
}
|
|
6
|
+
Object.assign(global, { LinkLightSidePanelImportPreview: { filterImportableDrafts } });
|
|
7
|
+
})(globalThis);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelStatus(global) {
|
|
3
|
+
function updateStatus({ state, key, values = {}, statusText, t }) {
|
|
4
|
+
state.key = key;
|
|
5
|
+
state.values = values;
|
|
6
|
+
statusText.textContent = t(key, values);
|
|
7
|
+
}
|
|
8
|
+
function updateCount({ count, value, t }) {
|
|
9
|
+
count.textContent = t("countLabel", { count: value });
|
|
10
|
+
}
|
|
11
|
+
global.LinkLightSidePanelStatus = { updateCount, updateStatus };
|
|
12
|
+
})(globalThis);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelTabs(global) {
|
|
3
|
+
function activateSidePanelTab(tabId, { documentRef = global.document } = {}) {
|
|
4
|
+
for (const button of documentRef.querySelectorAll("[data-tab]")) {
|
|
5
|
+
if (button.dataset.tab === tabId)
|
|
6
|
+
button.setAttribute("aria-current", "page");
|
|
7
|
+
else
|
|
8
|
+
button.removeAttribute("aria-current");
|
|
9
|
+
}
|
|
10
|
+
for (const panel of documentRef.querySelectorAll("[data-tab-panel]")) {
|
|
11
|
+
panel.hidden = panel.dataset.tabPanel !== tabId;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function initializeSidePanelTabs({ tabs, documentRef = global.document, onActivate, onDisabled, }) {
|
|
15
|
+
for (const button of documentRef.querySelectorAll("[data-tab]")) {
|
|
16
|
+
const tab = tabs.find((item) => item.id === button.dataset.tab);
|
|
17
|
+
if (!tab)
|
|
18
|
+
continue;
|
|
19
|
+
button.addEventListener("click", () => {
|
|
20
|
+
if (tab.disabled) {
|
|
21
|
+
onDisabled?.(tab);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
onActivate?.(tab);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
global.LinkLightSidePanelTabs = { activateSidePanelTab, initializeSidePanelTabs };
|
|
29
|
+
})(globalThis);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelTranslations(global) {
|
|
3
|
+
function applySidePanelTranslations({ documentRef = global.document, t, locale, titleKey = "extensionName", languageInput, languagePreference, safetyNote, onAfterStaticTranslations, }) {
|
|
4
|
+
documentRef.documentElement.lang = locale;
|
|
5
|
+
documentRef.title = t(titleKey);
|
|
6
|
+
for (const element of documentRef.querySelectorAll("[data-i18n]")) {
|
|
7
|
+
element.textContent = t(element.dataset.i18n);
|
|
8
|
+
}
|
|
9
|
+
for (const element of documentRef.querySelectorAll("[data-i18n-placeholder]")) {
|
|
10
|
+
element.setAttribute("placeholder", t(element.dataset.i18nPlaceholder));
|
|
11
|
+
}
|
|
12
|
+
for (const element of documentRef.querySelectorAll("[data-i18n-aria-label]")) {
|
|
13
|
+
element.setAttribute("aria-label", t(element.dataset.i18nAriaLabel));
|
|
14
|
+
}
|
|
15
|
+
for (const element of documentRef.querySelectorAll("[data-i18n-title]")) {
|
|
16
|
+
const values = {};
|
|
17
|
+
if (element.dataset.i18nTitleFeatureKey) {
|
|
18
|
+
values.feature = t(element.dataset.i18nTitleFeatureKey);
|
|
19
|
+
}
|
|
20
|
+
element.setAttribute("title", t(element.dataset.i18nTitle, values));
|
|
21
|
+
}
|
|
22
|
+
if (safetyNote)
|
|
23
|
+
safetyNote.textContent = t("safetyNote");
|
|
24
|
+
if (languageInput)
|
|
25
|
+
languageInput.value = languagePreference;
|
|
26
|
+
if (typeof onAfterStaticTranslations === "function")
|
|
27
|
+
onAfterStaticTranslations();
|
|
28
|
+
}
|
|
29
|
+
global.LinkLightSidePanelTranslations = { applySidePanelTranslations };
|
|
30
|
+
})(globalThis);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
interface ChromeLaunchOptions {
|
|
3
|
+
argv?: string[];
|
|
4
|
+
env?: NodeJS.ProcessEnv;
|
|
5
|
+
existsSync?: typeof existsSync;
|
|
6
|
+
platform?: NodeJS.Platform;
|
|
7
|
+
rootDir?: string;
|
|
8
|
+
extensionRoot?: string;
|
|
9
|
+
extensionName?: string;
|
|
10
|
+
extensionNames?: string[];
|
|
11
|
+
appUrl?: string;
|
|
12
|
+
profileDir?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function normalizeChromeExtensionPath(extensionPath: string, platform: NodeJS.Platform): string;
|
|
15
|
+
export declare function getChromeExtensionIdForPath(extensionPath: string, { platform }?: {
|
|
16
|
+
platform?: NodeJS.Platform | undefined;
|
|
17
|
+
}): string;
|
|
18
|
+
export declare function getChromeExecutableName(platform: NodeJS.Platform): "chrome" | "chrome.exe" | "Google Chrome for Testing";
|
|
19
|
+
export declare function findExecutableBelow(directory: string, executableName: string): string | null;
|
|
20
|
+
export declare function findLocalChromeForTesting({ rootDir, platform, exists, }?: {
|
|
21
|
+
rootDir?: string | undefined;
|
|
22
|
+
platform?: NodeJS.Platform | undefined;
|
|
23
|
+
exists?: typeof existsSync | undefined;
|
|
24
|
+
}): string | null;
|
|
25
|
+
export declare function isLikelyGoogleChromeOrChromeForTesting(chromePath: string): boolean;
|
|
26
|
+
export declare function candidateChromePaths({ browser, env, platform }: {
|
|
27
|
+
browser: 'chrome' | null;
|
|
28
|
+
env: NodeJS.ProcessEnv;
|
|
29
|
+
platform: NodeJS.Platform;
|
|
30
|
+
}): string[];
|
|
31
|
+
export declare function findChromePath({ argv, env, exists, platform, rootDir, }?: {
|
|
32
|
+
argv?: string[] | undefined;
|
|
33
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
34
|
+
exists?: typeof existsSync | undefined;
|
|
35
|
+
platform?: NodeJS.Platform | undefined;
|
|
36
|
+
rootDir?: string | undefined;
|
|
37
|
+
}): string;
|
|
38
|
+
export declare function isLikelyBrandedChrome(chromePath: string): boolean;
|
|
39
|
+
export declare function buildChromeLaunchConfig({ argv, env, existsSync: exists, platform, rootDir, extensionRoot, extensionName, extensionNames, appUrl, profileDir, }?: ChromeLaunchOptions): {
|
|
40
|
+
autoLoadSupported: false;
|
|
41
|
+
chromePath: string;
|
|
42
|
+
launchExtensionDirs: string[];
|
|
43
|
+
args: string[];
|
|
44
|
+
manualLoadPath: string;
|
|
45
|
+
resetProfile: boolean;
|
|
46
|
+
unsupportedAutoLoadReason: string;
|
|
47
|
+
} | {
|
|
48
|
+
autoLoadSupported: true;
|
|
49
|
+
chromePath: string;
|
|
50
|
+
launchExtensionDirs: string[];
|
|
51
|
+
args: string[];
|
|
52
|
+
resetProfile: boolean;
|
|
53
|
+
manualLoadPath?: undefined;
|
|
54
|
+
unsupportedAutoLoadReason?: undefined;
|
|
55
|
+
};
|
|
56
|
+
export declare function samePath(left: string, right: string): boolean;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { isMissingFile } from "./fs-error.js";
|
|
5
|
+
import { chromeExtensionsUrl, getArgValue, getChromeLaunchExtensionDirs, getProfileDirForExtensions, getProjectPaths, getRequestedAppUrl, getRequestedBrowser, getRequestedExtensions } from "./cli.js";
|
|
6
|
+
export function normalizeChromeExtensionPath(extensionPath, platform) {
|
|
7
|
+
const pathApi = platform === "win32" ? path.win32 : path.posix;
|
|
8
|
+
const resolvedPath = pathApi.resolve(extensionPath);
|
|
9
|
+
if (platform !== "win32") {
|
|
10
|
+
return resolvedPath;
|
|
11
|
+
}
|
|
12
|
+
return resolvedPath.replace(/^([a-z]):/, (_, driveLetter) => `${driveLetter.toUpperCase()}:`);
|
|
13
|
+
}
|
|
14
|
+
export function getChromeExtensionIdForPath(extensionPath, { platform = process.platform } = {}) {
|
|
15
|
+
const normalizedPath = normalizeChromeExtensionPath(extensionPath, platform);
|
|
16
|
+
const pathBytes = platform === "win32" ? Buffer.from(normalizedPath, "utf16le") : Buffer.from(normalizedPath);
|
|
17
|
+
const digest = createHash("sha256").update(pathBytes).digest("hex").slice(0, 32);
|
|
18
|
+
return digest.replace(/[0-9a-f]/g, (character) => "abcdefghijklmnop"[Number.parseInt(character, 16)]);
|
|
19
|
+
}
|
|
20
|
+
export function getChromeExecutableName(platform) {
|
|
21
|
+
if (platform === "win32")
|
|
22
|
+
return "chrome.exe";
|
|
23
|
+
if (platform === "darwin")
|
|
24
|
+
return "Google Chrome for Testing";
|
|
25
|
+
return "chrome";
|
|
26
|
+
}
|
|
27
|
+
export function findExecutableBelow(directory, executableName) {
|
|
28
|
+
const entries = readdirSync(directory, { withFileTypes: true });
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
const entryPath = path.join(directory, entry.name);
|
|
31
|
+
if (entry.isFile() && entry.name === executableName) {
|
|
32
|
+
return entryPath;
|
|
33
|
+
}
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
const foundPath = findExecutableBelow(entryPath, executableName);
|
|
36
|
+
if (foundPath)
|
|
37
|
+
return foundPath;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
export function findLocalChromeForTesting({ rootDir = process.cwd(), platform = process.platform, exists = existsSync, } = {}) {
|
|
43
|
+
const { chromeForTestingDir } = getProjectPaths(rootDir);
|
|
44
|
+
if (!exists(chromeForTestingDir)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
if (!statSync(chromeForTestingDir).isDirectory()) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return findExecutableBelow(chromeForTestingDir, getChromeExecutableName(platform));
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (isMissingFile(error)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function isLikelyGoogleChromeOrChromeForTesting(chromePath) {
|
|
61
|
+
const normalizedPath = chromePath.toLowerCase().replaceAll("\\", "/");
|
|
62
|
+
return (normalizedPath.includes("chrome for testing") ||
|
|
63
|
+
normalizedPath.includes("chrome-for-testing") ||
|
|
64
|
+
normalizedPath.includes("chrome-win64") ||
|
|
65
|
+
normalizedPath.includes("/google/chrome/application/chrome.exe") ||
|
|
66
|
+
normalizedPath.endsWith("/google chrome"));
|
|
67
|
+
}
|
|
68
|
+
export function candidateChromePaths({ browser, env, platform }) {
|
|
69
|
+
if (platform === "win32") {
|
|
70
|
+
if (browser === "chrome") {
|
|
71
|
+
return [
|
|
72
|
+
env.CHROME_FOR_TESTING_PATH,
|
|
73
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Google", "Chrome", "Application", "chrome.exe"),
|
|
74
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Google", "Chrome", "Application", "chrome.exe"),
|
|
75
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Google", "Chrome", "Application", "chrome.exe"),
|
|
76
|
+
String.raw `C:\Program Files\Google\Chrome\Application\chrome.exe`,
|
|
77
|
+
String.raw `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
|
|
78
|
+
].filter((candidate) => Boolean(candidate));
|
|
79
|
+
}
|
|
80
|
+
return [
|
|
81
|
+
env.CHROME_FOR_TESTING_PATH,
|
|
82
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Chromium", "Application", "chrome.exe"),
|
|
83
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Chromium", "Application", "chrome.exe"),
|
|
84
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Chromium", "Application", "chrome.exe"),
|
|
85
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
86
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
87
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
88
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Google", "Chrome", "Application", "chrome.exe"),
|
|
89
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Google", "Chrome", "Application", "chrome.exe"),
|
|
90
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Google", "Chrome", "Application", "chrome.exe"),
|
|
91
|
+
String.raw `C:\Program Files\Microsoft\Edge\Application\msedge.exe`,
|
|
92
|
+
String.raw `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe`,
|
|
93
|
+
String.raw `C:\Program Files\Google\Chrome\Application\chrome.exe`,
|
|
94
|
+
String.raw `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
|
|
95
|
+
].filter((candidate) => Boolean(candidate));
|
|
96
|
+
}
|
|
97
|
+
if (platform === "darwin") {
|
|
98
|
+
return ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"];
|
|
99
|
+
}
|
|
100
|
+
if (browser === "chrome") {
|
|
101
|
+
return ["/usr/bin/google-chrome", "/usr/bin/google-chrome-stable"];
|
|
102
|
+
}
|
|
103
|
+
return ["/usr/bin/chromium", "/snap/bin/chromium", "/usr/bin/microsoft-edge", "/usr/bin/google-chrome", "/usr/bin/google-chrome-stable"];
|
|
104
|
+
}
|
|
105
|
+
export function findChromePath({ argv = process.argv.slice(2), env = process.env, exists = existsSync, platform = process.platform, rootDir = process.cwd(), } = {}) {
|
|
106
|
+
const requestedBrowser = getRequestedBrowser(argv);
|
|
107
|
+
const explicitChromePath = getArgValue(argv, "--chrome-path") || env.CHROME_FOR_TESTING_PATH;
|
|
108
|
+
if (explicitChromePath?.trim()) {
|
|
109
|
+
return explicitChromePath.trim();
|
|
110
|
+
}
|
|
111
|
+
const localChromeForTestingPath = findLocalChromeForTesting({ rootDir, platform, exists });
|
|
112
|
+
if (localChromeForTestingPath) {
|
|
113
|
+
return localChromeForTestingPath;
|
|
114
|
+
}
|
|
115
|
+
if (env.CHROME_PATH?.trim() &&
|
|
116
|
+
(!requestedBrowser || isLikelyGoogleChromeOrChromeForTesting(env.CHROME_PATH.trim()))) {
|
|
117
|
+
return env.CHROME_PATH.trim();
|
|
118
|
+
}
|
|
119
|
+
const foundPath = candidateChromePaths({ browser: requestedBrowser, env, platform }).find((candidate) => exists(candidate));
|
|
120
|
+
if (foundPath) {
|
|
121
|
+
return foundPath;
|
|
122
|
+
}
|
|
123
|
+
throw new Error("Chrome executable was not found. Set CHROME_PATH or pass --chrome-path=<path-to-chrome>.");
|
|
124
|
+
}
|
|
125
|
+
export function isLikelyBrandedChrome(chromePath) {
|
|
126
|
+
const normalizedPath = chromePath.toLowerCase().replaceAll("\\", "/");
|
|
127
|
+
if (normalizedPath.includes("chrome for testing") || normalizedPath.includes("chrome-for-testing")) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (normalizedPath.includes("chrome-win64") || normalizedPath.includes("chromium") || normalizedPath.includes("/microsoft/edge/")) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return normalizedPath.includes("/google/chrome/application/chrome.exe") || normalizedPath.endsWith("/google chrome");
|
|
134
|
+
}
|
|
135
|
+
export function buildChromeLaunchConfig({ argv = process.argv.slice(2), env = process.env, existsSync: exists = existsSync, platform = process.platform, rootDir = process.cwd(), extensionRoot = getArgValue(argv, "--extension-root"), extensionName, extensionNames = extensionName ? [extensionName] : getRequestedExtensions(argv, rootDir), appUrl = getRequestedAppUrl(argv, env), profileDir = getProfileDirForExtensions(rootDir, extensionNames, extensionRoot), } = {}) {
|
|
136
|
+
const launchExtensionDirs = getChromeLaunchExtensionDirs({ extensionNames, rootDir, extensionRoot });
|
|
137
|
+
const launchExtensionArg = launchExtensionDirs.join(",");
|
|
138
|
+
const url = getArgValue(argv, "--url") || appUrl;
|
|
139
|
+
const chromePath = findChromePath({ argv, env, exists, platform, rootDir });
|
|
140
|
+
const autoLoadSupported = argv.includes("--allow-branded-chrome") || !isLikelyBrandedChrome(chromePath);
|
|
141
|
+
const baseArgs = [
|
|
142
|
+
`--user-data-dir=${profileDir}`,
|
|
143
|
+
"--new-window",
|
|
144
|
+
"--no-first-run",
|
|
145
|
+
"--no-default-browser-check",
|
|
146
|
+
];
|
|
147
|
+
if (!autoLoadSupported) {
|
|
148
|
+
return {
|
|
149
|
+
autoLoadSupported,
|
|
150
|
+
chromePath,
|
|
151
|
+
launchExtensionDirs,
|
|
152
|
+
args: [...baseArgs, chromeExtensionsUrl, url],
|
|
153
|
+
manualLoadPath: launchExtensionDirs.join(", "),
|
|
154
|
+
resetProfile: argv.includes("--reset-profile"),
|
|
155
|
+
unsupportedAutoLoadReason: "Automatic unpacked extension loading is disabled in current branded Chrome builds. Use Chrome for Testing for one-command loading, or load the unpacked extension manually.",
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
autoLoadSupported,
|
|
160
|
+
chromePath,
|
|
161
|
+
launchExtensionDirs,
|
|
162
|
+
args: [
|
|
163
|
+
...baseArgs,
|
|
164
|
+
`--load-extension=${launchExtensionArg}`,
|
|
165
|
+
`--disable-extensions-except=${launchExtensionArg}`,
|
|
166
|
+
chromeExtensionsUrl,
|
|
167
|
+
url,
|
|
168
|
+
],
|
|
169
|
+
resetProfile: argv.includes("--reset-profile"),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
export function samePath(left, right) {
|
|
173
|
+
const resolvedLeft = path.resolve(left);
|
|
174
|
+
const resolvedRight = path.resolve(right);
|
|
175
|
+
return process.platform === "win32"
|
|
176
|
+
? resolvedLeft.toLowerCase() === resolvedRight.toLowerCase()
|
|
177
|
+
: resolvedLeft === resolvedRight;
|
|
178
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { StdioOptions } from "node:child_process";
|
|
2
|
+
import type { CommandRunner } from "@openge/forge-command-runner";
|
|
3
|
+
interface InstallResult {
|
|
4
|
+
error?: Error;
|
|
5
|
+
stdout?: string | Uint8Array | null;
|
|
6
|
+
stderr?: string | Uint8Array | null;
|
|
7
|
+
status?: number | null;
|
|
8
|
+
}
|
|
9
|
+
interface InstallOptions {
|
|
10
|
+
env?: NodeJS.ProcessEnv;
|
|
11
|
+
rootDir?: string;
|
|
12
|
+
platform?: NodeJS.Platform;
|
|
13
|
+
maxAttempts?: number;
|
|
14
|
+
logger?: (message: string) => void;
|
|
15
|
+
stdio?: StdioOptions;
|
|
16
|
+
installRunner?: (command: string, args: string[], options: {
|
|
17
|
+
stdio: StdioOptions;
|
|
18
|
+
shell: boolean;
|
|
19
|
+
}) => InstallResult;
|
|
20
|
+
}
|
|
21
|
+
interface DevInstallOptions extends InstallOptions {
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
commandRunner?: Pick<ReturnType<typeof createInstallerCommandRunner>, "run">;
|
|
24
|
+
}
|
|
25
|
+
declare function createInstallerCommandRunner(platform: NodeJS.Platform): CommandRunner;
|
|
26
|
+
export declare function trimOutput(value: string, maxLength?: number): string;
|
|
27
|
+
export declare function stringifyProcessOutput(value: unknown): string;
|
|
28
|
+
export declare function getProcessOutputErrorReason(result: InstallResult): string;
|
|
29
|
+
export declare function formatChromeForTestingInstallFailure(result: InstallResult): string;
|
|
30
|
+
export declare function getPackageRunner({ env, platform }: {
|
|
31
|
+
env: NodeJS.ProcessEnv;
|
|
32
|
+
platform: NodeJS.Platform;
|
|
33
|
+
}): {
|
|
34
|
+
command: string;
|
|
35
|
+
argsPrefix: string[];
|
|
36
|
+
};
|
|
37
|
+
export declare function getChromeForTestingInstallStep({ env, platform, rootDir, }?: {
|
|
38
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
39
|
+
platform?: NodeJS.Platform | undefined;
|
|
40
|
+
rootDir?: string | undefined;
|
|
41
|
+
}): {
|
|
42
|
+
command: string;
|
|
43
|
+
args: string[];
|
|
44
|
+
};
|
|
45
|
+
export declare function getChromeForTestingFallbackBuildId(env?: NodeJS.ProcessEnv): string;
|
|
46
|
+
export declare function getChromeForTestingDirectInstallStep({ env, platform, rootDir, }?: {
|
|
47
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
48
|
+
platform?: NodeJS.Platform | undefined;
|
|
49
|
+
rootDir?: string | undefined;
|
|
50
|
+
}): {
|
|
51
|
+
command: string;
|
|
52
|
+
args: string[];
|
|
53
|
+
route: string;
|
|
54
|
+
} | null;
|
|
55
|
+
export declare function getChromeForTestingInstallSteps({ env, platform, rootDir, }?: {
|
|
56
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
57
|
+
platform?: NodeJS.Platform | undefined;
|
|
58
|
+
rootDir?: string | undefined;
|
|
59
|
+
}): {
|
|
60
|
+
command: string;
|
|
61
|
+
args: string[];
|
|
62
|
+
route: string;
|
|
63
|
+
}[];
|
|
64
|
+
export declare function installChromeForTesting({ env, installRunner, logger, maxAttempts, platform, rootDir, stdio, }?: InstallOptions): void;
|
|
65
|
+
export declare function installChromeForTestingForDev({ env, logger, maxAttempts, platform, rootDir, signal, commandRunner, ...options }?: DevInstallOptions): Promise<void>;
|
|
66
|
+
export {};
|