@webext-core/messaging 1.3.1 → 2.0.0
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/README.md +1 -1
- package/lib/chunk-ALTBYIMW.js +149 -0
- package/lib/generic-77a327a6.d.ts +128 -0
- package/lib/index.cjs +119 -83
- package/lib/index.d.ts +25 -99
- package/lib/index.global.js +312 -85
- package/lib/index.js +21 -115
- package/lib/page.cjs +259 -0
- package/lib/page.d.ts +86 -0
- package/lib/page.global.js +447 -0
- package/lib/page.js +100 -0
- package/package.json +23 -15
- package/LICENSE +0 -21
package/lib/index.global.js
CHANGED
|
@@ -2,10 +2,27 @@
|
|
|
2
2
|
var webExtCoreMessaging = (() => {
|
|
3
3
|
var __create = Object.create;
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
|
+
var __defProps = Object.defineProperties;
|
|
5
6
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
8
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
10
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
11
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
13
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
+
var __spreadValues = (a, b) => {
|
|
15
|
+
for (var prop in b || (b = {}))
|
|
16
|
+
if (__hasOwnProp.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
if (__getOwnPropSymbols)
|
|
19
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
20
|
+
if (__propIsEnum.call(b, prop))
|
|
21
|
+
__defNormalProp(a, prop, b[prop]);
|
|
22
|
+
}
|
|
23
|
+
return a;
|
|
24
|
+
};
|
|
25
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
9
26
|
var __commonJS = (cb, mod) => function __require() {
|
|
10
27
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
28
|
};
|
|
@@ -22,10 +39,18 @@ var webExtCoreMessaging = (() => {
|
|
|
22
39
|
return to;
|
|
23
40
|
};
|
|
24
41
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
42
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
43
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
44
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
45
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
46
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
47
|
mod
|
|
27
48
|
));
|
|
28
49
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
|
+
var __publicField = (obj, key, value) => {
|
|
51
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
52
|
+
return value;
|
|
53
|
+
};
|
|
29
54
|
var __async = (__this, __arguments, generator) => {
|
|
30
55
|
return new Promise((resolve, reject) => {
|
|
31
56
|
var fulfilled = (value) => {
|
|
@@ -47,9 +72,9 @@ var webExtCoreMessaging = (() => {
|
|
|
47
72
|
});
|
|
48
73
|
};
|
|
49
74
|
|
|
50
|
-
// ../../node_modules
|
|
75
|
+
// ../../node_modules/webextension-polyfill/dist/browser-polyfill.js
|
|
51
76
|
var require_browser_polyfill = __commonJS({
|
|
52
|
-
"../../node_modules
|
|
77
|
+
"../../node_modules/webextension-polyfill/dist/browser-polyfill.js"(exports, module) {
|
|
53
78
|
(function(global, factory) {
|
|
54
79
|
if (typeof define === "function" && define.amd) {
|
|
55
80
|
define("webextension-polyfill", ["module"], factory);
|
|
@@ -1047,104 +1072,306 @@ var webExtCoreMessaging = (() => {
|
|
|
1047
1072
|
__export(src_exports, {
|
|
1048
1073
|
defineExtensionMessaging: () => defineExtensionMessaging
|
|
1049
1074
|
});
|
|
1075
|
+
|
|
1076
|
+
// src/extension.ts
|
|
1050
1077
|
var import_webextension_polyfill = __toESM(require_browser_polyfill(), 1);
|
|
1051
|
-
|
|
1052
|
-
|
|
1078
|
+
|
|
1079
|
+
// ../../node_modules/serialize-error/error-constructors.js
|
|
1080
|
+
var list = [
|
|
1081
|
+
// Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
|
|
1082
|
+
EvalError,
|
|
1083
|
+
RangeError,
|
|
1084
|
+
ReferenceError,
|
|
1085
|
+
SyntaxError,
|
|
1086
|
+
TypeError,
|
|
1087
|
+
URIError,
|
|
1088
|
+
// Built-in errors
|
|
1089
|
+
globalThis.DOMException,
|
|
1090
|
+
// Node-specific errors
|
|
1091
|
+
// https://nodejs.org/api/errors.html
|
|
1092
|
+
globalThis.AssertionError,
|
|
1093
|
+
globalThis.SystemError
|
|
1094
|
+
].filter(Boolean).map(
|
|
1095
|
+
(constructor) => [constructor.name, constructor]
|
|
1096
|
+
);
|
|
1097
|
+
var errorConstructors = new Map(list);
|
|
1098
|
+
var error_constructors_default = errorConstructors;
|
|
1099
|
+
|
|
1100
|
+
// ../../node_modules/serialize-error/index.js
|
|
1101
|
+
var NonError = class extends Error {
|
|
1102
|
+
constructor(message) {
|
|
1103
|
+
super(NonError._prepareSuperMessage(message));
|
|
1104
|
+
__publicField(this, "name", "NonError");
|
|
1105
|
+
}
|
|
1106
|
+
static _prepareSuperMessage(message) {
|
|
1107
|
+
try {
|
|
1108
|
+
return JSON.stringify(message);
|
|
1109
|
+
} catch (e) {
|
|
1110
|
+
return String(message);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
var commonProperties = [
|
|
1115
|
+
{
|
|
1116
|
+
property: "name",
|
|
1117
|
+
enumerable: false
|
|
1118
|
+
},
|
|
1119
|
+
{
|
|
1120
|
+
property: "message",
|
|
1121
|
+
enumerable: false
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
property: "stack",
|
|
1125
|
+
enumerable: false
|
|
1126
|
+
},
|
|
1127
|
+
{
|
|
1128
|
+
property: "code",
|
|
1129
|
+
enumerable: true
|
|
1130
|
+
},
|
|
1131
|
+
{
|
|
1132
|
+
property: "cause",
|
|
1133
|
+
enumerable: false
|
|
1134
|
+
}
|
|
1135
|
+
];
|
|
1136
|
+
var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
|
|
1137
|
+
var toJSON = (from) => {
|
|
1138
|
+
toJsonWasCalled.add(from);
|
|
1139
|
+
const json = from.toJSON();
|
|
1140
|
+
toJsonWasCalled.delete(from);
|
|
1141
|
+
return json;
|
|
1142
|
+
};
|
|
1143
|
+
var getErrorConstructor = (name) => {
|
|
1144
|
+
var _a;
|
|
1145
|
+
return (_a = error_constructors_default.get(name)) != null ? _a : Error;
|
|
1146
|
+
};
|
|
1147
|
+
var destroyCircular = ({
|
|
1148
|
+
from,
|
|
1149
|
+
seen,
|
|
1150
|
+
to,
|
|
1151
|
+
forceEnumerable,
|
|
1152
|
+
maxDepth,
|
|
1153
|
+
depth,
|
|
1154
|
+
useToJSON,
|
|
1155
|
+
serialize
|
|
1156
|
+
}) => {
|
|
1157
|
+
if (!to) {
|
|
1158
|
+
if (Array.isArray(from)) {
|
|
1159
|
+
to = [];
|
|
1160
|
+
} else if (!serialize && isErrorLike(from)) {
|
|
1161
|
+
const Error2 = getErrorConstructor(from.name);
|
|
1162
|
+
to = new Error2();
|
|
1163
|
+
} else {
|
|
1164
|
+
to = {};
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
seen.push(from);
|
|
1168
|
+
if (depth >= maxDepth) {
|
|
1169
|
+
return to;
|
|
1170
|
+
}
|
|
1171
|
+
if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
|
|
1172
|
+
return toJSON(from);
|
|
1173
|
+
}
|
|
1174
|
+
const continueDestroyCircular = (value) => destroyCircular({
|
|
1175
|
+
from: value,
|
|
1176
|
+
seen: [...seen],
|
|
1177
|
+
forceEnumerable,
|
|
1178
|
+
maxDepth,
|
|
1179
|
+
depth,
|
|
1180
|
+
useToJSON,
|
|
1181
|
+
serialize
|
|
1182
|
+
});
|
|
1183
|
+
for (const [key, value] of Object.entries(from)) {
|
|
1184
|
+
if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
|
|
1185
|
+
to[key] = "[object Buffer]";
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
|
|
1189
|
+
to[key] = "[object Stream]";
|
|
1190
|
+
continue;
|
|
1191
|
+
}
|
|
1192
|
+
if (typeof value === "function") {
|
|
1193
|
+
continue;
|
|
1194
|
+
}
|
|
1195
|
+
if (!value || typeof value !== "object") {
|
|
1196
|
+
try {
|
|
1197
|
+
to[key] = value;
|
|
1198
|
+
} catch (e) {
|
|
1199
|
+
}
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
if (!seen.includes(from[key])) {
|
|
1203
|
+
depth++;
|
|
1204
|
+
to[key] = continueDestroyCircular(from[key]);
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
to[key] = "[Circular]";
|
|
1208
|
+
}
|
|
1209
|
+
for (const { property, enumerable } of commonProperties) {
|
|
1210
|
+
if (typeof from[property] !== "undefined" && from[property] !== null) {
|
|
1211
|
+
Object.defineProperty(to, property, {
|
|
1212
|
+
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
|
|
1213
|
+
enumerable: forceEnumerable ? true : enumerable,
|
|
1214
|
+
configurable: true,
|
|
1215
|
+
writable: true
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
return to;
|
|
1220
|
+
};
|
|
1221
|
+
function serializeError(value, options = {}) {
|
|
1222
|
+
const {
|
|
1223
|
+
maxDepth = Number.POSITIVE_INFINITY,
|
|
1224
|
+
useToJSON = true
|
|
1225
|
+
} = options;
|
|
1226
|
+
if (typeof value === "object" && value !== null) {
|
|
1227
|
+
return destroyCircular({
|
|
1228
|
+
from: value,
|
|
1229
|
+
seen: [],
|
|
1230
|
+
forceEnumerable: true,
|
|
1231
|
+
maxDepth,
|
|
1232
|
+
depth: 0,
|
|
1233
|
+
useToJSON,
|
|
1234
|
+
serialize: true
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
if (typeof value === "function") {
|
|
1238
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
1239
|
+
}
|
|
1240
|
+
return value;
|
|
1241
|
+
}
|
|
1242
|
+
function deserializeError(value, options = {}) {
|
|
1243
|
+
const { maxDepth = Number.POSITIVE_INFINITY } = options;
|
|
1244
|
+
if (value instanceof Error) {
|
|
1245
|
+
return value;
|
|
1246
|
+
}
|
|
1247
|
+
if (isMinimumViableSerializedError(value)) {
|
|
1248
|
+
const Error2 = getErrorConstructor(value.name);
|
|
1249
|
+
return destroyCircular({
|
|
1250
|
+
from: value,
|
|
1251
|
+
seen: [],
|
|
1252
|
+
to: new Error2(),
|
|
1253
|
+
maxDepth,
|
|
1254
|
+
depth: 0,
|
|
1255
|
+
serialize: false
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
1258
|
+
return new NonError(value);
|
|
1259
|
+
}
|
|
1260
|
+
function isErrorLike(value) {
|
|
1261
|
+
return Boolean(value) && typeof value === "object" && "name" in value && "message" in value && "stack" in value;
|
|
1262
|
+
}
|
|
1263
|
+
function isMinimumViableSerializedError(value) {
|
|
1264
|
+
return Boolean(value) && typeof value === "object" && "message" in value && !Array.isArray(value);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
// src/generic.ts
|
|
1268
|
+
function defineGenericMessanging(config) {
|
|
1269
|
+
let removeRootListener;
|
|
1270
|
+
let perTypeListeners = {};
|
|
1053
1271
|
function cleanupRootListener() {
|
|
1054
|
-
if (Object.entries(
|
|
1055
|
-
|
|
1056
|
-
|
|
1272
|
+
if (Object.entries(perTypeListeners).length === 0) {
|
|
1273
|
+
removeRootListener == null ? void 0 : removeRootListener();
|
|
1274
|
+
removeRootListener = void 0;
|
|
1057
1275
|
}
|
|
1058
1276
|
}
|
|
1059
|
-
let keyListeners = {};
|
|
1060
1277
|
let idSeq = Math.floor(Math.random() * 1e4);
|
|
1061
1278
|
function getNextId() {
|
|
1062
1279
|
return idSeq++;
|
|
1063
1280
|
}
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
throw err;
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1281
|
+
return {
|
|
1282
|
+
sendMessage(type, data, ...args) {
|
|
1283
|
+
return __async(this, null, function* () {
|
|
1284
|
+
var _a2, _b;
|
|
1285
|
+
const message = {
|
|
1286
|
+
id: getNextId(),
|
|
1287
|
+
type,
|
|
1288
|
+
data,
|
|
1289
|
+
timestamp: Date.now()
|
|
1290
|
+
};
|
|
1291
|
+
(_a2 = config.logger) == null ? void 0 : _a2.debug(`[messaging] sendMessage {id=${message.id}} \u2500\u1405`, message, ...args);
|
|
1292
|
+
const response = yield config.sendMessage(message, ...args);
|
|
1293
|
+
const { res, err } = response != null ? response : { err: new Error("No response") };
|
|
1294
|
+
(_b = config.logger) == null ? void 0 : _b.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
1295
|
+
if (err != null)
|
|
1296
|
+
throw deserializeError(err);
|
|
1297
|
+
return res;
|
|
1298
|
+
});
|
|
1299
|
+
},
|
|
1300
|
+
onMessage(type, onReceived) {
|
|
1301
|
+
var _a2, _b, _c;
|
|
1302
|
+
if (removeRootListener == null) {
|
|
1303
|
+
(_a2 = config.logger) == null ? void 0 : _a2.debug(
|
|
1304
|
+
`[messaging] "${type}" initialized the message listener for this context`
|
|
1305
|
+
);
|
|
1306
|
+
removeRootListener = config.addRootListener((message) => {
|
|
1307
|
+
var _a3, _b2;
|
|
1308
|
+
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
1309
|
+
const err = Error(
|
|
1310
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
1311
|
+
message
|
|
1312
|
+
)}`
|
|
1313
|
+
);
|
|
1314
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
1315
|
+
throw err;
|
|
1316
|
+
}
|
|
1317
|
+
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
1318
|
+
const listener = perTypeListeners[message.type];
|
|
1319
|
+
if (listener == null)
|
|
1320
|
+
return;
|
|
1321
|
+
const res = listener(message);
|
|
1092
1322
|
return Promise.resolve(res).then((res2) => {
|
|
1093
|
-
var
|
|
1094
|
-
(
|
|
1323
|
+
var _a4;
|
|
1324
|
+
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { res: res2 });
|
|
1095
1325
|
return { res: res2 };
|
|
1096
1326
|
}).catch((err) => {
|
|
1097
|
-
var
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
errMessage = err.message;
|
|
1101
|
-
else
|
|
1102
|
-
errMessage = String(err);
|
|
1103
|
-
(_a3 = config == null ? void 0 : config.logger) == null ? void 0 : _a3.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
1104
|
-
return { err: errMessage };
|
|
1327
|
+
var _a4;
|
|
1328
|
+
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
1329
|
+
return { err: serializeError(err) };
|
|
1105
1330
|
});
|
|
1106
|
-
}
|
|
1331
|
+
});
|
|
1332
|
+
}
|
|
1333
|
+
if (perTypeListeners[type] != null) {
|
|
1334
|
+
const err = Error(
|
|
1335
|
+
`[messaging] In this JS context, only one listener can be setup for ${type}`
|
|
1336
|
+
);
|
|
1337
|
+
(_b = config.logger) == null ? void 0 : _b.error(err);
|
|
1338
|
+
throw err;
|
|
1339
|
+
}
|
|
1340
|
+
perTypeListeners[type] = onReceived;
|
|
1341
|
+
(_c = config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${type}`);
|
|
1342
|
+
return () => {
|
|
1343
|
+
delete perTypeListeners[type];
|
|
1344
|
+
cleanupRootListener();
|
|
1107
1345
|
};
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
);
|
|
1114
|
-
(_b = config == null ? void 0 : config.logger) == null ? void 0 : _b.error(err);
|
|
1115
|
-
throw err;
|
|
1116
|
-
}
|
|
1117
|
-
keyListeners[type] = onReceived;
|
|
1118
|
-
(_c = config == null ? void 0 : config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${type}`);
|
|
1119
|
-
return function removeListener() {
|
|
1120
|
-
delete keyListeners[type];
|
|
1346
|
+
},
|
|
1347
|
+
removeAllListeners() {
|
|
1348
|
+
Object.keys(perTypeListeners).forEach((type) => {
|
|
1349
|
+
delete perTypeListeners[type];
|
|
1350
|
+
});
|
|
1121
1351
|
cleanupRootListener();
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
// src/extension.ts
|
|
1357
|
+
function defineExtensionMessaging(config) {
|
|
1358
|
+
return defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
|
|
1359
|
+
sendMessage(message, tabId) {
|
|
1130
1360
|
if (tabId == null)
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
cleanupRootListener();
|
|
1146
|
-
}
|
|
1147
|
-
return { onMessage, sendMessage, removeAllMessageListeners };
|
|
1361
|
+
return import_webextension_polyfill.default.runtime.sendMessage(message);
|
|
1362
|
+
return import_webextension_polyfill.default.tabs.sendMessage(tabId, message);
|
|
1363
|
+
},
|
|
1364
|
+
addRootListener(processMessage) {
|
|
1365
|
+
const listener = (message, sender) => {
|
|
1366
|
+
if (typeof message === "object")
|
|
1367
|
+
return processMessage(__spreadProps(__spreadValues({}, message), { sender }));
|
|
1368
|
+
else
|
|
1369
|
+
return processMessage(message);
|
|
1370
|
+
};
|
|
1371
|
+
import_webextension_polyfill.default.runtime.onMessage.addListener(listener);
|
|
1372
|
+
return () => import_webextension_polyfill.default.runtime.onMessage.removeListener(listener);
|
|
1373
|
+
}
|
|
1374
|
+
}));
|
|
1148
1375
|
}
|
|
1149
1376
|
return __toCommonJS(src_exports);
|
|
1150
1377
|
})();
|
package/lib/index.js
CHANGED
|
@@ -1,123 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
__spreadProps,
|
|
3
|
+
__spreadValues,
|
|
4
|
+
defineGenericMessanging
|
|
5
|
+
} from "./chunk-ALTBYIMW.js";
|
|
21
6
|
|
|
22
|
-
// src/
|
|
7
|
+
// src/extension.ts
|
|
23
8
|
import Browser from "webextension-polyfill";
|
|
24
9
|
function defineExtensionMessaging(config) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
function onMessage(type, onReceived) {
|
|
38
|
-
var _a, _b, _c;
|
|
39
|
-
if (rootListener == null) {
|
|
40
|
-
(_a = config == null ? void 0 : config.logger) == null ? void 0 : _a.debug(
|
|
41
|
-
`[messaging] ${type} initialized message listener for this context`
|
|
42
|
-
);
|
|
43
|
-
rootListener = (message, sender) => {
|
|
44
|
-
var _a2, _b2, _c2;
|
|
45
|
-
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
46
|
-
const err = Error(
|
|
47
|
-
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
48
|
-
message
|
|
49
|
-
)}`
|
|
50
|
-
);
|
|
51
|
-
(_a2 = config == null ? void 0 : config.logger) == null ? void 0 : _a2.error(err);
|
|
52
|
-
throw err;
|
|
53
|
-
}
|
|
54
|
-
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message, sender);
|
|
55
|
-
const entries = Object.entries(keyListeners);
|
|
56
|
-
for (const [type2, listener] of entries) {
|
|
57
|
-
if (message.type !== type2)
|
|
58
|
-
continue;
|
|
59
|
-
(_c2 = config == null ? void 0 : config.logger) == null ? void 0 : _c2.debug(`[messaging] onMessage {id=${message.id}} \u140A\u2500`, message);
|
|
60
|
-
const res = listener({
|
|
61
|
-
data: message.data,
|
|
62
|
-
sender,
|
|
63
|
-
timestamp: message.timestamp
|
|
64
|
-
});
|
|
65
|
-
return Promise.resolve(res).then((res2) => {
|
|
66
|
-
var _a3;
|
|
67
|
-
(_a3 = config == null ? void 0 : config.logger) == null ? void 0 : _a3.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { res: res2 });
|
|
68
|
-
return { res: res2 };
|
|
69
|
-
}).catch((err) => {
|
|
70
|
-
var _a3;
|
|
71
|
-
let errMessage;
|
|
72
|
-
if (err instanceof Error)
|
|
73
|
-
errMessage = err.message;
|
|
74
|
-
else
|
|
75
|
-
errMessage = String(err);
|
|
76
|
-
(_a3 = config == null ? void 0 : config.logger) == null ? void 0 : _a3.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
77
|
-
return { err: errMessage };
|
|
78
|
-
});
|
|
79
|
-
}
|
|
10
|
+
return defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
|
|
11
|
+
sendMessage(message, tabId) {
|
|
12
|
+
if (tabId == null)
|
|
13
|
+
return Browser.runtime.sendMessage(message);
|
|
14
|
+
return Browser.tabs.sendMessage(tabId, message);
|
|
15
|
+
},
|
|
16
|
+
addRootListener(processMessage) {
|
|
17
|
+
const listener = (message, sender) => {
|
|
18
|
+
if (typeof message === "object")
|
|
19
|
+
return processMessage(__spreadProps(__spreadValues({}, message), { sender }));
|
|
20
|
+
else
|
|
21
|
+
return processMessage(message);
|
|
80
22
|
};
|
|
81
|
-
Browser.runtime.onMessage.addListener(
|
|
23
|
+
Browser.runtime.onMessage.addListener(listener);
|
|
24
|
+
return () => Browser.runtime.onMessage.removeListener(listener);
|
|
82
25
|
}
|
|
83
|
-
|
|
84
|
-
const err = Error(
|
|
85
|
-
`[messaging] In this JS context, only one listener can be setup for ${type}`
|
|
86
|
-
);
|
|
87
|
-
(_b = config == null ? void 0 : config.logger) == null ? void 0 : _b.error(err);
|
|
88
|
-
throw err;
|
|
89
|
-
}
|
|
90
|
-
keyListeners[type] = onReceived;
|
|
91
|
-
(_c = config == null ? void 0 : config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${type}`);
|
|
92
|
-
return function removeListener() {
|
|
93
|
-
delete keyListeners[type];
|
|
94
|
-
cleanupRootListener();
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function sendMessage(type, data, tabId) {
|
|
98
|
-
return __async(this, null, function* () {
|
|
99
|
-
var _a, _b;
|
|
100
|
-
const message = { id: getNextId(), type, data, timestamp: Date.now() };
|
|
101
|
-
(_a = config == null ? void 0 : config.logger) == null ? void 0 : _a.debug(`[messaging] sendMessage {id=${message.id}} \u2500\u1405`, message);
|
|
102
|
-
let response;
|
|
103
|
-
if (tabId == null)
|
|
104
|
-
response = yield Browser.runtime.sendMessage(message);
|
|
105
|
-
else
|
|
106
|
-
response = yield Browser.tabs.sendMessage(tabId, message);
|
|
107
|
-
const { res, err } = response != null ? response : { err: "No response" };
|
|
108
|
-
(_b = config == null ? void 0 : config.logger) == null ? void 0 : _b.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
109
|
-
if (err != null)
|
|
110
|
-
throw Error(err);
|
|
111
|
-
return res;
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
function removeAllMessageListeners() {
|
|
115
|
-
Object.keys(keyListeners).forEach((type) => {
|
|
116
|
-
delete keyListeners[type];
|
|
117
|
-
});
|
|
118
|
-
cleanupRootListener();
|
|
119
|
-
}
|
|
120
|
-
return { onMessage, sendMessage, removeAllMessageListeners };
|
|
26
|
+
}));
|
|
121
27
|
}
|
|
122
28
|
export {
|
|
123
29
|
defineExtensionMessaging
|