webextension-messages 1.0.2 → 1.0.4

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2026 Morozov Igor (ismorozs)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Webextension Messages
2
+ Easily and neatly describe all communication instances between the background process and tabs in one place. Wait for the result from the message sent in the same instruction flow.
3
+
4
+ ## How to install and prepare
5
+ Install the library through
6
+ ```sh
7
+ npm install webextension-messages
8
+ ```
9
+ then import with
10
+ ```js
11
+ import WebextensionMessages from 'webextension-messages'
12
+ ```
13
+ in your script file.
14
+
15
+ ## Usage
16
+ Library has only one method ```setup```, which creates all message-sending functions and sets up listeners.
17
+ With such a signature:
18
+ ```js
19
+ WebextensionMessages.setup (
20
+ MessageHandlersMap {
21
+ MessageSenderFunctionName1: MessageHandler1 (...MessageParameters1) => ResultToSendBack1
22
+ MessageSenderFunctionName2: MessageHandler2 (...MessageParameters2) => ResultToSendBack2
23
+ MessageSenderFunctionName3: MessageHandler3 (...MessageParameters3) => ResultToSendBack3
24
+ ...
25
+ },
26
+ CommunicationId
27
+ ) =>
28
+ MessageSendersMap {
29
+ MessageSenderFunctionName1: (...MessageParameters1) => PromiseWithResult1
30
+ MessageSenderFunctionName2: (...MessageParameters2) => PromiseWithResult2
31
+ MessageSenderFunctionName3: (...MessageParameters3) => PromiseWithResult3
32
+ ...
33
+ stopCommunication ()
34
+ }
35
+ ```
36
+ Where:
37
+ ```MessageHandlersMap``` - map linking sender and handler function on the receiving end
38
+ ```MessageSendersMap``` - map of sender functions
39
+
40
+ ```MessageSenderFunctionName``` - name of the function that will send the message
41
+ ```MessageHandler``` - function that will handle the message on the receiving end
42
+ ```MessageParameters``` - payload to the sender function and parameters for the handler function
43
+ ```ResultToSendBack``` - return value of the handler function that will be sent back to the sender
44
+ ```PromiseWithResult``` - return value of the sender function, a promise that will be resolved to ```ResultToSendBack``` from the receiver
45
+
46
+ ```CommunicationId``` (optional) - string identifying the declared communication, used only for removing listeners
47
+ ```stopCommunication ()``` - special predefined method for stopping messaging and removing listeners, works only if ```CommunicationId``` is given
48
+
49
+
50
+
51
+
52
+ **Important: the exact same ```setup``` method with the exact same arguments must run on both sides to ensure their proper communication!**
53
+
54
+ ## Examples
55
+ Basic usage:
56
+ ```js
57
+ // shared.js =>
58
+ // code that must be shared between the background and page scripts
59
+ import WebextensionMessages from 'webextension-messages'
60
+ const { sum } = WebextensionMessages.setup({
61
+ sum: (x, y) => x + y,
62
+ })
63
+ export { sum };
64
+
65
+
66
+ // background-script.js =>
67
+ // if there's no sender functions for one of the ends, just import the file to set up listeners
68
+ import from "./shared.js"
69
+
70
+
71
+ // content-script.js =>
72
+ import { sum } from "./shared.js"; // import sender function
73
+ const result = await sum(1, 2); // send a message to the background and wait for the response
74
+ console.log(result);
75
+ // 3
76
+ ```
77
+
78
+ Stop messaging and remove listeners:
79
+ ```js
80
+ // setting up communication
81
+ cosnt { stopCommunication } = WebextensionMessages.setup({
82
+ sum: (x, y) => x + y,
83
+ }, "Math correspondence");
84
+ // give the communication an identifier for both sides to know what set of functions is under discussion
85
+
86
+ // stopping it somewhere lower in the code
87
+ stopCommunication();
88
+ // no more listening for sum() message
89
+ ```