webext-messenger 0.15.0-3 → 0.15.1-0
Sign up to get free protection for your applications and to get access to all the features.
package/distribution/receiver.js
CHANGED
@@ -2,7 +2,7 @@ import browser from "webextension-polyfill";
|
|
2
2
|
import { serializeError } from "serialize-error";
|
3
3
|
import { messenger } from "./sender.js";
|
4
4
|
import { handlers, isObject, MessengerError, debug, __webextMessenger, } from "./shared.js";
|
5
|
-
import { getContextName } from "webext-detect-page";
|
5
|
+
import { getContextName, isBackgroundPage } from "webext-detect-page";
|
6
6
|
import { getActionForMessage, nameThisTarget } from "./thisTarget.js";
|
7
7
|
export function isMessengerMessage(message) {
|
8
8
|
return (isObject(message) &&
|
@@ -16,7 +16,7 @@ function onMessageListener(message, sender) {
|
|
16
16
|
// TODO: Add test for this eventuality: ignore unrelated messages
|
17
17
|
return;
|
18
18
|
}
|
19
|
-
// Target check must be synchronous (`await` means we're
|
19
|
+
// Target check must be synchronous (`await` means we're handling the message)
|
20
20
|
const action = getActionForMessage(sender, message.target);
|
21
21
|
if (action === "ignore") {
|
22
22
|
return;
|
@@ -25,36 +25,39 @@ function onMessageListener(message, sender) {
|
|
25
25
|
}
|
26
26
|
// This function can only be called when the message *will* be handled locally.
|
27
27
|
// Returning "undefined" or throwing an error will still handle it.
|
28
|
-
async function handleMessage(message, sender,
|
29
|
-
|
30
|
-
|
28
|
+
async function handleMessage(message, sender,
|
29
|
+
// Once messages reach this function they cannot be "ignored", they're already being handled
|
30
|
+
action) {
|
31
|
+
const { type, target, args, options = {} } = message;
|
32
|
+
const { trace = [] } = options;
|
33
|
+
trace.push(sender);
|
34
|
+
const meta = { trace };
|
35
|
+
debug(type, "↘️ received", { sender, args, wasForwarded: trace.length > 1 });
|
31
36
|
let handleMessage;
|
32
37
|
if (action === "forward") {
|
33
38
|
debug(type, "🔀 forwarded", { sender, target });
|
34
|
-
handleMessage = async () => messenger(type,
|
39
|
+
handleMessage = async () => messenger(type, meta, target, ...args);
|
35
40
|
}
|
36
41
|
else {
|
37
42
|
const localHandler = handlers.get(type);
|
38
43
|
if (!localHandler) {
|
39
44
|
throw new MessengerError(`No handler registered for ${type} in ${getContextName()}`);
|
40
45
|
}
|
41
|
-
debug(type, "➡️ will be handled here");
|
42
|
-
const meta = { trace: [sender] };
|
46
|
+
debug(type, "➡️ will be handled here,", getContextName());
|
43
47
|
handleMessage = async () => localHandler.apply(meta, args);
|
44
48
|
}
|
45
|
-
|
46
|
-
|
47
|
-
// Errors must be serialized because the stacktraces are currently lost on Chrome
|
49
|
+
const response = await handleMessage().then((value) => ({ value }), (error) => ({
|
50
|
+
// Errors must be serialized because the stack traces are currently lost on Chrome
|
48
51
|
// and https://github.com/mozilla/webextension-polyfill/issues/210
|
49
52
|
error: serializeError(error),
|
50
|
-
}))
|
51
|
-
|
52
|
-
|
53
|
-
return { ...response, __webextMessenger };
|
54
|
-
});
|
53
|
+
}));
|
54
|
+
debug(type, "↗️ responding", response);
|
55
|
+
return { ...response, __webextMessenger };
|
55
56
|
}
|
56
57
|
export function registerMethods(methods) {
|
57
|
-
|
58
|
+
if (!isBackgroundPage()) {
|
59
|
+
void nameThisTarget();
|
60
|
+
}
|
58
61
|
for (const [type, method] of Object.entries(methods)) {
|
59
62
|
if (handlers.has(type)) {
|
60
63
|
throw new MessengerError(`Handler already set for ${type}`);
|
package/distribution/sender.js
CHANGED
@@ -17,15 +17,15 @@ function makeMessage(type, args, target, options) {
|
|
17
17
|
};
|
18
18
|
}
|
19
19
|
// Do not turn this into an `async` function; Notifications must turn `void`
|
20
|
-
function manageConnection(type, options, sendMessage) {
|
20
|
+
function manageConnection(type, options, target, sendMessage) {
|
21
21
|
if (!options.isNotification) {
|
22
|
-
return manageMessage(type, sendMessage);
|
22
|
+
return manageMessage(type, target, sendMessage);
|
23
23
|
}
|
24
24
|
void sendMessage().catch((error) => {
|
25
25
|
debug(type, "notification failed", { error });
|
26
26
|
});
|
27
27
|
}
|
28
|
-
async function manageMessage(type, sendMessage) {
|
28
|
+
async function manageMessage(type, target, sendMessage) {
|
29
29
|
const response = await pRetry(async () => {
|
30
30
|
const response = await sendMessage();
|
31
31
|
if (!isMessengerResponse(response)) {
|
@@ -37,7 +37,9 @@ async function manageMessage(type, sendMessage) {
|
|
37
37
|
factor: 1.3,
|
38
38
|
maxRetryTime: 4000,
|
39
39
|
onFailedAttempt(error) {
|
40
|
-
if (
|
40
|
+
if (
|
41
|
+
// Don't retry sending to the background page unless it really hasn't loaded yet
|
42
|
+
(target.page !== "background" && error instanceof MessengerError) ||
|
41
43
|
String(error.message).startsWith(errorNonExistingTarget)) {
|
42
44
|
debug(type, "will retry. Attempt", error.attemptNumber);
|
43
45
|
}
|
@@ -68,11 +70,11 @@ function messenger(type, options, target, ...args) {
|
|
68
70
|
debug(type, "↗️ sending message to runtime");
|
69
71
|
return browser.runtime.sendMessage(makeMessage(type, args, target, options));
|
70
72
|
};
|
71
|
-
return manageConnection(type, options, sendMessage);
|
73
|
+
return manageConnection(type, options, target, sendMessage);
|
72
74
|
}
|
73
75
|
// Contexts without direct Tab access must go through background
|
74
76
|
if (!browser.tabs) {
|
75
|
-
return manageConnection(type, options, async () => {
|
77
|
+
return manageConnection(type, options, target, async () => {
|
76
78
|
debug(type, "↗️ sending message to runtime");
|
77
79
|
return browser.runtime.sendMessage(makeMessage(type, args, target, options));
|
78
80
|
});
|
@@ -80,7 +82,7 @@ function messenger(type, options, target, ...args) {
|
|
80
82
|
// `frameId` must be specified. If missing, the message is sent to every frame
|
81
83
|
const { tabId, frameId = 0 } = target;
|
82
84
|
// Message tab directly
|
83
|
-
return manageConnection(type, options, async () => {
|
85
|
+
return manageConnection(type, options, target, async () => {
|
84
86
|
debug(type, "↗️ sending message to tab", tabId, "frame", frameId);
|
85
87
|
return browser.tabs.sendMessage(tabId, makeMessage(type, args, target, options), {
|
86
88
|
frameId,
|
@@ -1,9 +1,4 @@
|
|
1
|
-
import { MessengerMeta, Sender } from "./types.js";
|
2
|
-
interface AnyTarget {
|
3
|
-
tabId?: number | "this";
|
4
|
-
frameId?: number;
|
5
|
-
page?: string;
|
6
|
-
}
|
1
|
+
import { AnyTarget, MessengerMeta, Sender } from "./types.js";
|
7
2
|
export declare function getActionForMessage(from: Sender, { ...to }: AnyTarget): "respond" | "forward" | "ignore";
|
8
3
|
export declare function nameThisTarget(): Promise<void>;
|
9
4
|
declare function __getTabData(this: MessengerMeta): AnyTarget;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { isBackgroundPage, isContentScript } from "webext-detect-page";
|
1
|
+
import { isBackgroundPage, isContentScript, isExtensionContext, } from "webext-detect-page";
|
2
2
|
import { messenger } from "./sender.js";
|
3
3
|
import { registerMethods } from "./receiver.js";
|
4
4
|
import { debug } from "./shared.js";
|
@@ -26,7 +26,7 @@ export function getActionForMessage(from, { ...to } // Clone object because we'r
|
|
26
26
|
// If this *was* the target, then probably no one else answered
|
27
27
|
return "ignore";
|
28
28
|
}
|
29
|
-
//
|
29
|
+
// Set "this" tab to the current tabId
|
30
30
|
if (to.tabId === "this" && thisTarget.tabId === ((_a = from.tab) === null || _a === void 0 ? void 0 : _a.id)) {
|
31
31
|
to.tabId = thisTarget.tabId;
|
32
32
|
}
|
@@ -53,9 +53,11 @@ function __getTabData() {
|
|
53
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 };
|
54
54
|
}
|
55
55
|
export function initPrivateApi() {
|
56
|
-
// Any context can handler this message
|
57
|
-
registerMethods({ __getTabData });
|
58
56
|
if (isBackgroundPage()) {
|
59
57
|
thisTarget = { page: "background" };
|
60
58
|
}
|
59
|
+
if (isExtensionContext()) {
|
60
|
+
// Any context can handler this message
|
61
|
+
registerMethods({ __getTabData });
|
62
|
+
}
|
61
63
|
}
|
package/distribution/types.d.ts
CHANGED
@@ -45,6 +45,11 @@ export declare type MessengerMessage = Message & {
|
|
45
45
|
/** Guarantees that a message is meant to be handled by this library */
|
46
46
|
__webextMessenger: true;
|
47
47
|
};
|
48
|
+
export interface AnyTarget {
|
49
|
+
tabId?: number | "this";
|
50
|
+
frameId?: number;
|
51
|
+
page?: string;
|
52
|
+
}
|
48
53
|
export interface Target {
|
49
54
|
tabId: number;
|
50
55
|
frameId?: number;
|