@extension-use/host 0.0.0-stage → 0.1.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 +6 -2
- package/dist/background/index.d.ts +317 -0
- package/dist/background/index.js +1019 -0
- package/dist/chunk-4SALFB5W.js +61 -0
- package/dist/chunk-5X4QMMKA.js +16 -0
- package/dist/chunk-QYMPEWZP.js +843 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +192 -0
- package/dist/content/index.d.ts +62 -0
- package/dist/content/index.js +1523 -0
- package/dist/frame/index.d.ts +33 -0
- package/dist/frame/index.js +196 -0
- package/dist/groups-DC3obZGP.d.ts +90 -0
- package/dist/offscreen/index.d.ts +3 -0
- package/dist/offscreen/index.js +107 -0
- package/dist/permissions.d.ts +60 -0
- package/dist/permissions.js +14 -0
- package/dist/standalone/extension-use-background.js +70 -0
- package/dist/standalone/extension-use-content.js +24 -0
- package/dist/standalone/extension-use-frame.js +1 -0
- package/dist/standalone/extension-use-offscreen.js +1 -0
- package/dist/standalone/extension-use-ui.js +28 -0
- package/dist/ui/index.d.ts +11 -0
- package/dist/ui/index.js +281 -0
- package/package.json +73 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type BridgeHandler = (input: unknown, ctx: {
|
|
2
|
+
userActive: boolean;
|
|
3
|
+
}) => Promise<unknown>;
|
|
4
|
+
/** Built-in bridge actions. Inputs are checked by hand to keep zod out of extension pages. */
|
|
5
|
+
declare const BRIDGE_ACTIONS: Record<string, BridgeHandler>;
|
|
6
|
+
interface FrameBridgeOptions {
|
|
7
|
+
/** The web app to load, e.g. "https://app.example.com/ext". */
|
|
8
|
+
url: string;
|
|
9
|
+
/**
|
|
10
|
+
* Origins allowed to send commands. Default: the origin of `url`.
|
|
11
|
+
* The background checks this again against `controllers.frame.origins`.
|
|
12
|
+
*/
|
|
13
|
+
allowedOrigins?: string[];
|
|
14
|
+
/** Where to mount the iframe. Default: document.body. */
|
|
15
|
+
container?: HTMLElement;
|
|
16
|
+
/** Extra iframe attributes, e.g. { allow: "clipboard-write" }. */
|
|
17
|
+
attributes?: Record<string, string>;
|
|
18
|
+
/** Shown if the app has not loaded after `fallbackAfterMs` (default 8000). */
|
|
19
|
+
fallback?: HTMLElement | string;
|
|
20
|
+
fallbackAfterMs?: number;
|
|
21
|
+
/** Run gesture-gated actions (permissions.*) in this page. Default true. */
|
|
22
|
+
bridgeActions?: boolean;
|
|
23
|
+
debug?: boolean;
|
|
24
|
+
}
|
|
25
|
+
interface FrameBridge {
|
|
26
|
+
iframe: HTMLIFrameElement;
|
|
27
|
+
/** Reload the app (and start a new session). */
|
|
28
|
+
reload(): void;
|
|
29
|
+
destroy(): void;
|
|
30
|
+
}
|
|
31
|
+
declare function mountFrameBridge(options: FrameBridgeOptions): FrameBridge;
|
|
32
|
+
|
|
33
|
+
export { BRIDGE_ACTIONS, type FrameBridge, type FrameBridgeOptions, mountFrameBridge };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FRAME_PORT
|
|
3
|
+
} from "../chunk-5X4QMMKA.js";
|
|
4
|
+
|
|
5
|
+
// src/frame/index.ts
|
|
6
|
+
import { ExtensionUseError, envelope, isEnvelope, matchAny } from "@extension-use/protocol";
|
|
7
|
+
function permissionSet(input) {
|
|
8
|
+
const i = input ?? {};
|
|
9
|
+
const strings = (x) => Array.isArray(x) && x.every((s) => typeof s === "string") ? x : void 0;
|
|
10
|
+
const permissions = strings(i.permissions);
|
|
11
|
+
const origins = strings(i.origins);
|
|
12
|
+
if (i.permissions !== void 0 && !permissions || i.origins !== void 0 && !origins) {
|
|
13
|
+
throw new ExtensionUseError("INVALID_INPUT", "permissions and origins must be arrays of strings");
|
|
14
|
+
}
|
|
15
|
+
if (!permissions?.length && !origins?.length) throw new ExtensionUseError("INVALID_INPUT", "Give permissions or origins");
|
|
16
|
+
return { ...permissions ? { permissions } : {}, ...origins ? { origins } : {} };
|
|
17
|
+
}
|
|
18
|
+
var BRIDGE_ACTIONS = {
|
|
19
|
+
"permissions.contains": async (input) => ({ granted: await chrome.permissions.contains(permissionSet(input)) }),
|
|
20
|
+
"permissions.getAll": async () => {
|
|
21
|
+
const all = await chrome.permissions.getAll();
|
|
22
|
+
return { permissions: all.permissions ?? [], origins: all.origins ?? [] };
|
|
23
|
+
},
|
|
24
|
+
"permissions.request": async (input, { userActive }) => {
|
|
25
|
+
const set = permissionSet(input);
|
|
26
|
+
if (!userActive) {
|
|
27
|
+
throw new ExtensionUseError("NEEDS_USER_GESTURE", "Call permissions.request right after the person clicks in your app");
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
return { granted: await chrome.permissions.request(set) };
|
|
31
|
+
} catch (err) {
|
|
32
|
+
const message = String(err?.message ?? err);
|
|
33
|
+
if (/user gesture/i.test(message)) throw new ExtensionUseError("NEEDS_USER_GESTURE", message);
|
|
34
|
+
throw new ExtensionUseError("NEEDS_PERMISSION", message);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
function mountFrameBridge(options) {
|
|
39
|
+
const appOrigin = new URL(options.url).origin;
|
|
40
|
+
const allowed = options.allowedOrigins ?? [appOrigin];
|
|
41
|
+
const log = (...a) => options.debug && console.debug("[extension-use/frame]", ...a);
|
|
42
|
+
const iframe = document.createElement("iframe");
|
|
43
|
+
iframe.style.cssText = "border:0;width:100%;height:100%;display:block;";
|
|
44
|
+
for (const [k, v] of Object.entries(options.attributes ?? {})) iframe.setAttribute(k, v);
|
|
45
|
+
(options.container ?? document.body).append(iframe);
|
|
46
|
+
let port;
|
|
47
|
+
let frameOrigin;
|
|
48
|
+
let loaded = false;
|
|
49
|
+
let windowId;
|
|
50
|
+
const windowReady = chrome.windows?.getCurrent().then((w) => {
|
|
51
|
+
windowId = w.id;
|
|
52
|
+
}).catch(() => {
|
|
53
|
+
});
|
|
54
|
+
const bridgeActions = options.bridgeActions === false ? {} : BRIDGE_ACTIONS;
|
|
55
|
+
let granted = [];
|
|
56
|
+
let clientHello;
|
|
57
|
+
const subscriptions = /* @__PURE__ */ new Set();
|
|
58
|
+
const inflight = /* @__PURE__ */ new Set();
|
|
59
|
+
const toFrame = (env, origin) => {
|
|
60
|
+
log("\u2192 frame", env);
|
|
61
|
+
iframe.contentWindow?.postMessage(env, origin);
|
|
62
|
+
};
|
|
63
|
+
function openPort(origin, resume) {
|
|
64
|
+
port?.disconnect();
|
|
65
|
+
let p;
|
|
66
|
+
try {
|
|
67
|
+
p = chrome.runtime.connect({ name: FRAME_PORT });
|
|
68
|
+
} catch (err) {
|
|
69
|
+
log("could not connect", err);
|
|
70
|
+
port = void 0;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const hello = { type: "bridge-hello", origin, ...windowId !== void 0 ? { windowId } : {} };
|
|
74
|
+
p.postMessage(hello);
|
|
75
|
+
let welcomed = false;
|
|
76
|
+
let swallowWelcome = resume;
|
|
77
|
+
p.onMessage.addListener((env) => {
|
|
78
|
+
if (env?.kind === "welcome") {
|
|
79
|
+
welcomed = true;
|
|
80
|
+
granted = env.granted;
|
|
81
|
+
if (swallowWelcome) {
|
|
82
|
+
swallowWelcome = false;
|
|
83
|
+
log("resumed session", env.session);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const extra = Object.keys(bridgeActions).filter((a) => matchAny(granted, a)).map((action) => ({ action, version: 1, where: "bridge" }));
|
|
87
|
+
env = { ...env, capabilities: [...env.capabilities, ...extra] };
|
|
88
|
+
} else if (env?.kind === "result") {
|
|
89
|
+
inflight.delete(env.id);
|
|
90
|
+
} else if (env?.kind === "bye") {
|
|
91
|
+
clientHello = void 0;
|
|
92
|
+
subscriptions.clear();
|
|
93
|
+
}
|
|
94
|
+
toFrame(env, origin);
|
|
95
|
+
});
|
|
96
|
+
p.onDisconnect.addListener(() => {
|
|
97
|
+
void chrome.runtime.lastError;
|
|
98
|
+
if (port !== p) return;
|
|
99
|
+
port = void 0;
|
|
100
|
+
log("port closed");
|
|
101
|
+
for (const id of inflight) {
|
|
102
|
+
toFrame(
|
|
103
|
+
envelope({ kind: "result", id, ok: false, error: { code: "NOT_CONNECTED", message: "The extension's background restarted during this call. Try again." } }),
|
|
104
|
+
origin
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
inflight.clear();
|
|
108
|
+
if (welcomed && clientHello && subscriptions.size) openPort(origin, true);
|
|
109
|
+
});
|
|
110
|
+
port = p;
|
|
111
|
+
if (resume && clientHello) {
|
|
112
|
+
p.postMessage(clientHello);
|
|
113
|
+
if (subscriptions.size) p.postMessage(envelope({ kind: "subscribe", events: [...subscriptions] }));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let queue = Promise.resolve();
|
|
117
|
+
const onMessage = (event) => {
|
|
118
|
+
if (event.source !== iframe.contentWindow) return;
|
|
119
|
+
if (!matchAny(allowed, event.origin)) {
|
|
120
|
+
log("rejected message from", event.origin);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (!isEnvelope(event.data)) return;
|
|
124
|
+
const data = event.data;
|
|
125
|
+
const origin = event.origin;
|
|
126
|
+
const userActive = navigator.userActivation?.isActive ?? false;
|
|
127
|
+
loaded = true;
|
|
128
|
+
queue = queue.then(async () => {
|
|
129
|
+
log("\u2190 frame", data);
|
|
130
|
+
await windowReady;
|
|
131
|
+
if (data.kind === "hello" || frameOrigin !== origin) {
|
|
132
|
+
frameOrigin = origin;
|
|
133
|
+
clientHello = data.kind === "hello" ? data : void 0;
|
|
134
|
+
subscriptions.clear();
|
|
135
|
+
inflight.clear();
|
|
136
|
+
openPort(origin, false);
|
|
137
|
+
} else if (!port) {
|
|
138
|
+
openPort(origin, true);
|
|
139
|
+
}
|
|
140
|
+
if (data.kind === "call" && Object.hasOwn(bridgeActions, data.action)) {
|
|
141
|
+
void runBridgeAction(data, userActive).then((result) => toFrame(result, origin));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (data.kind === "subscribe") for (const e of data.events) subscriptions.add(e);
|
|
145
|
+
else if (data.kind === "unsubscribe") for (const e of data.events) subscriptions.delete(e);
|
|
146
|
+
if (!port) {
|
|
147
|
+
if (data.kind === "call") {
|
|
148
|
+
toFrame(envelope({ kind: "result", id: data.id, ok: false, error: { code: "NOT_CONNECTED", message: "The extension was reloaded. Reopen this panel." } }), origin);
|
|
149
|
+
}
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (data.kind === "call") inflight.add(data.id);
|
|
153
|
+
port.postMessage(data);
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
window.addEventListener("message", onMessage);
|
|
157
|
+
async function runBridgeAction(call, userActive) {
|
|
158
|
+
try {
|
|
159
|
+
if (!matchAny(granted, call.action)) throw new ExtensionUseError("NOT_GRANTED", `This controller may not call "${call.action}"`);
|
|
160
|
+
const output = await bridgeActions[call.action](call.input ?? {}, { userActive });
|
|
161
|
+
return envelope({ kind: "result", id: call.id, ok: true, output });
|
|
162
|
+
} catch (err) {
|
|
163
|
+
return envelope({ kind: "result", id: call.id, ok: false, error: ExtensionUseError.from(err).toPayload() });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
iframe.src = options.url;
|
|
167
|
+
let fallbackTimer;
|
|
168
|
+
if (options.fallback) {
|
|
169
|
+
fallbackTimer = setTimeout(() => {
|
|
170
|
+
if (loaded) return;
|
|
171
|
+
const node = typeof options.fallback === "string" ? Object.assign(document.createElement("div"), { textContent: options.fallback }) : options.fallback;
|
|
172
|
+
iframe.replaceWith(node);
|
|
173
|
+
}, options.fallbackAfterMs ?? 8e3);
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
iframe,
|
|
177
|
+
reload() {
|
|
178
|
+
port?.disconnect();
|
|
179
|
+
port = void 0;
|
|
180
|
+
clientHello = void 0;
|
|
181
|
+
subscriptions.clear();
|
|
182
|
+
inflight.clear();
|
|
183
|
+
iframe.src = options.url;
|
|
184
|
+
},
|
|
185
|
+
destroy() {
|
|
186
|
+
clearTimeout(fallbackTimer);
|
|
187
|
+
window.removeEventListener("message", onMessage);
|
|
188
|
+
port?.disconnect();
|
|
189
|
+
iframe.remove();
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
export {
|
|
194
|
+
BRIDGE_ACTIONS,
|
|
195
|
+
mountFrameBridge
|
|
196
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { ActionSchema } from '@extension-use/protocol/schemas';
|
|
2
|
+
import { Target } from '@extension-use/protocol';
|
|
3
|
+
import * as z from 'zod';
|
|
4
|
+
|
|
5
|
+
interface SessionInfo {
|
|
6
|
+
id: string;
|
|
7
|
+
transport: string;
|
|
8
|
+
origin: string;
|
|
9
|
+
windowId?: number;
|
|
10
|
+
granted: string[];
|
|
11
|
+
}
|
|
12
|
+
interface ResolvedTab {
|
|
13
|
+
tabId: number;
|
|
14
|
+
windowId: number;
|
|
15
|
+
url: string;
|
|
16
|
+
}
|
|
17
|
+
interface ActionContext {
|
|
18
|
+
session: SessionInfo;
|
|
19
|
+
host: {
|
|
20
|
+
id: string;
|
|
21
|
+
name?: string;
|
|
22
|
+
version?: string;
|
|
23
|
+
};
|
|
24
|
+
target: Target | undefined;
|
|
25
|
+
signal: AbortSignal;
|
|
26
|
+
timeoutMs: number;
|
|
27
|
+
/** Resolve the call's target tab. Throws TARGET_NOT_FOUND or BLOCKED_BY_POLICY. */
|
|
28
|
+
tab(): Promise<ResolvedTab>;
|
|
29
|
+
/** Run a content action in the call's target tab. */
|
|
30
|
+
callContent<O = unknown>(action: string, input: unknown): Promise<O>;
|
|
31
|
+
/**
|
|
32
|
+
* Run another action as the same controller, through the full pipeline
|
|
33
|
+
* (grants, input checks, policy, audit). Used by run.sequence.
|
|
34
|
+
*/
|
|
35
|
+
call(action: string, input: unknown, options?: {
|
|
36
|
+
target?: Target;
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
}): Promise<unknown>;
|
|
39
|
+
/** The actions this controller may call. */
|
|
40
|
+
listActions(): ActionDef[];
|
|
41
|
+
/** Check a URL against the site policy. Throws BLOCKED_BY_POLICY. */
|
|
42
|
+
assertAllowedUrl(url: string): void;
|
|
43
|
+
emit(name: string, payload: unknown, tabId?: number): void;
|
|
44
|
+
}
|
|
45
|
+
interface ActionDef<S extends ActionSchema = ActionSchema> {
|
|
46
|
+
name: string;
|
|
47
|
+
schema: S;
|
|
48
|
+
/** Required for background actions. Content actions are forwarded to the content runtime. */
|
|
49
|
+
handler?: (input: z.output<S["input"]>, ctx: ActionContext) => Promise<z.input<S["output"]>> | z.input<S["output"]>;
|
|
50
|
+
}
|
|
51
|
+
interface ActionGroup {
|
|
52
|
+
name: string;
|
|
53
|
+
/** Manifest `permissions` this group needs. */
|
|
54
|
+
permissions: string[];
|
|
55
|
+
/** Whether this group needs host access to pages (content script or capture). */
|
|
56
|
+
needsHostAccess: boolean;
|
|
57
|
+
actions: ActionDef[];
|
|
58
|
+
}
|
|
59
|
+
/** Define a host-specific action, such as `shop.captureItems`. */
|
|
60
|
+
declare function defineAction<S extends ActionSchema>(name: string, schema: S, handler?: ActionDef<S>["handler"]): ActionDef<S>;
|
|
61
|
+
|
|
62
|
+
declare const builtinGroups: {
|
|
63
|
+
readonly runtime: ActionGroup;
|
|
64
|
+
readonly run: ActionGroup;
|
|
65
|
+
readonly tabs: ActionGroup;
|
|
66
|
+
readonly windows: ActionGroup;
|
|
67
|
+
readonly capture: ActionGroup;
|
|
68
|
+
readonly frames: ActionGroup;
|
|
69
|
+
readonly page: ActionGroup;
|
|
70
|
+
readonly dom: ActionGroup;
|
|
71
|
+
readonly edit: ActionGroup;
|
|
72
|
+
readonly wait: ActionGroup;
|
|
73
|
+
readonly overlay: ActionGroup;
|
|
74
|
+
readonly picker: ActionGroup;
|
|
75
|
+
readonly tabCapture: ActionGroup;
|
|
76
|
+
readonly downloads: ActionGroup;
|
|
77
|
+
readonly bookmarks: ActionGroup;
|
|
78
|
+
readonly history: ActionGroup;
|
|
79
|
+
readonly cookies: ActionGroup;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Groups that need permissions or files beyond the defaults; enable them by
|
|
83
|
+
* name. The browser-data groups (downloads, bookmarks, history, cookies) reach
|
|
84
|
+
* the person's data outside any page, so no host gets them by accident.
|
|
85
|
+
*/
|
|
86
|
+
declare const OPT_IN_GROUPS: readonly (keyof typeof builtinGroups)[];
|
|
87
|
+
/** The default groups: every built-in group except the opt-in ones. */
|
|
88
|
+
declare const allBuiltinGroups: ActionGroup[];
|
|
89
|
+
|
|
90
|
+
export { type ActionGroup as A, OPT_IN_GROUPS as O, type ResolvedTab as R, type SessionInfo as S, type ActionDef as a, builtinGroups as b, type ActionContext as c, allBuiltinGroups as d, defineAction as e };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OFFSCREEN_MESSAGE
|
|
3
|
+
} from "../chunk-5X4QMMKA.js";
|
|
4
|
+
|
|
5
|
+
// src/offscreen/index.ts
|
|
6
|
+
var mounted = false;
|
|
7
|
+
function mountOffscreen() {
|
|
8
|
+
if (mounted) return;
|
|
9
|
+
mounted = true;
|
|
10
|
+
const held = /* @__PURE__ */ new Map();
|
|
11
|
+
const stop = (tabId) => {
|
|
12
|
+
const h = held.get(tabId);
|
|
13
|
+
if (!h) return false;
|
|
14
|
+
held.delete(tabId);
|
|
15
|
+
for (const t of h.stream.getTracks()) t.stop();
|
|
16
|
+
h.video.srcObject = null;
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
const size = (h) => ({ width: h.width, height: h.height });
|
|
20
|
+
async function grab(track) {
|
|
21
|
+
if (typeof ImageCapture === "undefined") return void 0;
|
|
22
|
+
return Promise.race([new ImageCapture(track).grabFrame().catch(() => void 0), new Promise((r) => setTimeout(r, 1e3))]);
|
|
23
|
+
}
|
|
24
|
+
async function start(c) {
|
|
25
|
+
const existing = held.get(c.tabId);
|
|
26
|
+
if (existing) return size(existing);
|
|
27
|
+
if (!c.streamId) throw new Error(`Tab ${c.tabId} is not being captured`);
|
|
28
|
+
const wantWidth = c.width ?? 1280;
|
|
29
|
+
const wantHeight = c.height ?? 800;
|
|
30
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
31
|
+
audio: false,
|
|
32
|
+
video: {
|
|
33
|
+
// Chrome's legacy constraints for tab capture. min = max asks for a
|
|
34
|
+
// fixed size: with only a max, Chrome lowers the size when it's busy.
|
|
35
|
+
mandatory: {
|
|
36
|
+
chromeMediaSource: "tab",
|
|
37
|
+
chromeMediaSourceId: c.streamId,
|
|
38
|
+
minWidth: wantWidth,
|
|
39
|
+
maxWidth: wantWidth,
|
|
40
|
+
minHeight: wantHeight,
|
|
41
|
+
maxHeight: wantHeight,
|
|
42
|
+
maxFrameRate: 30
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const track = stream.getVideoTracks()[0];
|
|
47
|
+
const video = document.createElement("video");
|
|
48
|
+
video.muted = true;
|
|
49
|
+
video.srcObject = stream;
|
|
50
|
+
await video.play();
|
|
51
|
+
if (!video.videoWidth) await new Promise((r) => video.addEventListener("loadedmetadata", r, { once: true }));
|
|
52
|
+
const h = { stream, track, video, width: video.videoWidth, height: video.videoHeight };
|
|
53
|
+
held.set(c.tabId, h);
|
|
54
|
+
track.addEventListener("ended", () => held.get(c.tabId) === h && stop(c.tabId));
|
|
55
|
+
return size(h);
|
|
56
|
+
}
|
|
57
|
+
async function frame(c) {
|
|
58
|
+
const h = held.get(c.tabId);
|
|
59
|
+
if (!h) throw new Error(`Tab ${c.tabId} is not being captured`);
|
|
60
|
+
const bitmap = await grab(h.track);
|
|
61
|
+
const width = bitmap?.width ?? h.video.videoWidth;
|
|
62
|
+
const height = bitmap?.height ?? h.video.videoHeight;
|
|
63
|
+
if (!width || !height) throw new Error("The tab has not painted a frame yet");
|
|
64
|
+
const canvas = new OffscreenCanvas(width, height);
|
|
65
|
+
canvas.getContext("2d").drawImage(bitmap ?? h.video, 0, 0);
|
|
66
|
+
bitmap?.close();
|
|
67
|
+
h.width = width;
|
|
68
|
+
h.height = height;
|
|
69
|
+
const blob = await canvas.convertToBlob({
|
|
70
|
+
type: c.format === "jpeg" ? "image/jpeg" : "image/png",
|
|
71
|
+
...c.quality !== void 0 ? { quality: c.quality / 100 } : {}
|
|
72
|
+
});
|
|
73
|
+
return { dataUrl: await blobToDataUrl(blob), width, height };
|
|
74
|
+
}
|
|
75
|
+
async function handle(c) {
|
|
76
|
+
switch (c.op) {
|
|
77
|
+
case "start":
|
|
78
|
+
return start(c);
|
|
79
|
+
case "stop":
|
|
80
|
+
return { stopped: stop(c.tabId) };
|
|
81
|
+
case "list":
|
|
82
|
+
return { tabIds: [...held.keys()] };
|
|
83
|
+
case "frame":
|
|
84
|
+
return frame(c);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
88
|
+
const command = message?.[OFFSCREEN_MESSAGE];
|
|
89
|
+
if (!command || sender.id !== chrome.runtime.id) return;
|
|
90
|
+
handle(command).then(
|
|
91
|
+
(output) => sendResponse({ ok: true, output }),
|
|
92
|
+
(err) => sendResponse({ ok: false, error: String(err?.message ?? err) })
|
|
93
|
+
);
|
|
94
|
+
return true;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async function blobToDataUrl(blob) {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const reader = new FileReader();
|
|
100
|
+
reader.onload = () => resolve(reader.result);
|
|
101
|
+
reader.onerror = () => reject(reader.error);
|
|
102
|
+
reader.readAsDataURL(blob);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
mountOffscreen
|
|
107
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { b as builtinGroups, A as ActionGroup } from './groups-DC3obZGP.js';
|
|
2
|
+
import '@extension-use/protocol/schemas';
|
|
3
|
+
import '@extension-use/protocol';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Build-time helpers: work out the manifest entries a host needs from the
|
|
8
|
+
* action groups it enables, so extensions ask for no more than they use.
|
|
9
|
+
*
|
|
10
|
+
* // Any build setup (plain JS/TS, or a custom bundler):
|
|
11
|
+
* import { withExtensionUse } from "@extension-use/host/permissions";
|
|
12
|
+
* const manifest = withExtensionUse(baseManifest, {
|
|
13
|
+
* groups: ["tabs", "page", "dom", "overlay"],
|
|
14
|
+
* controllerOrigins: ["https://app.example.com"],
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Lower level:
|
|
18
|
+
* const { permissions, host_permissions } = permissionsFor([builtinGroups.dom, builtinGroups.tabs]);
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
type GroupName = keyof typeof builtinGroups;
|
|
22
|
+
declare const GROUP_NAMES: GroupName[];
|
|
23
|
+
/** The groups enabled when none are named: all but the opt-in ones (tabCapture). */
|
|
24
|
+
declare const DEFAULT_GROUP_NAMES: ("runtime" | "run" | "tabs" | "windows" | "capture" | "frames" | "page" | "dom" | "edit" | "wait" | "overlay" | "picker" | "tabCapture" | "downloads" | "bookmarks" | "history" | "cookies")[];
|
|
25
|
+
interface PermissionOptions {
|
|
26
|
+
/** Pages the content runtime runs on. Default ["<all_urls>"]. */
|
|
27
|
+
matches?: string[];
|
|
28
|
+
/** Inject the content script on demand (adds "scripting"). Default true. */
|
|
29
|
+
injectOnDemand?: boolean;
|
|
30
|
+
/** Extension pages host a frame controller in the side panel (adds "sidePanel"). */
|
|
31
|
+
sidePanel?: boolean;
|
|
32
|
+
/** Remote control through a relay (adds "storage" and "alarms"). */
|
|
33
|
+
remote?: boolean;
|
|
34
|
+
}
|
|
35
|
+
declare function permissionsFor(groups: ActionGroup[], options?: PermissionOptions): {
|
|
36
|
+
permissions: string[];
|
|
37
|
+
host_permissions: string[];
|
|
38
|
+
};
|
|
39
|
+
interface ManifestOptions extends Omit<PermissionOptions, "sidePanel"> {
|
|
40
|
+
/** Built-in groups the host enables (must match defineHost). Default: all but the opt-in tabCapture group. */
|
|
41
|
+
groups?: GroupName[];
|
|
42
|
+
/** Web app origins allowed to connect from a normal tab (externally_connectable). */
|
|
43
|
+
controllerOrigins?: string[];
|
|
44
|
+
/** Add the "sidePanel" permission. Default true. */
|
|
45
|
+
sidePanel?: boolean;
|
|
46
|
+
}
|
|
47
|
+
interface ManifestFields {
|
|
48
|
+
permissions?: string[];
|
|
49
|
+
host_permissions?: string[];
|
|
50
|
+
externally_connectable?: {
|
|
51
|
+
matches?: string[];
|
|
52
|
+
ids?: string[];
|
|
53
|
+
accepts_tls_channel_id?: boolean;
|
|
54
|
+
};
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
}
|
|
57
|
+
/** Return a copy of `manifest` with the permissions and externally_connectable entries Extension-use needs. */
|
|
58
|
+
declare function withExtensionUse<M extends ManifestFields>(manifest: M, options?: ManifestOptions): M;
|
|
59
|
+
|
|
60
|
+
export { DEFAULT_GROUP_NAMES, GROUP_NAMES, type GroupName, type ManifestFields, type ManifestOptions, type PermissionOptions, permissionsFor, withExtensionUse };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_GROUP_NAMES,
|
|
3
|
+
GROUP_NAMES,
|
|
4
|
+
permissionsFor,
|
|
5
|
+
withExtensionUse
|
|
6
|
+
} from "./chunk-4SALFB5W.js";
|
|
7
|
+
import "./chunk-QYMPEWZP.js";
|
|
8
|
+
import "./chunk-5X4QMMKA.js";
|
|
9
|
+
export {
|
|
10
|
+
DEFAULT_GROUP_NAMES,
|
|
11
|
+
GROUP_NAMES,
|
|
12
|
+
permissionsFor,
|
|
13
|
+
withExtensionUse
|
|
14
|
+
};
|