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.
@@ -1,4 +1,4 @@
1
1
  export * from "./receiver.js";
2
2
  export * from "./sender.js";
3
3
  export * from "./types.js";
4
- export { getThisTarget, getTopLevelFrame } from "./thisTarget.js";
4
+ export { getThisFrame, getTopLevelFrame } from "./thisTarget.js";
@@ -2,6 +2,6 @@
2
2
  export * from "./receiver.js";
3
3
  export * from "./sender.js";
4
4
  export * from "./types.js";
5
- export { getThisTarget, getTopLevelFrame } from "./thisTarget.js";
5
+ export { getThisFrame, getTopLevelFrame } from "./thisTarget.js";
6
6
  import { initPrivateApi } from "./thisTarget.js";
7
7
  initPrivateApi();
@@ -1,5 +1,5 @@
1
1
  import { type JsonObject } from "type-fest";
2
- declare type ErrorObject = {
2
+ type ErrorObject = {
3
3
  name?: string;
4
4
  stack?: string;
5
5
  message?: string;
@@ -1,6 +1,6 @@
1
- import { type AnyTarget, type KnownTarget, type TopLevelFrame, type Message, type MessengerMeta, type Sender } from "./types.js";
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 getThisTarget(): Promise<KnownTarget>;
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
- return location.pathname + location.search;
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 getThisTarget() {
116
+ export async function getThisFrame() {
116
117
  await storeTabData(); // It should already have been called by we still need to await it
117
- return thisTarget;
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 getThisTarget();
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,
@@ -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
- declare type WithTarget<Method> = Method extends (...args: infer PreviousArguments) => infer TReturnValue ? (target: Target | PageTarget, ...args: PreviousArguments) => TReturnValue : never;
10
- declare type ActuallyOmitThisParameter<T> = T extends (...args: infer A) => infer R ? (...args: A) => R : T;
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 declare type PublicMethod<Method extends ValueOf<MessengerMethods>> = Asyncify<ActuallyOmitThisParameter<Method>>;
13
- export declare type PublicMethodWithTarget<Method extends ValueOf<MessengerMethods>> = WithTarget<PublicMethod<Method>>;
14
- export declare type MessengerMeta = {
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
- declare type RawMessengerResponse = {
21
+ }
22
+ type RawMessengerResponse = {
18
23
  value: unknown;
19
24
  } | {
20
25
  error: ErrorObject;
21
26
  };
22
- export declare type MessengerResponse = RawMessengerResponse & {
27
+ export type MessengerResponse = RawMessengerResponse & {
23
28
  /** Guarantees that the message was handled by this library */
24
29
  __webextMessenger: true;
25
30
  };
26
- declare type Arguments = any[];
27
- export declare type Method = (this: MessengerMeta, ...args: Arguments) => Promise<unknown>;
28
- export declare type Options = {
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 declare type Message<LocalArguments extends Arguments = Arguments> = {
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 declare type Sender = Runtime.MessageSender & {
48
+ export type Sender = Runtime.MessageSender & {
44
49
  origin?: string;
45
50
  };
46
- export declare type MessengerMessage = Message & {
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 declare type AnyTarget = {
55
+ export interface AnyTarget {
51
56
  tabId?: number | "this";
52
57
  frameId?: number;
53
58
  page?: string;
54
- };
55
- export declare type TopLevelFrame = {
59
+ }
60
+ export interface TopLevelFrame {
56
61
  tabId: number;
57
62
  frameId: 0;
58
- };
59
- export declare type KnownTarget = {
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 declare type Target = {
72
+ }
73
+ export interface Target {
65
74
  tabId: number;
66
75
  frameId?: number;
67
- };
68
- export declare type PageTarget = {
76
+ }
77
+ export interface PageTarget {
69
78
  tabId?: number | "this";
70
79
  page: string;
71
- };
80
+ }
72
81
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.22.0-5",
3
+ "version": "0.22.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",