@arraypress/extension-kit 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +214 -2
- package/bin/extension-kit.js +35 -0
- package/package.json +67 -4
- package/src/about/links.d.ts +51 -0
- package/src/about/links.js +48 -0
- package/src/build/duplication.d.ts +116 -0
- package/src/build/duplication.js +119 -0
- package/src/build/index.d.ts +11 -0
- package/src/build/index.js +12 -0
- package/src/build/release-checks.d.ts +36 -0
- package/src/build/release-checks.js +61 -0
- package/src/build/toolbar-icons.d.ts +96 -0
- package/src/build/toolbar-icons.js +101 -0
- package/src/build/vendor.d.ts +26 -0
- package/src/build/vendor.js +43 -0
- package/src/build/zip.d.ts +31 -0
- package/src/build/zip.js +47 -0
- package/src/commands/shortcuts.d.ts +39 -0
- package/src/commands/shortcuts.js +31 -0
- package/src/dom/i18n.d.ts +39 -0
- package/src/dom/i18n.js +49 -0
- package/src/dom/page.d.ts +59 -0
- package/src/dom/page.js +93 -0
- package/src/index.d.ts +21 -0
- package/src/index.js +24 -0
- package/src/settings/saver.d.ts +76 -0
- package/src/settings/saver.js +98 -0
- package/src/settings/storage.d.ts +91 -0
- package/src/settings/storage.js +76 -0
- package/src/settings/store.d.ts +138 -0
- package/src/settings/store.js +173 -0
- package/src/styles/chrome-ui.css +1094 -0
- package/src/testing/e2e.d.ts +165 -0
- package/src/testing/e2e.js +272 -0
- package/src/testing/fake-storage.d.ts +71 -0
- package/src/testing/fake-storage.js +145 -0
- package/src/testing/index.d.ts +9 -0
- package/src/testing/index.js +10 -0
- package/src/testing/package.d.ts +39 -0
- package/src/testing/package.js +96 -0
- package/src/toolbar/offscreen.d.ts +30 -0
- package/src/toolbar/offscreen.js +37 -0
- package/src/toolbar/scheme.d.ts +81 -0
- package/src/toolbar/scheme.js +87 -0
- package/src/toolbar/theme.d.ts +187 -0
- package/src/toolbar/theme.js +176 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker side of the light/dark toolbar glyph, as Chrome's own toolbar
|
|
3
|
+
* icons do. The service worker can't see the scheme (no `matchMedia`), so
|
|
4
|
+
* documents report it (toolbar/scheme.js); this stores the last report per
|
|
5
|
+
* computer and shows the matching glyph. `setIcon` changes only the image: a
|
|
6
|
+
* badge still draws over it.
|
|
7
|
+
*
|
|
8
|
+
* An extension with nothing of its own on every page can keep an offscreen
|
|
9
|
+
* document open to hear a theme change (`watchFromOffscreen`,
|
|
10
|
+
* toolbar/offscreen.js).
|
|
11
|
+
*
|
|
12
|
+
* @module toolbar/theme
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { TOOLBAR_DARK_KEY, TOOLBAR_SCHEME } from './scheme.js';
|
|
16
|
+
|
|
17
|
+
/** Where the offscreen scheme watcher lives, from the extension's root. */
|
|
18
|
+
export const OFFSCREEN_PATH = 'offscreen/offscreen.html';
|
|
19
|
+
|
|
20
|
+
/** Where the rendered glyphs live (build/toolbar-icons.js writes them). */
|
|
21
|
+
export const TOOLBAR_ICON_DIR = '/icons/toolbar';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {{16: string, 32: string, 48: string}} ToolbarIconPaths
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The worker APIs the toolbar theme uses: pieces of `chrome`, or fakes.
|
|
29
|
+
*
|
|
30
|
+
* @typedef {object} ToolbarThemeApi
|
|
31
|
+
* @property {{get(keys: string): Promise<Record<string, any>>, set(items: Record<string, unknown>): Promise<void>}} storage
|
|
32
|
+
* `chrome.storage.local`.
|
|
33
|
+
* @property {{setIcon(details: {path: ToolbarIconPaths}): Promise<void>}} action `chrome.action`.
|
|
34
|
+
* @property {string} extensionId `chrome.runtime.id`: reports from anyone else are ignored.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {object} ToolbarTheme
|
|
39
|
+
* @property {() => Promise<void>} restore Show the dark glyph again if that's what was
|
|
40
|
+
* seen last: after a restart or re-enable Chrome shows the manifest's light one.
|
|
41
|
+
* @property {(message: unknown, sender?: {id?: string}) => boolean} onMessage Handle a
|
|
42
|
+
* scheme report; returns whether it was one (handled or ignored), so a worker's
|
|
43
|
+
* message listener can stop there.
|
|
44
|
+
* @property {() => Promise<void>} settled Resolves once every report so far is handled.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The glyph for a scheme; the manifest's `default_icon` is the light one.
|
|
49
|
+
* Paths start with "/": Chrome resolves `setIcon` paths against the worker's
|
|
50
|
+
* own address, where a relative one fails to load.
|
|
51
|
+
*
|
|
52
|
+
* @param {boolean} dark - The dark scheme's glyph, else the light one's.
|
|
53
|
+
* @returns {ToolbarIconPaths} Paths by size, for `chrome.action.setIcon`.
|
|
54
|
+
* @example
|
|
55
|
+
* toolbarIcon(true); // { 16: '/icons/toolbar/dark-16.png', 32: …, 48: … }
|
|
56
|
+
*/
|
|
57
|
+
export function toolbarIcon(dark) {
|
|
58
|
+
const scheme = dark ? 'dark' : 'light';
|
|
59
|
+
return {
|
|
60
|
+
16: `${TOOLBAR_ICON_DIR}/${scheme}-16.png`,
|
|
61
|
+
32: `${TOOLBAR_ICON_DIR}/${scheme}-32.png`,
|
|
62
|
+
48: `${TOOLBAR_ICON_DIR}/${scheme}-48.png`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The worker's toolbar theme: one report at a time, so two quick ones can't
|
|
68
|
+
* store one scheme and show the other.
|
|
69
|
+
*
|
|
70
|
+
* @param {ToolbarThemeApi} api - Storage, the action and this extension's id.
|
|
71
|
+
* @returns {ToolbarTheme} The theme's handlers.
|
|
72
|
+
*/
|
|
73
|
+
export function createToolbarTheme({ storage, action, extensionId }) {
|
|
74
|
+
/** @type {Promise<void>} */
|
|
75
|
+
let queue = Promise.resolve();
|
|
76
|
+
/** @param {boolean} dark */
|
|
77
|
+
const show = (dark) => action.setIcon({ path: toolbarIcon(dark) });
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
restore() {
|
|
81
|
+
queue = queue.then(async () => {
|
|
82
|
+
const { [TOOLBAR_DARK_KEY]: dark } = await storage.get(TOOLBAR_DARK_KEY);
|
|
83
|
+
if (dark === true) await show(true);
|
|
84
|
+
}).catch((error) => console.error('Restoring the toolbar icon:', error));
|
|
85
|
+
return queue;
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
onMessage(message, sender) {
|
|
89
|
+
const report = /** @type {{type?: unknown, dark?: unknown} | null | undefined} */ (message);
|
|
90
|
+
if (report?.type !== TOOLBAR_SCHEME) return false;
|
|
91
|
+
const { dark } = report;
|
|
92
|
+
if (sender?.id !== extensionId || typeof dark !== 'boolean') return true;
|
|
93
|
+
queue = queue.then(async () => {
|
|
94
|
+
const { [TOOLBAR_DARK_KEY]: was = false } = await storage.get(TOOLBAR_DARK_KEY);
|
|
95
|
+
if (was === dark) return;
|
|
96
|
+
// Shown first: if Chrome refuses the icon, nothing is stored, and the next report tries again.
|
|
97
|
+
await show(dark);
|
|
98
|
+
await storage.set({ [TOOLBAR_DARK_KEY]: dark });
|
|
99
|
+
}).catch((error) => console.error('Changing the toolbar icon:', error));
|
|
100
|
+
return true;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
settled: () => queue,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* What opening the offscreen watcher needs: pieces of `chrome`, or fakes.
|
|
109
|
+
*
|
|
110
|
+
* @typedef {object} OffscreenApi
|
|
111
|
+
* @property {{createDocument(details: {url: string, reasons: string[], justification: string}): Promise<void>, Reason: {MATCH_MEDIA: string}}} offscreen
|
|
112
|
+
* `chrome.offscreen`.
|
|
113
|
+
* @property {{getContexts(filter: {contextTypes: 'OFFSCREEN_DOCUMENT'[]}): Promise<unknown[]>}} runtime `chrome.runtime`.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
/** @type {Promise<boolean> | null} */
|
|
117
|
+
let opening = null;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Open the offscreen document that watches the scheme (reason MATCH_MEDIA),
|
|
121
|
+
* unless it's open. It stays open: it's one small page in the extension's own
|
|
122
|
+
* process, and the only way to hear a theme change while no page of ours is
|
|
123
|
+
* open. Chrome closes it when the browser quits, so call this each time the
|
|
124
|
+
* worker starts. Concurrent calls share one attempt.
|
|
125
|
+
*
|
|
126
|
+
* @param {OffscreenApi} api - `chrome.offscreen` and `chrome.runtime`.
|
|
127
|
+
* @param {string} [url] - The document, from the extension's root. Default {@link OFFSCREEN_PATH}.
|
|
128
|
+
* @returns {Promise<boolean>} Whether it opened one (false: already open).
|
|
129
|
+
*/
|
|
130
|
+
export function watchFromOffscreen({ offscreen, runtime }, url = OFFSCREEN_PATH) {
|
|
131
|
+
opening ??= (async () => {
|
|
132
|
+
const open = await runtime.getContexts({ contextTypes: ['OFFSCREEN_DOCUMENT'] });
|
|
133
|
+
if (open.length) return false;
|
|
134
|
+
await offscreen.createDocument({
|
|
135
|
+
url,
|
|
136
|
+
reasons: [offscreen.Reason.MATCH_MEDIA],
|
|
137
|
+
justification: 'Reads whether Chrome is in light or dark mode, to show the matching toolbar icon.',
|
|
138
|
+
});
|
|
139
|
+
return true;
|
|
140
|
+
})().finally(() => { opening = null; });
|
|
141
|
+
return opening;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Everything `startToolbarTheme` wires up: `chrome` itself will do.
|
|
146
|
+
*
|
|
147
|
+
* @typedef {object} ToolbarWorkerApi
|
|
148
|
+
* @property {{local: ToolbarThemeApi['storage']}} storage `chrome.storage`.
|
|
149
|
+
* @property {ToolbarThemeApi['action']} action `chrome.action`.
|
|
150
|
+
* @property {OffscreenApi['offscreen']} offscreen `chrome.offscreen`.
|
|
151
|
+
* @property {OffscreenApi['runtime'] & {id: string, onMessage: {addListener(listener: (message: unknown, sender: {id?: string}) => boolean): void}}} runtime
|
|
152
|
+
* `chrome.runtime`.
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The whole worker side for an extension that watches from an offscreen
|
|
157
|
+
* document: restore the last glyph, open the watcher, and handle its reports.
|
|
158
|
+
* Call it at the worker's top level, so the listener attaches every time the
|
|
159
|
+
* worker wakes.
|
|
160
|
+
*
|
|
161
|
+
* @param {ToolbarWorkerApi} api - `chrome`, or a fake.
|
|
162
|
+
* @returns {ToolbarTheme} The theme, for tests.
|
|
163
|
+
* @example
|
|
164
|
+
* // background.js
|
|
165
|
+
* startToolbarTheme(chrome);
|
|
166
|
+
*/
|
|
167
|
+
export function startToolbarTheme(api) {
|
|
168
|
+
const theme = createToolbarTheme({ storage: api.storage.local, action: api.action, extensionId: api.runtime.id });
|
|
169
|
+
theme.restore();
|
|
170
|
+
watchFromOffscreen(api).catch((error) => console.error('Opening the colour scheme watcher:', error));
|
|
171
|
+
api.runtime.onMessage.addListener((message, sender) => {
|
|
172
|
+
theme.onMessage(message, sender);
|
|
173
|
+
return false;
|
|
174
|
+
});
|
|
175
|
+
return theme;
|
|
176
|
+
}
|