webext-messenger 0.22.0-5 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
package/distribution/index.d.ts
CHANGED
package/distribution/index.js
CHANGED
@@ -2,6 +2,6 @@
|
|
2
2
|
export * from "./receiver.js";
|
3
3
|
export * from "./sender.js";
|
4
4
|
export * from "./types.js";
|
5
|
-
export {
|
5
|
+
export { getThisFrame, getTopLevelFrame } from "./thisTarget.js";
|
6
6
|
import { initPrivateApi } from "./thisTarget.js";
|
7
7
|
initPrivateApi();
|
package/distribution/shared.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { type AnyTarget, type
|
1
|
+
import { type AnyTarget, type TopLevelFrame, type Message, type MessengerMeta, type Sender, type FrameTarget } from "./types.js";
|
2
2
|
export declare function getActionForMessage(from: Sender, message: Message): "respond" | "forward" | "ignore";
|
3
3
|
export declare function __getTabData(this: MessengerMeta): AnyTarget;
|
4
|
-
export declare function
|
4
|
+
export declare function getThisFrame(): Promise<FrameTarget>;
|
5
5
|
export declare function getTopLevelFrame(): Promise<TopLevelFrame>;
|
6
6
|
export declare function initPrivateApi(): void;
|
@@ -29,16 +29,17 @@ const thisTarget = isBackground()
|
|
29
29
|
? { page: "background" }
|
30
30
|
: {
|
31
31
|
get page() {
|
32
|
-
|
32
|
+
// Extension pages have relative URLs to simplify comparison
|
33
|
+
const origin = location.protocol.startsWith("http")
|
34
|
+
? location.origin
|
35
|
+
: "";
|
36
|
+
// Don't use the hash
|
37
|
+
return origin + location.pathname + location.search;
|
33
38
|
},
|
34
39
|
};
|
35
40
|
let tabDataStatus =
|
36
41
|
// The background page doesn't have a tab
|
37
|
-
isBackground()
|
38
|
-
// Content scripts don't use named targets yet
|
39
|
-
isContentScript()
|
40
|
-
? "not-needed"
|
41
|
-
: "needed";
|
42
|
+
isBackground() ? "not-needed" : "needed";
|
42
43
|
function compareTargets(to, thisTarget) {
|
43
44
|
for (const [key, value] of Object.entries(to)) {
|
44
45
|
if (thisTarget[key] === value) {
|
@@ -112,15 +113,17 @@ const storeTabData = once(async () => {
|
|
112
113
|
export function __getTabData() {
|
113
114
|
return { tabId: this.trace[0]?.tab?.id, frameId: this.trace[0]?.frameId };
|
114
115
|
}
|
115
|
-
export async function
|
116
|
+
export async function getThisFrame() {
|
116
117
|
await storeTabData(); // It should already have been called by we still need to await it
|
117
|
-
|
118
|
+
const { tabId, frameId } = thisTarget;
|
119
|
+
if (typeof tabId !== "number" || typeof frameId !== "number") {
|
120
|
+
throw new TypeError("This target is not in a frame");
|
121
|
+
}
|
122
|
+
// Rebuild object to return exactly these two properties and nothing more
|
123
|
+
return { tabId, frameId };
|
118
124
|
}
|
119
125
|
export async function getTopLevelFrame() {
|
120
|
-
const { tabId } = await
|
121
|
-
if (typeof tabId !== "number") {
|
122
|
-
throw new TypeError("This target is not in a tab");
|
123
|
-
}
|
126
|
+
const { tabId } = await getThisFrame();
|
124
127
|
return {
|
125
128
|
tabId,
|
126
129
|
frameId: 0,
|
package/distribution/types.d.ts
CHANGED
@@ -1,72 +1,81 @@
|
|
1
1
|
import { type Runtime } from "webextension-polyfill";
|
2
2
|
import { type Asyncify, type ValueOf } from "type-fest";
|
3
3
|
import { type ErrorObject } from "serialize-error";
|
4
|
+
/**
|
5
|
+
* @file Target types are a bit overlapping. That's because some are "request" targets
|
6
|
+
* and some are "known" targets. The difference is that you could "request" `{tabId: 1}`, but you "know" that a specific target is exactly `{tabId: 1, frameId: 5}`
|
7
|
+
* TODO: Cleanup, clarify, deduplicate Target types
|
8
|
+
*/
|
4
9
|
declare global {
|
5
10
|
interface MessengerMethods {
|
6
11
|
_: Method;
|
7
12
|
}
|
8
13
|
}
|
9
|
-
|
10
|
-
|
14
|
+
type WithTarget<Method> = Method extends (...args: infer PreviousArguments) => infer TReturnValue ? (target: Target | PageTarget, ...args: PreviousArguments) => TReturnValue : never;
|
15
|
+
type ActuallyOmitThisParameter<T> = T extends (...args: infer A) => infer R ? (...args: A) => R : T;
|
11
16
|
/** Removes the `this` type and ensure it's always Promised */
|
12
|
-
export
|
13
|
-
export
|
14
|
-
export
|
17
|
+
export type PublicMethod<Method extends ValueOf<MessengerMethods>> = Asyncify<ActuallyOmitThisParameter<Method>>;
|
18
|
+
export type PublicMethodWithTarget<Method extends ValueOf<MessengerMethods>> = WithTarget<PublicMethod<Method>>;
|
19
|
+
export interface MessengerMeta {
|
15
20
|
trace: Sender[];
|
16
|
-
}
|
17
|
-
|
21
|
+
}
|
22
|
+
type RawMessengerResponse = {
|
18
23
|
value: unknown;
|
19
24
|
} | {
|
20
25
|
error: ErrorObject;
|
21
26
|
};
|
22
|
-
export
|
27
|
+
export type MessengerResponse = RawMessengerResponse & {
|
23
28
|
/** Guarantees that the message was handled by this library */
|
24
29
|
__webextMessenger: true;
|
25
30
|
};
|
26
|
-
|
27
|
-
export
|
28
|
-
export
|
31
|
+
type Arguments = any[];
|
32
|
+
export type Method = (this: MessengerMeta, ...args: Arguments) => Promise<unknown>;
|
33
|
+
export interface Options {
|
29
34
|
/**
|
30
35
|
* "Notifications" won't await the response, return values, attempt retries, nor throw errors
|
31
36
|
* @default false
|
32
37
|
*/
|
33
38
|
isNotification?: boolean;
|
34
39
|
trace?: Sender[];
|
35
|
-
}
|
36
|
-
export
|
40
|
+
}
|
41
|
+
export type Message<LocalArguments extends Arguments = Arguments> = {
|
37
42
|
type: keyof MessengerMethods;
|
38
43
|
args: LocalArguments;
|
39
44
|
target: Target | PageTarget;
|
40
45
|
/** If the message is being sent to an intermediary receiver, also set the options */
|
41
46
|
options?: Options;
|
42
47
|
};
|
43
|
-
export
|
48
|
+
export type Sender = Runtime.MessageSender & {
|
44
49
|
origin?: string;
|
45
50
|
};
|
46
|
-
export
|
51
|
+
export type MessengerMessage = Message & {
|
47
52
|
/** Guarantees that a message is meant to be handled by this library */
|
48
53
|
__webextMessenger: true;
|
49
54
|
};
|
50
|
-
export
|
55
|
+
export interface AnyTarget {
|
51
56
|
tabId?: number | "this";
|
52
57
|
frameId?: number;
|
53
58
|
page?: string;
|
54
|
-
}
|
55
|
-
export
|
59
|
+
}
|
60
|
+
export interface TopLevelFrame {
|
56
61
|
tabId: number;
|
57
62
|
frameId: 0;
|
58
|
-
}
|
59
|
-
export
|
63
|
+
}
|
64
|
+
export interface FrameTarget {
|
65
|
+
tabId: number;
|
66
|
+
frameId: number;
|
67
|
+
}
|
68
|
+
export interface KnownTarget {
|
60
69
|
tabId?: number;
|
61
70
|
frameId?: number;
|
62
71
|
page: string;
|
63
|
-
}
|
64
|
-
export
|
72
|
+
}
|
73
|
+
export interface Target {
|
65
74
|
tabId: number;
|
66
75
|
frameId?: number;
|
67
|
-
}
|
68
|
-
export
|
76
|
+
}
|
77
|
+
export interface PageTarget {
|
69
78
|
tabId?: number | "this";
|
70
79
|
page: string;
|
71
|
-
}
|
80
|
+
}
|
72
81
|
export {};
|