webextension-messages 1.0.0 → 1.0.2
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/dist/webextension-messages.js +1392 -0
- package/package.json +8 -3
- package/{webextension-messages.js → src/index.js} +19 -15
- package/webpack.config.js +17 -0
package/package.json
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webextension-messages",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Package simplifying the exchange of messages between the background and tabs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Igor Morozov",
|
|
7
|
-
"main": "webextension-messages.js",
|
|
7
|
+
"main": "./dist/webextension-messages.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"start": "webpack"
|
|
10
|
+
},
|
|
8
11
|
"devDependencies": {
|
|
9
|
-
"webextension-polyfill": "0.12.0"
|
|
12
|
+
"webextension-polyfill": "0.12.0",
|
|
13
|
+
"webpack": "5.108.4",
|
|
14
|
+
"webpack-cli": "7.2.1"
|
|
10
15
|
}
|
|
11
16
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
const browser = require("webextension-polyfill");
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const STOP_ACTION = `WEBEXTENSION_MESSAGES_STOP_COMMUNICATION`;
|
|
4
4
|
const RESULT_PROMISES = {};
|
|
5
5
|
const ACTIONS = {};
|
|
6
6
|
|
|
7
7
|
browser.runtime.onMessage.addListener((message) => {
|
|
8
|
-
if (message.action ===
|
|
9
|
-
|
|
8
|
+
if (message.action === STOP_ACTION) {
|
|
9
|
+
stopCommunication(message.payload);
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
function setupCommunication (actions, messagesId = generateId()) {
|
|
14
14
|
const messages = Object.fromEntries(
|
|
15
15
|
Object.entries(actions).map(([action]) => [
|
|
16
16
|
action,
|
|
17
|
-
(payload) =>
|
|
17
|
+
(payload) => sendMessageForResult(action, payload),
|
|
18
18
|
]),
|
|
19
19
|
);
|
|
20
20
|
|
|
@@ -24,11 +24,11 @@ export default function (actions, messagesId = generateId()) {
|
|
|
24
24
|
|
|
25
25
|
return {
|
|
26
26
|
...messages,
|
|
27
|
-
|
|
27
|
+
stopCommunication: () => stopCommunication(messagesId, true),
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
async function onMessage(message, actions = {}) {
|
|
31
|
+
async function onMessage (message, actions = {}) {
|
|
32
32
|
if (RESULT_PROMISES[message.resultId]) {
|
|
33
33
|
RESULT_PROMISES[message.resultId](message.payload);
|
|
34
34
|
delete RESULT_PROMISES[message.resultId];
|
|
@@ -41,13 +41,13 @@ async function onMessage(message, actions = {}) {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function
|
|
44
|
+
function sendMessageForResult (action, payload) {
|
|
45
45
|
const { resultId, promise } = createResultPromise();
|
|
46
46
|
sendMessage(action, payload, resultId);
|
|
47
47
|
return promise;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
function sendMessage(action, payload, resultId) {
|
|
50
|
+
function sendMessage (action, payload, resultId) {
|
|
51
51
|
const message = { action, payload, resultId };
|
|
52
52
|
|
|
53
53
|
if (isBackgroundScript()) {
|
|
@@ -57,7 +57,7 @@ function sendMessage(action, payload, resultId) {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
function createResultPromise() {
|
|
60
|
+
function createResultPromise () {
|
|
61
61
|
const resultId = generateId();
|
|
62
62
|
|
|
63
63
|
return {
|
|
@@ -66,26 +66,30 @@ function createResultPromise() {
|
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
function isBackgroundScript() {
|
|
69
|
+
function isBackgroundScript () {
|
|
70
70
|
return (
|
|
71
71
|
window.location.protocol === "chrome-extension:" ||
|
|
72
72
|
window.location.protocol === "moz-extension:"
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
function generateId() {
|
|
76
|
+
function generateId () {
|
|
77
77
|
return `${Date.now()}-${Math.random()}`;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
function getCurrentTab() {
|
|
80
|
+
function getCurrentTab () {
|
|
81
81
|
return browser.tabs.query({ active: true, currentWindow: true });
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
function
|
|
84
|
+
function stopCommunication (messagesId, sendToReceiver) {
|
|
85
85
|
browser.runtime.onMessage.removeListener(ACTIONS[messagesId]);
|
|
86
86
|
delete ACTIONS[messagesId];
|
|
87
87
|
|
|
88
88
|
if (sendToReceiver) {
|
|
89
|
-
sendMessage(
|
|
89
|
+
sendMessage(STOP_ACTION, messagesId);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
+
|
|
93
|
+
module.exports = {
|
|
94
|
+
setup: setupCommunication,
|
|
95
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
entry: "./src/index.js",
|
|
3
|
+
output: {
|
|
4
|
+
filename: "webextension-messages.js",
|
|
5
|
+
library: "WebextensionMessages",
|
|
6
|
+
libraryTarget: "umd",
|
|
7
|
+
globalObject: "this",
|
|
8
|
+
},
|
|
9
|
+
mode: "development",
|
|
10
|
+
watch: true,
|
|
11
|
+
|
|
12
|
+
stats: {
|
|
13
|
+
colors: true,
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
devtool: false,
|
|
17
|
+
};
|