webext-messenger 0.15.0-2 → 0.15.0-3
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/distribution/receiver.js
CHANGED
@@ -17,7 +17,7 @@ function onMessageListener(message, sender) {
|
|
17
17
|
return;
|
18
18
|
}
|
19
19
|
// Target check must be synchronous (`await` means we're handing the message)
|
20
|
-
const action = getActionForMessage(message.target);
|
20
|
+
const action = getActionForMessage(sender, message.target);
|
21
21
|
if (action === "ignore") {
|
22
22
|
return;
|
23
23
|
}
|
@@ -1,6 +1,10 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
1
|
+
import { MessengerMeta, Sender } from "./types.js";
|
2
|
+
interface AnyTarget {
|
3
|
+
tabId?: number | "this";
|
4
|
+
frameId?: number;
|
5
|
+
page?: string;
|
6
|
+
}
|
7
|
+
export declare function getActionForMessage(from: Sender, { ...to }: AnyTarget): "respond" | "forward" | "ignore";
|
4
8
|
export declare function nameThisTarget(): Promise<void>;
|
5
9
|
declare function __getTabData(this: MessengerMeta): AnyTarget;
|
6
10
|
declare global {
|
@@ -6,9 +6,10 @@ import { debug } from "./shared.js";
|
|
6
6
|
// This CANNOT be awaited because waiting for it means "I will handle the message."
|
7
7
|
// If a message is received before this is ready, it will just have to be ignored.
|
8
8
|
let thisTarget;
|
9
|
-
//
|
10
|
-
|
11
|
-
|
9
|
+
export function getActionForMessage(from, { ...to } // Clone object because we're editing it
|
10
|
+
) {
|
11
|
+
var _a;
|
12
|
+
if (to.page === "any") {
|
12
13
|
return "respond";
|
13
14
|
}
|
14
15
|
// Content scripts only receive messages that are meant for them. In the future
|
@@ -17,7 +18,7 @@ export function getActionForMessage(target) {
|
|
17
18
|
return "respond";
|
18
19
|
}
|
19
20
|
// We're in an extension page, but the target is not one.
|
20
|
-
if (!
|
21
|
+
if (!to.page) {
|
21
22
|
return "forward";
|
22
23
|
}
|
23
24
|
if (!thisTarget) {
|
@@ -25,18 +26,24 @@ export function getActionForMessage(target) {
|
|
25
26
|
// If this *was* the target, then probably no one else answered
|
26
27
|
return "ignore";
|
27
28
|
}
|
29
|
+
// If requests "this" tab, then set it to allow the next condition
|
30
|
+
if (to.tabId === "this" && thisTarget.tabId === ((_a = from.tab) === null || _a === void 0 ? void 0 : _a.id)) {
|
31
|
+
to.tabId = thisTarget.tabId;
|
32
|
+
}
|
28
33
|
// Every `target` key must match `thisTarget`
|
29
|
-
const isThisTarget = Object.entries(
|
34
|
+
const isThisTarget = Object.entries(to).every(
|
30
35
|
// @ts-expect-error Optional properties
|
31
36
|
([key, value]) => thisTarget[key] === value);
|
32
37
|
if (!isThisTarget) {
|
33
|
-
debug("The message’s target is",
|
38
|
+
debug("The message’s target is", to, "but this is", thisTarget);
|
34
39
|
}
|
35
40
|
return isThisTarget ? "respond" : "ignore";
|
36
41
|
}
|
42
|
+
let nameRequested = false;
|
37
43
|
export async function nameThisTarget() {
|
38
44
|
// Same as above: CS receives messages correctly
|
39
|
-
if (!thisTarget && !isContentScript()) {
|
45
|
+
if (!nameRequested && !thisTarget && !isContentScript()) {
|
46
|
+
nameRequested = true;
|
40
47
|
thisTarget = await messenger("__getTabData", {}, { page: "any" });
|
41
48
|
thisTarget.page = location.pathname;
|
42
49
|
}
|
@@ -46,8 +53,9 @@ function __getTabData() {
|
|
46
53
|
return { tabId: (_b = (_a = this.trace[0]) === null || _a === void 0 ? void 0 : _a.tab) === null || _b === void 0 ? void 0 : _b.id, frameId: (_c = this.trace[0]) === null || _c === void 0 ? void 0 : _c.frameId };
|
47
54
|
}
|
48
55
|
export function initPrivateApi() {
|
56
|
+
// Any context can handler this message
|
57
|
+
registerMethods({ __getTabData });
|
49
58
|
if (isBackgroundPage()) {
|
50
59
|
thisTarget = { page: "background" };
|
51
|
-
registerMethods({ __getTabData });
|
52
60
|
}
|
53
61
|
}
|
package/distribution/types.d.ts
CHANGED