A RetroSearch Logo

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

Search Query:

Showing content from https://developer.chrome.com/docs/extensions/develop/migrate/api-calls below:

Update your code | Chrome Extensions

Update your code

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

Updates that are unrelated to other issues

Note: Manifest V3 is supported generally in Chrome 88 or later. For extension features added in later Chrome versions, see the API reference documentation for support information. If your extension requires a specific API, you can specify a minimum chrome version in the manifest file.

This is the first of three sections describing changes needed for code that is not part of the extension service worker. This section is for required code changes that are unrelated to other issues. The next two sections cover replacing blocking web requests and improving security.

Replace tabs.executeScript() with scripting.executeScript()

In Manifest V3, executeScript() moves from the tabs API to the scripting API. This requires changes to permissions in the manifest file in addition to actual code changes.

For the executeScript() method you need:

The scripting.executeScript() method is similar to how it worked with tabs.executeScript(). There are a few differences.

The example shows how to do this.

Manifest V2

async function getCurrentTab() {/* ... */}
let tab = await getCurrentTab();

chrome.tabs.executeScript(
  tab.id,
  {
    file: 'content-script.js'
  }
);

In a background script file.

Manifest V3

async function getCurrentTab()
let tab = await getCurrentTab();

chrome.scripting.executeScript({
  target: {tabId: tab.id},
  files: ['content-script.js']
});

In the extension service worker.

Replace tabs.insertCSS() and tabs.removeCSS() with scripting.insertCSS() and scripting.removeCSS()

In Manifest V3, insertCSS() and removeCSS() move from the tabs API to the scripting API. This requires changes to permissions in the manifest file in addition to code changes:

The functions on the scripting API are similar to the functions on tabs. There are a few differences.

The example shows how to do this for insertCSS(). The procedure for removeCSS() is the same.

Manifest V2

chrome.tabs.insertCSS(tabId, injectDetails, () => {
  // callback code
});

In a background script file.

Manifest V3

const insertPromise = await chrome.scripting.insertCSS({
  files: ["style.css"],
  target: { tabId: tab.id }
});
// Remaining code. 

In the extension service worker.

Replace Browser Actions and Page Actions with Actions

Browser actions and page actions were separate concepts in Manifest V2. Though they started with distinct roles, the differences between them decreased over time. In Manifest V3, these concepts are consolidated into the Action API. This requires changes in your manifest.json and extension code that is different from what you would have put in your Manifest V2 background script.

Actions in Manifest V3 most closely resemble browser actions; however, the action API does not provide hide() and show() as pageAction did. If you still need page actions, you can either emulate them using declarative content or call enable() or disable() with a tab ID.

Replace "browser_action" and "page_action" with "action"

In the manifest.json replace the "browser_action" and "page_action" fields with the "action" field. Consult the reference for information on the "action" field.

Manifest V2

{
  ...
  "page_action": { ... },
  "browser_action": {
    "default_popup": "popup.html"
   }
  ...
}

Manifest V3

{
  ...
  "action": {
    "default_popup": "popup.html"
  }

  ...
}
Replace the browserAction and pageAction APIs with the action API

Where your Manifest V2 used the browserAction and pageAction APIs, you should now use the action API.

Manifest V2

chrome.browserAction.onClicked.addListener(tab => { ... });
chrome.pageAction.onClicked.addListener(tab => { ... });

Manifest V3

chrome.action.onClicked.addListener(tab => { ... });
Replace callbacks with promises

In Manifest V3, many extension API methods return promises. A Promise is a proxy or placeholder for a value returned by an asynchronous method. If you've never used Promises, you can read about them on MDN. This page describes what you need to know to use them in a Chrome extension.

For backward compatibility, many methods continue to support callbacks after promise support is added. Be aware that you cannot use both on the same function call. If you pass a callback, the function will not return a promise and if you want a promise returned do not pass a callback. Some API features, such as event listeners, will continue to require callbacks. To check whether a method supports promises, look for the "Promise" label in its API reference.

To convert from a callback to a promise, remove the callback and handle the returned promise. The example below is taken from the optional permissions sample, newtab.js specifically. The callback version shows what the sample's call to request() would look like with a callback. Note that the promise version could be rewritten with async and await.

Callback

chrome.permissions.request(newPerms, (granted) => {
  if (granted) {
    console.log('granted');
  } else {
    console.log('not granted');
  }
});

Promise

const newPerms = { permissions: ['topSites'] };
chrome.permissions.request(newPerms)
.then((granted) => {
  if (granted) {
    console.log('granted');
  } else {
    console.log('not granted');
  }
});
Replace functions that expect a Manifest V2 background context

Other extension contexts can only interact with extension service workers using message passing. Consequently, you'll need to replace calls that expect a background context, specifically:

Your extension scripts should use message passing to communicate between a service worker and other parts of your extension. Currently this can be accomplished by using sendMessage() and implementing chrome.runtime.onMessage in your extension service worker. Long term, you should plan to replace these calls with postMessage() and a service worker's message event handler.

Replace unsupported APIs

The methods and properties listed below need to change in Manifest V3.

Manifest V2 method or property Replace with chrome.extension.connect() chrome.runtime.connect() chrome.extension.connectNative() chrome.runtime.connectNative() chrome.extension.getExtensionTabs() chrome.extension.getViews() chrome.extension.getURL() chrome.runtime.getURL() chrome.extension.lastError Where methods return promises, use promise.catch() chrome.extension.onConnect chrome.runtime.onConnect chrome.extension.onConnectExternal chrome.runtime.onConnectExternal chrome.extension.onMessage chrome.runtime.onMessage chrome.extension.onRequest chrome.runtime.onMessage chrome.extension.onRequestExternal chrome.runtime.onMessageExternal chrome.extension.sendMessage() chrome.runtime.sendMessage() chrome.extension.sendNativeMessage() chrome.runtime.sendNativeMessage() chrome.extension.sendRequest() chrome.runtime.sendMessage() chrome.runtime.onSuspend (background scripts) Not supported in extension service workers. Use the beforeunload document event instead. chrome.tabs.getAllInWindow() chrome.tabs.query() chrome.tabs.getSelected() chrome.tabs.query() chrome.tabs.onActiveChanged chrome.tabs.onActivated chrome.tabs.onHighlightChanged chrome.tabs.onHighlighted chrome.tabs.onSelectionChanged chrome.tabs.onActivated chrome.tabs.sendRequest() chrome.runtime.sendMessage() chrome.tabs.Tab.selected chrome.tabs.Tab.highlighted

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 2023-03-09 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 2023-03-09 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