-
-
Notifications
You must be signed in to change notification settings - Fork 368
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
[RFC] Support for Offscreen Documents #527
Comments
Is there a workaround to implement this in Plasmo now? |
@dimadolgopolovn yeah you can just use it as-is. All low-level chrome API are available at your disposal. |
Adding the solution here so that people don't waste time on it. Add offscreen.html and offscreen.js to project root as per Chrome documentation. offscreen.html: <!DOCTYPE html>
<script src="offscreen.js"></script> background.js: import OFFSCREEN_DOCUMENT_PATH from 'url:~offscreen.html'
async function createOffscreenDocument() {
if (!(await hasDocument())) {
await chrome.offscreen.createDocument({
url: OFFSCREEN_DOCUMENT_PATH,
reasons: [chrome.offscreen.Reason.WEB_RTC],
justification: 'P2P data transfer'
}).then(e => {
// Now that we have an offscreen document, we can dispatch the
// message.
})
}
}
createOffscreenDocument()
async function hasDocument() {
// Check all windows controlled by the service worker if one of them is the offscreen document
// @ts-ignore clients
const matchedClients = await clients.matchAll()
for (const client of matchedClients) {
if (client.url.endsWith(OFFSCREEN_DOCUMENT_PATH)) {
return true
}
}
return false
} |
@dimadolgopolovn Thank you for the solution! I struggled to connect the html file to the document and that url import did the trick!
It seems I can also use typescript file as a script. <script src="offscreen.ts" type="module"></script> |
@jkkrow @dimadolgopolovn Plasmo doesn't seem to pick up the files when compiling how did you add this to the project. |
@anooppoommen, same as with adding a background.ts. You put it in root and you just need to add “export {}” as the first line. |
@dimadolgopolovn I tried creating an |
You should include "offscreen" to permissions in the package.json. {
"permissions": [
"offscreen",
]
} |
@jkkrow added the permission too but the file doesn't appear in the final build did you add it to the tsconfig or anywhere to let Plasmo pick it up |
I didn't set any configuration regarding this. I only put offscreen.ts and offscreen.html in the src directory. This is my tsconfig.json: {
"extends": "plasmo/templates/tsconfig.base",
"exclude": [
"node_modules"
],
"include": [
".plasmo/index.d.ts",
"./**/*.ts",
"./**/*.tsx",
],
"compilerOptions": {
"paths": {
"~*": [
"./src/*"
]
},
"baseUrl": ".",
"strictNullChecks": true,
"ignoreDeprecations": "5.0"
},
} This is where I use offscreen in message handler: import type { PlasmoMessaging } from '@plasmohq/messaging';
import OFFSCREEN_DOCUMENT_PATH from 'url:~src/offscreen.html';
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
if (!(await chrome.offscreen.hasDocument())) {
await chrome.offscreen.createDocument({
url: OFFSCREEN_DOCUMENT_PATH,
reasons: [chrome.offscreen.Reason.BLOBS],
justification: 'Testing',
});
}
res.send('');
};
export default handler; |
@jkkrow thanks for the example it's really helpful. the issue for me seems to be the tsconfig in my project. |
adding the sample
|
What should we abstract out for this API? Would love for folks who've played around with this API to chime in. Should we design it like this:
The above then get compiled into a To abstract out the html, you can use the tab-page feature: https://docs.plasmo.com/framework/tab-pages Make a file in await chrome.offscreen.createDocument({
url: chrome.runtime.getURL("tabs/offscreen.html"),
reasons: [chrome.offscreen.Reason.BLOBS],
justification: 'Testing',
}); |
I think creating the offscreen document manually is better for more versatile usage. Let's say if someone needs offscreen documents for different reasons. If the config is bound to In most cases it won't matter, but this option decides different lifespan of offscreen. According to this,
So I think it would be better to let developers create the offscreen manually, but with abstracted function (Such as checking the existing document before create one). For example,
Or maybe abstract this further so that document is created only when user request an operation to it. For example,
|
I added the three files below, and offscreen permission to my manifest/package.json and it worked. I am creating this post to compile all the code for a fully working example in one place. The code is primarily from @dimadolgopolovn and @anooppoommen. Thank you guys! All of these files are should be placed at the root of your project. In my case that is in the src/ directory. Note: You can move the background.ts file into a background folder if your prefer. In which case it would look like this background/index.ts. packages.json
background.ts
offscreen.ts offscreen.html
|
On chromium 128.0.6*** this method raises a Content Security Policy error. Any thoughts about how to implement it? |
How do you envision this feature/change to look/work like?
The Offscreen API has been added since Chrome 109 version. This solves a problem with the service worker which can't access the DOM or the window interface. It would be great if this feature is natively supported in Plasmo.
As quoted in this link, Manifest V3 extensions are service worker-based, but service workers don't provide support for the same APIs and mechanisms that full document-based pages (which include background and event pages) do. Additionally, using content scripts to access DOM APIs on web pages leaves the extension at the mercy of different content security policies on a page-to-page basis.
Offscreen Documents supports DOM-related features and APIs by allowing Manifest V3 extensions to open minimal, scoped, and relatively un-permissioned offscreen documents at runtime through a dedicated API.
What is the purpose of this change/feature? Why?
The Offscreen API requires a dedicated html page just like other extension pages, so currently it seems impossible to implement this within the Plasmo environment using React and Typescript which automatically bundles the tsx files into html and js.
(OPTIONAL) Example implementations
Like other extension pages or background sw, automatically building an offscreen environment by creating a file named
offscreen.ts
in a root directory or src folder looks like an ideal implementation.(OPTIONAL) Contact Details
No response
Verify canary release
plasmo
canary releaseCode of Conduct
The text was updated successfully, but these errors were encountered: