webext-messenger 0.21.0 → 0.22.0-2
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
package/distribution/shared.d.ts
CHANGED
@@ -14,4 +14,5 @@ export declare const debug: (...args: any[]) => void;
|
|
14
14
|
export declare const warn: (...args: any[]) => void;
|
15
15
|
export declare function isErrorObject(error: unknown): error is ErrorObject;
|
16
16
|
export declare function delay(milliseconds: number): Promise<void>;
|
17
|
+
export declare function once<Callback extends (...arguments_: unknown[]) => unknown>(function_: Callback): Callback;
|
17
18
|
export {};
|
package/distribution/shared.js
CHANGED
@@ -40,3 +40,14 @@ export async function delay(milliseconds) {
|
|
40
40
|
setTimeout(resolve, milliseconds);
|
41
41
|
});
|
42
42
|
}
|
43
|
+
export function once(function_) {
|
44
|
+
let called = false;
|
45
|
+
let returnValue;
|
46
|
+
return function (...arguments_) {
|
47
|
+
if (called) {
|
48
|
+
returnValue = function_.apply(this, arguments_);
|
49
|
+
called = true;
|
50
|
+
}
|
51
|
+
return returnValue;
|
52
|
+
};
|
53
|
+
}
|
@@ -1,4 +1,5 @@
|
|
1
1
|
import { AnyTarget, Message, MessengerMeta, Sender } 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<AnyTarget>;
|
4
5
|
export declare function initPrivateApi(): void;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { isBackground, isContentScript, isExtensionContext, } from "webext-detect-page";
|
2
2
|
import { messenger } from "./sender.js";
|
3
3
|
import { registerMethods } from "./receiver.js";
|
4
|
-
import { debug, MessengerError } from "./shared.js";
|
4
|
+
import { debug, MessengerError, once } from "./shared.js";
|
5
5
|
/**
|
6
6
|
* @file This file exists because `runtime.sendMessage` acts as a broadcast to
|
7
7
|
* all open extension pages, so the receiver needs a way to figure out if the
|
@@ -91,7 +91,7 @@ export function getActionForMessage(from, message) {
|
|
91
91
|
}
|
92
92
|
return isThisTarget ? "respond" : "ignore";
|
93
93
|
}
|
94
|
-
|
94
|
+
const storeTabData = once(async () => {
|
95
95
|
if (tabDataStatus !== "needed") {
|
96
96
|
return;
|
97
97
|
}
|
@@ -108,13 +108,17 @@ async function storeTabData() {
|
|
108
108
|
// @ts-expect-error TODO: update lib to accept Error#cause
|
109
109
|
{ cause: error });
|
110
110
|
}
|
111
|
-
}
|
111
|
+
});
|
112
112
|
export function __getTabData() {
|
113
113
|
return { tabId: this.trace[0]?.tab?.id, frameId: this.trace[0]?.frameId };
|
114
114
|
}
|
115
|
+
export async function getThisTarget() {
|
116
|
+
await storeTabData(); // It should already have been called by we still need to await it
|
117
|
+
return thisTarget;
|
118
|
+
}
|
115
119
|
export function initPrivateApi() {
|
116
120
|
if (isExtensionContext()) {
|
117
|
-
// Only `runtime` pages can handle this message but I can't remove it
|
121
|
+
// Only `runtime` pages can handle this message but I can't remove it because its listener
|
118
122
|
// also serves the purpose of throwing a specific error when no methods have been registered.
|
119
123
|
// https://github.com/pixiebrix/webext-messenger/pull/80
|
120
124
|
registerMethods({ __getTabData });
|
package/package.json
CHANGED
package/readme.md
CHANGED
@@ -18,7 +18,3 @@ import messenger from "webext-messenger";
|
|
18
18
|
## Context
|
19
19
|
|
20
20
|
- [Initial considerations for this library](https://github.com/pixiebrix/webext-messenger/issues/1)
|
21
|
-
|
22
|
-
## npm publishing
|
23
|
-
|
24
|
-
Collaborators can publish a new version of what's on main [via "workflow_dispatch"](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/) under [Actions » Publish](https://github.com/pixiebrix/webext-messenger/actions/workflows/npm-publish.yml)
|