webext-messenger 0.27.0 → 0.28.1

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.
@@ -1,5 +1,5 @@
1
1
  import { serializeError } from "serialize-error";
2
- import { getContextName } from "webext-detect-page";
2
+ import { getContextName } from "webext-detect";
3
3
  import { messenger } from "./sender.js";
4
4
  import { isObject, MessengerError, __webextMessenger } from "./shared.js";
5
5
  import { log } from "./logging.js";
@@ -1,5 +1,5 @@
1
1
  import pRetry from "p-retry";
2
- import { isBackground } from "webext-detect-page";
2
+ import { isBackground } from "webext-detect";
3
3
  import { deserializeError } from "serialize-error";
4
4
  import { isObject, MessengerError, __webextMessenger } from "./shared.js";
5
5
  import { log } from "./logging.js";
@@ -113,7 +113,10 @@ async function manageMessage(type, target, seq, retry, sendMessage) {
113
113
  log.debug(type, seq, "will retry. Attempt", error.attemptNumber);
114
114
  },
115
115
  }).catch((error) => {
116
- if (error?.message === _errorNonExistingTarget) {
116
+ if (error &&
117
+ typeof error === "object" &&
118
+ "message" in error &&
119
+ error?.message === _errorNonExistingTarget) {
117
120
  throw new MessengerError(`The target ${JSON.stringify(target)} for ${type} was not found`);
118
121
  }
119
122
  events.dispatchEvent(new CustomEvent("attempts-exhausted", {
@@ -1,4 +1,4 @@
1
- import { isBackground, isContentScript } from "webext-detect-page";
1
+ import { isBackground, isContentScript } from "webext-detect";
2
2
  export function compareTargets(to, thisTarget) {
3
3
  for (const [key, value] of Object.entries(to)) {
4
4
  if (thisTarget[key] === value) {
@@ -1,7 +1,7 @@
1
1
  import { assert, describe, test, vi } from "vitest";
2
2
  import { getActionForMessage } from "./targetLogic.js";
3
- import { isContentScript, isBackground } from "webext-detect-page";
4
- vi.mock("webext-detect-page");
3
+ import { isContentScript, isBackground } from "webext-detect";
4
+ vi.mock("webext-detect");
5
5
  const tab = {
6
6
  id: 1,
7
7
  url: "http://example.com",
@@ -1,7 +1,8 @@
1
- import { getContextName, isBackground, isExtensionContext, } from "webext-detect-page";
1
+ import { getContextName, isBackground, isExtensionContext, isOffscreenDocument, } from "webext-detect";
2
2
  import { messenger } from "./sender.js";
3
3
  import { registerMethods } from "./receiver.js";
4
4
  import { MessengerError, once } from "./shared.js";
5
+ import { pEvent } from "p-event";
5
6
  /**
6
7
  * @file This file exists because `runtime.sendMessage` acts as a broadcast to
7
8
  * all open extension pages, so the receiver needs a way to figure out if the
@@ -25,9 +26,12 @@ import { MessengerError, once } from "./shared.js";
25
26
  // Soft warning: Race conditions are possible.
26
27
  // This CANNOT be awaited because waiting for it means "I will handle the message."
27
28
  // If a message is received before this is ready, it will just have to be ignored.
28
- export const thisTarget = isBackground()
29
- ? { page: "background" }
30
- : {
29
+ export const thisTarget = (() => {
30
+ if (isBackground())
31
+ return { page: "background" };
32
+ if (isOffscreenDocument())
33
+ return { page: "offscreen" };
34
+ return {
31
35
  get page() {
32
36
  // Extension pages have relative URLs to simplify comparison
33
37
  const origin = location.protocol.startsWith("http")
@@ -37,9 +41,10 @@ export const thisTarget = isBackground()
37
41
  return origin + location.pathname + location.search;
38
42
  },
39
43
  };
44
+ })();
40
45
  let tabDataStatus =
41
- // The background page doesn't have a tab
42
- isBackground() ? "not-needed" : "needed";
46
+ // Exclude contexts that don't have a tab associated to them
47
+ isBackground() || isOffscreenDocument() ? "not-needed" : "needed";
43
48
  export function getTabDataStatus() {
44
49
  return tabDataStatus;
45
50
  }
@@ -47,6 +52,11 @@ const storeTabData = once(async () => {
47
52
  if (tabDataStatus !== "needed") {
48
53
  return;
49
54
  }
55
+ // If the page is prerendering, wait for the change to be able to get the tab data so the frameId is correct
56
+ // https://developer.mozilla.org/en-US/docs/Web/API/Document/prerenderingchange_event
57
+ if ("prerendering" in document && Boolean(document.prerendering)) {
58
+ await pEvent(document, "prerenderingchange");
59
+ }
50
60
  try {
51
61
  tabDataStatus = "pending";
52
62
  Object.assign(thisTarget, await messenger("__getTabData", {}, { page: "any" }));
@@ -67,8 +77,7 @@ export async function getThisFrame() {
67
77
  if (typeof tabId !== "number" || typeof frameId !== "number") {
68
78
  let moreInfo = "(error retrieving context information)";
69
79
  try {
70
- moreInfo = `(context: ${getContextName()}, url: ${globalThis.location
71
- ?.href})`;
80
+ moreInfo = `(context: ${getContextName()}, url: ${globalThis.location?.href})`;
72
81
  }
73
82
  catch { }
74
83
  throw new TypeError(`This target is not in a frame ${moreInfo}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.27.0",
3
+ "version": "0.28.1",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -26,34 +26,36 @@
26
26
  "watch": "tsc --watch"
27
27
  },
28
28
  "dependencies": {
29
- "p-retry": "^6.2.0",
29
+ "p-event": "^6.0.1",
30
+ "p-retry": "^6.2.1",
30
31
  "serialize-error": "^11.0.3",
31
- "type-fest": "^4.13.0",
32
- "webext-detect-page": "^5.0.1"
32
+ "type-fest": "^4.29.1",
33
+ "webext-detect": "^5.3.1",
34
+ "webext-events": "^3.1.1"
33
35
  },
34
36
  "@parcel/resolver-default": {
35
37
  "packageExports": true
36
38
  },
37
39
  "devDependencies": {
38
40
  "@parcel/config-webextension": "^2.11.0",
39
- "@sindresorhus/tsconfig": "^5.0.0",
40
- "@types/chrome": "^0.0.263",
41
- "@types/tape": "^5.6.4",
42
- "@types/webextension-polyfill": "^0.10.7",
41
+ "@sindresorhus/tsconfig": "^7.0.0",
42
+ "@types/chrome": "^0.0.287",
43
+ "@types/tape": "^5.6.5",
44
+ "@types/webextension-polyfill": "^0.12.1",
43
45
  "buffer": "^6.0.3",
44
46
  "eslint": "^8.57.0",
45
- "eslint-config-pixiebrix": "^0.37.2",
47
+ "eslint-config-pixiebrix": "^0.41.1",
46
48
  "events": "^3.3.0",
47
49
  "npm-run-all": "^4.1.5",
48
50
  "parcel": "^2.11.0",
49
51
  "path-browserify": "^1.0.1",
50
52
  "process": "^0.11.10",
51
53
  "stream-browserify": "^3.0.0",
52
- "tape": "^5.7.5",
53
- "typescript": "^5.4.2",
54
- "vitest": "^1.4.0",
55
- "webext-content-scripts": "^2.6.1",
56
- "webextension-polyfill": "^0.10.0"
54
+ "tape": "^5.9.0",
55
+ "typescript": "^5.7.2",
56
+ "vitest": "^2.1.6",
57
+ "webext-content-scripts": "^2.7.0",
58
+ "webextension-polyfill": "^0.12.0"
57
59
  },
58
60
  "targets": {
59
61
  "main": false,
@@ -72,7 +74,8 @@
72
74
  "run": {
73
75
  "startUrl": [
74
76
  "https://fregante.github.io/pixiebrix-testing-ground/Will-call-background-methods",
75
- "https://fregante.github.io/pixiebrix-testing-ground/Will-call-other-CS-via-background"
77
+ "https://fregante.github.io/pixiebrix-testing-ground/Will-call-other-CS-via-background",
78
+ "https://fregante.github.io/pixiebrix-testing-ground/Will-call-offscreen-methods"
76
79
  ]
77
80
  }
78
81
  }
package/readme.md CHANGED
@@ -17,8 +17,7 @@ import messenger from "webext-messenger";
17
17
 
18
18
  ## Context
19
19
 
20
- - [Initial considertions for this library](https://github.com/pixiebrix/webext-messenger/issues/1)
21
-
20
+ - [Initial considerations for this library](https://github.com/pixiebrix/webext-messenger/issues/1)
22
21
 
23
22
  ## npm publishing
24
23