-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add offscreen support #276
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c1fb70
Upgrade testing extension to MV3
fregante 7fcb705
Lint
fregante d79889c
Start offscreen
fregante 672e856
Finalize offscreen
fregante 1af5282
Merge branch 'main' into offscreen
fregante ec40e28
Prettier
fregante 465b7a0
Silence `offscreen.createDocument`
fregante 1c21875
Lint
fregante be8bbb2
PR review
fregante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!doctype html> | ||
<meta charset="UTF-8" /> | ||
<title>Offscreen page</title> | ||
<script type="module" src="webextensionPolyfill.ts"></script> | ||
<script type="module" src="offscreen/registration.ts"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function addFrame(): void { | ||
const frame = document.createElement("iframe"); | ||
frame.src = "https://example.com"; | ||
document.body.append(frame); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import test from "tape"; | ||
import { getLocation, addFrame, getTrace } from "./api.js"; | ||
import { senderIsCurrentPage } from "../helpers.js"; | ||
|
||
test("should get a value from the offscreen document", async (t) => { | ||
t.equal(await getLocation(), chrome.runtime.getURL("offscreen.html")); | ||
}); | ||
|
||
test("notification should return undefined", async (t) => { | ||
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression -- Testing for this specifically | ||
t.equals(addFrame(), undefined); | ||
}); | ||
|
||
test("should receive trace", async (t) => { | ||
const trace = await getTrace(); | ||
t.true(Array.isArray(trace)); | ||
|
||
const originalSender = trace[0]; | ||
|
||
senderIsCurrentPage( | ||
t, | ||
originalSender, | ||
"Messages should mention the current page in trace[0]", | ||
); | ||
|
||
t.equal( | ||
trace.length, | ||
1, | ||
"The offscreen page can and should only be messaged directly", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { getNotifier, getMethod } from "webext-messenger"; | ||
|
||
const target = { page: "offscreen" }; | ||
|
||
export const getLocation = getMethod("getLocation", target); | ||
export const addFrame = getNotifier("addFrame", target); | ||
export const getTrace = getMethod("getTrace", target); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getLocation(): string { | ||
return location.href; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { type MessengerMeta, type Sender } from "webext-messenger"; | ||
|
||
export async function getTrace(this: MessengerMeta): Promise<Sender[]> { | ||
return this.trace; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { isOffscreenDocument } from "webext-detect"; | ||
import { registerMethods } from "webext-messenger"; | ||
|
||
import { addFrame } from "./addFrame.js"; | ||
import { getLocation } from "./getLocation.js"; | ||
import { getTrace } from "./getTrace.js"; | ||
|
||
declare global { | ||
interface MessengerMethods { | ||
addFrame: typeof addFrame; | ||
getLocation: typeof getLocation; | ||
getTrace: typeof getTrace; | ||
} | ||
} | ||
|
||
if (!isOffscreenDocument()) { | ||
throw new Error( | ||
"This file must only be run in the offscreen document, which is the receiving end", | ||
); | ||
} | ||
|
||
registerMethods({ | ||
addFrame, | ||
getLocation, | ||
getTrace, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: update comment