Skip to content

Conversation

madaster97
Copy link

@madaster97 madaster97 commented Jul 27, 2025

In the documentation for maintaining an offscreen document, the recommendation is to do the heavy lifting in the parameters that can be passed to getContexts:

const existingContexts = await chrome.runtime.getContexts({
  contextTypes: ['OFFSCREEN_DOCUMENT'],
  documentUrls: [offscreenUrl]
});

But in some of the functional examples showing offscreen documents, we don't follow that recommendation:

const matchedClients = await clients.matchAll();
for (const client of matchedClients) {
  if (client.url.endsWith(OFFSCREEN_DOCUMENT_PATH)) {
    return true;
  }
}

In this PR, I've made the above edit to the offscreen-dom cookbook, along with 1 other edit: I've removed the check for document that we do before trying to close the document. Instead, I've just put in a warning if our attempt to close the document fails:

chrome.offscreen.closeDocument().catch(error => {
  console.warn(
    'Received an error while attempting to close an offscreen document, after receiving a message assumed to come from an offscreen document: %o', error)
});

Does this look OK? Should we add it to the other 2 offscreen examples? (clipboard-write and geolocation - nvm already there!)

Copy link
Member

@oliverdunk oliverdunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great - thanks for the contribution! Can you run eslint to fix the linter errors identified by CI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants