webext-messenger 0.27.0 → 0.29.0-0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -27,19 +28,21 @@ import { MessengerError, once } from "./shared.js";
27
28
  // If a message is received before this is ready, it will just have to be ignored.
28
29
  export const thisTarget = isBackground()
29
30
  ? { page: "background" }
30
- : {
31
- get page() {
32
- // Extension pages have relative URLs to simplify comparison
33
- const origin = location.protocol.startsWith("http")
34
- ? location.origin
35
- : "";
36
- // Don't use the hash
37
- return origin + location.pathname + location.search;
38
- },
39
- };
31
+ : (isOffscreenDocument()
32
+ ? { page: "offscreen" }
33
+ : {
34
+ get page() {
35
+ // Extension pages have relative URLs to simplify comparison
36
+ const origin = location.protocol.startsWith("http")
37
+ ? location.origin
38
+ : "";
39
+ // Don't use the hash
40
+ return origin + location.pathname + location.search;
41
+ },
42
+ });
40
43
  let tabDataStatus =
41
44
  // The background page doesn't have a tab
42
- isBackground() ? "not-needed" : "needed";
45
+ isBackground() || isOffscreenDocument() ? "not-needed" : "needed";
43
46
  export function getTabDataStatus() {
44
47
  return tabDataStatus;
45
48
  }
@@ -47,6 +50,11 @@ const storeTabData = once(async () => {
47
50
  if (tabDataStatus !== "needed") {
48
51
  return;
49
52
  }
53
+ // If the page is prerendering, wait for the change to be able to get the tab data so the frameId is correct
54
+ // https://developer.mozilla.org/en-US/docs/Web/API/Document/prerenderingchange_event
55
+ if ("prerendering" in document && Boolean(document.prerendering)) {
56
+ await pEvent(document, 'prerenderingchange');
57
+ }
50
58
  try {
51
59
  tabDataStatus = "pending";
52
60
  Object.assign(thisTarget, await messenger("__getTabData", {}, { page: "any" }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.27.0",
3
+ "version": "0.29.0-0",
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-event": "^6.0.1",
29
30
  "p-retry": "^6.2.0",
30
31
  "serialize-error": "^11.0.3",
31
- "type-fest": "^4.13.0",
32
- "webext-detect-page": "^5.0.1"
32
+ "type-fest": "^4.26.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
+ "@sindresorhus/tsconfig": "^6.0.0",
42
+ "@types/chrome": "^0.0.279",
41
43
  "@types/tape": "^5.6.4",
42
- "@types/webextension-polyfill": "^0.10.7",
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.40.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.6.3",
56
+ "vitest": "^2.1.4",
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,7 +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)
20
+ - [Initial considerations for this library](https://github.com/pixiebrix/webext-messenger/issues/1)
21
21
 
22
22
 
23
23
  ## npm publishing