webext-messenger 0.28.0 → 0.29.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.
@@ -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,4 +1,4 @@
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";
@@ -26,9 +26,12 @@ import { pEvent } from "p-event";
26
26
  // Soft warning: Race conditions are possible.
27
27
  // This CANNOT be awaited because waiting for it means "I will handle the message."
28
28
  // If a message is received before this is ready, it will just have to be ignored.
29
- export const thisTarget = isBackground()
30
- ? { page: "background" }
31
- : {
29
+ export const thisTarget = (() => {
30
+ if (isBackground())
31
+ return { page: "background" };
32
+ if (isOffscreenDocument())
33
+ return { page: "offscreen" };
34
+ return {
32
35
  get page() {
33
36
  // Extension pages have relative URLs to simplify comparison
34
37
  const origin = location.protocol.startsWith("http")
@@ -38,9 +41,10 @@ export const thisTarget = isBackground()
38
41
  return origin + location.pathname + location.search;
39
42
  },
40
43
  };
44
+ })();
41
45
  let tabDataStatus =
42
- // The background page doesn't have a tab
43
- isBackground() ? "not-needed" : "needed";
46
+ // Exclude contexts that don't have a tab associated to them
47
+ isBackground() || isOffscreenDocument() ? "not-needed" : "needed";
44
48
  export function getTabDataStatus() {
45
49
  return tabDataStatus;
46
50
  }
@@ -51,7 +55,7 @@ const storeTabData = once(async () => {
51
55
  // If the page is prerendering, wait for the change to be able to get the tab data so the frameId is correct
52
56
  // https://developer.mozilla.org/en-US/docs/Web/API/Document/prerenderingchange_event
53
57
  if ("prerendering" in document && Boolean(document.prerendering)) {
54
- await pEvent(document, 'prerenderingchange');
58
+ await pEvent(document, "prerenderingchange");
55
59
  }
56
60
  try {
57
61
  tabDataStatus = "pending";
@@ -73,8 +77,7 @@ export async function getThisFrame() {
73
77
  if (typeof tabId !== "number" || typeof frameId !== "number") {
74
78
  let moreInfo = "(error retrieving context information)";
75
79
  try {
76
- moreInfo = `(context: ${getContextName()}, url: ${globalThis.location
77
- ?.href})`;
80
+ moreInfo = `(context: ${getContextName()}, url: ${globalThis.location?.href})`;
78
81
  }
79
82
  catch { }
80
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.28.0",
3
+ "version": "0.29.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -29,31 +29,32 @@
29
29
  "p-event": "^6.0.1",
30
30
  "p-retry": "^6.2.0",
31
31
  "serialize-error": "^11.0.3",
32
- "type-fest": "^4.18.3",
33
- "webext-detect-page": "^5.0.1"
32
+ "type-fest": "^4.26.1",
33
+ "webext-detect": "^5.3.1",
34
+ "webext-events": "^3.1.1"
34
35
  },
35
36
  "@parcel/resolver-default": {
36
37
  "packageExports": true
37
38
  },
38
39
  "devDependencies": {
39
40
  "@parcel/config-webextension": "^2.11.0",
40
- "@sindresorhus/tsconfig": "^5.0.0",
41
- "@types/chrome": "^0.0.268",
41
+ "@sindresorhus/tsconfig": "^6.0.0",
42
+ "@types/chrome": "^0.0.279",
42
43
  "@types/tape": "^5.6.4",
43
- "@types/webextension-polyfill": "^0.10.7",
44
+ "@types/webextension-polyfill": "^0.12.1",
44
45
  "buffer": "^6.0.3",
45
46
  "eslint": "^8.57.0",
46
- "eslint-config-pixiebrix": "^0.39.0",
47
+ "eslint-config-pixiebrix": "^0.40.1",
47
48
  "events": "^3.3.0",
48
49
  "npm-run-all": "^4.1.5",
49
50
  "parcel": "^2.11.0",
50
51
  "path-browserify": "^1.0.1",
51
52
  "process": "^0.11.10",
52
53
  "stream-browserify": "^3.0.0",
53
- "tape": "^5.7.5",
54
- "typescript": "^5.4.5",
55
- "vitest": "^1.6.0",
56
- "webext-content-scripts": "^2.6.1",
54
+ "tape": "^5.9.0",
55
+ "typescript": "^5.6.3",
56
+ "vitest": "^2.1.4",
57
+ "webext-content-scripts": "^2.7.0",
57
58
  "webextension-polyfill": "^0.12.0"
58
59
  },
59
60
  "targets": {
@@ -73,7 +74,8 @@
73
74
  "run": {
74
75
  "startUrl": [
75
76
  "https://fregante.github.io/pixiebrix-testing-ground/Will-call-background-methods",
76
- "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"
77
79
  ]
78
80
  }
79
81
  }
package/readme.md CHANGED
@@ -19,7 +19,6 @@ import messenger from "webext-messenger";
19
19
 
20
20
  - [Initial considerations for this library](https://github.com/pixiebrix/webext-messenger/issues/1)
21
21
 
22
-
23
22
  ## npm publishing
24
23
 
25
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)