A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.chrome.com/docs/extensions/reference/api/offscreen below:

chrome.offscreen | API | Chrome for Developers

chrome.offscreen

Stay organized with collections Save and categorize content based on your preferences.

Description

Use the offscreen API to create and manage offscreen documents.

Permissions

offscreen

To use the Offscreen API, declare the "offscreen" permission in the extension manifest. For example:

{
  "name": "My extension",
  ...
  "permissions": [
    "offscreen"
  ],
  ...
}
Availability Concepts and usage

Service workers don't have DOM access, and many websites have content security policies that limit the functionality of content scripts. The Offscreen API allows the extension to use DOM APIs in a hidden document without interrupting the user experience by opening new windows or tabs. The runtime API is the only extensions API supported by offscreen documents.

Pages loaded as offscreen documents are handled differently from other types of extension pages. The extension's permissions carry over to offscreen documents, but with limits on extension API access. For example, because the chrome.runtime API is the only extensions API supported by offscreen documents, messaging must be handled using members of that API.

The following are other ways offscreen documents behave differently from normal pages:

Use chrome.offscreen.createDocument() and chrome.offscreen.closeDocument() to create and close an offscreen document. createDocument() requires the document's url, a reason, and a justification:

chrome.offscreen.createDocument({
  url: 'off_screen.html',
  reasons: ['CLIPBOARD'],
  justification: 'reason for needing the document',
});
Reasons

For a list of valid reasons, see the Reasons section. Reasons are set during document creation to determine the document's lifespan. The AUDIO_PLAYBACK reason sets the document to close after 30 seconds without audio playing. All other reasons don't set lifetime limits.

Examples Maintain the lifecycle of an offscreen document

The following example shows how to ensure that an offscreen document exists. The setupOffscreenDocument() function calls runtime.getContexts() to find an existing offscreen document, or creates the document if it doesn't already exist.

let creating; // A global promise to avoid concurrency issues
async function setupOffscreenDocument(path) {
  // Check all windows controlled by the service worker to see if one
  // of them is the offscreen document with the given path
  const offscreenUrl = chrome.runtime.getURL(path);
  const existingContexts = await chrome.runtime.getContexts({
    contextTypes: ['OFFSCREEN_DOCUMENT'],
    documentUrls: [offscreenUrl]
  });

  if (existingContexts.length > 0) {
    return;
  }

  // create offscreen document
  if (creating) {
    await creating;
  } else {
    creating = chrome.offscreen.createDocument({
      url: path,
      reasons: ['CLIPBOARD'],
      justification: 'reason for needing the document',
    });
    await creating;
    creating = null;
  }
}

Before sending a message to an offscreen document, call setupOffscreenDocument() to make sure the document exists, as demonstrated in the following example.

chrome.action.onClicked.addListener(async () => {
  await setupOffscreenDocument('off_screen.html');

  // Send message to offscreen document
  chrome.runtime.sendMessage({
    type: '...',
    target: 'offscreen',
    data: '...'
  });
});

For complete examples, see the offscreen-clipboard and offscreen-dom demos on GitHub.

Before Chrome 116: check if an offscreen document is open

runtime.getContexts() was added in Chrome 116. In earlier versions of Chrome, use clients.matchAll() to check for an existing offscreen document:

async function hasOffscreenDocument() {
  if ('getContexts' in chrome.runtime) {
    const contexts = await chrome.runtime.getContexts({
      contextTypes: ['OFFSCREEN_DOCUMENT'],
      documentUrls: [OFFSCREEN_DOCUMENT_PATH]
    });
    return Boolean(contexts.length);
  } else {
    const matchedClients = await clients.matchAll();
    return matchedClients.some(client => {
      return client.url.includes(chrome.runtime.id);
    });
  }
}
Types Properties Enum

"TESTING"
A reason used for testing purposes only.

"AUDIO_PLAYBACK"
Specifies that the offscreen document is responsible for playing audio.

"IFRAME_SCRIPTING"
Specifies that the offscreen document needs to embed and script an iframe in order to modify the iframe's content.

"DOM_SCRAPING"
Specifies that the offscreen document needs to embed an iframe and scrape its DOM to extract information.

"BLOBS"
Specifies that the offscreen document needs to interact with Blob objects (including URL.createObjectURL()).

"DOM_PARSER"
Specifies that the offscreen document needs to use the DOMParser API.

"USER_MEDIA"
Specifies that the offscreen document needs to interact with media streams from user media (e.g. getUserMedia()).

"DISPLAY_MEDIA"
Specifies that the offscreen document needs to interact with media streams from display media (e.g. getDisplayMedia()).

"WEB_RTC"
Specifies that the offscreen document needs to use WebRTC APIs.

"CLIPBOARD"
Specifies that the offscreen document needs to interact with the Clipboard API.

"LOCAL_STORAGE"
Specifies that the offscreen document needs access to localStorage.

"WORKERS"
Specifies that the offscreen document needs to spawn workers.

"BATTERY_STATUS"
Specifies that the offscreen document needs to use navigator.getBattery.

"MATCH_MEDIA"
Specifies that the offscreen document needs to use window.matchMedia.

"GEOLOCATION"
Specifies that the offscreen document needs to use navigator.geolocation.

Methods

closeDocument()

chrome.offscreen.closeDocument(): Promise<void>

Closes the currently-open offscreen document for the extension.

createDocument()

chrome.offscreen.createDocument(
  parameters: CreateParameters,
)
: Promise<void>

Creates a new offscreen document for the extension.

Parameters

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-11 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-11 UTC."],[],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4