diff --git a/.eslintrc b/.eslintrc index 7dd2ffe..7ef4a4c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -50,6 +50,7 @@ } } ], + "unicorn/no-nested-ternary": "off", // Prettier conflict "@typescript-eslint/consistent-type-definitions": "off" // Unconvinced by its utility; may be counterproductive }, "overrides": [ diff --git a/package.json b/package.json index 832ef2c..5669c48 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,8 @@ "run": { "startUrl": [ "https://fregante.github.io/pixiebrix-testing-ground/Will-call-background-methods", - "https://fregante.github.io/pixiebrix-testing-ground/Will-call-other-CS-via-background" + "https://fregante.github.io/pixiebrix-testing-ground/Will-call-other-CS-via-background", + "https://fregante.github.io/pixiebrix-testing-ground/Will-call-offscreen-methods" ] } } diff --git a/source/test/background.ts b/source/test/background.ts index 1efc4fc..1233cb6 100644 --- a/source/test/background.ts +++ b/source/test/background.ts @@ -1,3 +1,21 @@ import "./webextensionPolyfill.js"; import "./background/registration.ts"; import "./contentscript/api.test.ts"; + +async function init() { + try { + await chrome.offscreen.createDocument({ + url: "offscreen.html", + // @ts-expect-error wrong? + reasons: ["DOM_PARSER"], + justification: "testing", + }); + } catch (error) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any + if (!(error as any).message.includes("Only a single offscreen")) { + throw error; + } + } +} + +void init(); diff --git a/source/test/contentscript/api.test.ts b/source/test/contentscript/api.test.ts index 2e55137..62d7320 100644 --- a/source/test/contentscript/api.test.ts +++ b/source/test/contentscript/api.test.ts @@ -1,6 +1,6 @@ import test from "tape"; import { isBackground, isContentScript, isWebPage } from "webext-detect"; -import { type PageTarget, type Sender, type Target } from "webext-messenger"; +import { type PageTarget, type Target } from "webext-messenger"; import { errorTabDoesntExist, errorTargetClosedEarly, @@ -12,6 +12,8 @@ import { sleep, trackSettleTime, expectDuration, + senderIsBackground, + senderIsCurrentPage, } from "../helpers.js"; import * as backgroundContext from "../background/api.js"; import * as localContext from "../background/testingApi.js"; @@ -30,31 +32,6 @@ import { } from "./api.js"; import { MessengerError } from "../../shared.js"; -const extensionUrl = new URL(chrome.runtime.getURL("")); - -function senderIsCurrentPage( - t: test.Test, - sender: Sender | undefined, - message: string, -) { - t.equal(sender?.url, location.href, message); -} - -function senderisBackground( - t: test.Test, - sender: Sender | undefined, - message: string, -) { - /* eslint-disable @typescript-eslint/prefer-nullish-coalescing -- It's an OR on falsy values */ - t.true( - sender?.origin === extensionUrl.origin || // Chrome - sender?.origin === "null" || // Chrome, old - sender?.url?.includes("/background.") || - sender?.url?.endsWith("/_generated_background_page.html"), // Firefox - message, - ); -} - const { openTab, createTargets, ensureScripts, closeTab } = isBackground() ? localContext : backgroundContext; @@ -149,9 +126,9 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) { const directSender = trace.at(-1); if (isBackground()) { - senderisBackground( + senderIsBackground( t, - directSender, + originalSender, "Messages should mention the current page (background) in trace[0]", ); } else { @@ -163,7 +140,7 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) { } if (!("page" in target && isContentScript())) { - senderisBackground( + senderIsBackground( t, directSender, "Messages originated in content scripts or background pages must come directly from the background page", diff --git a/source/test/helpers.ts b/source/test/helpers.ts index 0f5a69d..e4d2da8 100644 --- a/source/test/helpers.ts +++ b/source/test/helpers.ts @@ -1,4 +1,5 @@ import type test from "tape"; +import { type Sender } from "webext-messenger"; export async function expectRejection( t: test.Test, @@ -65,3 +66,28 @@ export function expectDuration( ); } } + +const extensionUrl = new URL(chrome.runtime.getURL("")); + +export function senderIsCurrentPage( + t: test.Test, + sender: Sender | undefined, + message: string, +) { + t.equal(sender?.url, location.href, message); +} + +export function senderIsBackground( + t: test.Test, + sender: Sender | undefined, + message: string, +) { + /* eslint-disable @typescript-eslint/prefer-nullish-coalescing -- It's an OR on falsy values */ + t.true( + sender?.origin === extensionUrl.origin || // Chrome + sender?.origin === "null" || // Chrome, old + sender?.url?.includes("/background.") || + sender?.url?.endsWith("/_generated_background_page.html"), // Firefox + message, + ); +} diff --git a/source/test/manifest.json b/source/test/manifest.json index 31b197c..fed14c1 100644 --- a/source/test/manifest.json +++ b/source/test/manifest.json @@ -3,7 +3,7 @@ "name": "webext-messenger", "version": "0.0.0", "manifest_version": 3, - "permissions": ["webNavigation", "scripting", "storage"], + "permissions": ["webNavigation", "scripting", "storage", "offscreen"], "host_permissions": ["https://fregante.github.io/*"], "background": { "type": "module", @@ -21,6 +21,12 @@ ], "js": ["webextensionPolyfill.ts", "background/api.test.ts"] }, + { + "matches": [ + "https://fregante.github.io/pixiebrix-testing-ground/Will-call-offscreen-methods" + ], + "js": ["webextensionPolyfill.ts", "offscreen/api.test.ts"] + }, { "all_frames": true, "matches": [ diff --git a/source/test/offscreen.html b/source/test/offscreen.html new file mode 100644 index 0000000..4281c34 --- /dev/null +++ b/source/test/offscreen.html @@ -0,0 +1,5 @@ + + +