-
Notifications
You must be signed in to change notification settings - Fork 71
Open
Labels
needs-triage: chromeChrome needs to assess this issue for the first timeChrome needs to assess this issue for the first time
Description
Motivation
Current implementation of the offscreen API is complicated and requires many steps, often only to perform a simple DOM operation.
Solution
Browser handled offscreen document:
- created by browser when needed
- destroyed after usage or together with the service worker (if
funcisasyncand won't resolve in time)
Example
Let's say we want to implement a function that fetches a page title:
async function fetchPageTitle(url: string) {
const pageHtml = await (await fetch(url)).text();
const pageTitle = await chrome.offscreen.executeScript({
args: [pageHtml],
func: (pageHtml) => {
const parser = new DOMParser();
const doc = parser.parseFromString(pageHtml, 'text/html');
return doc.title; // returning a Promise should be also supported!
},
});
return pageTitle;
}Why is this better
- it's a familiar API (see scripting.executeScript)
- the code execution flow is straightforward - easy to reason about
- the code logic is nicely encapsulated inside a single function (not across files/message handlers)
- potentially better cross browser compatibility - one could easily write a polyfill for Firefox event page that simply executes the provided function in the current event page
What's missing / not clear
I'm not sure about the "reasons" parameter, do we need that?
See also
Chromium issue for this proposal.
tophf
Metadata
Metadata
Assignees
Labels
needs-triage: chromeChrome needs to assess this issue for the first timeChrome needs to assess this issue for the first time