Update your code

Updates that are unrelated to other issues

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" permission.
  • Either host permissions or the "activeTab" permission.

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

  • While the old method could only take a single file, the new method can take an array of files.
  • You also pass a ScriptInjection object instead of InjectDetails. There are multiple differences between the two. For example, the tabId is now passed as a member of ScriptInjection.target instead of as a method argument.

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 "scripting" permission.
  • Either host permissions or the "activeTab" permission.

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

  • When calling these methods, you pass a CSSInjection object instead of InjectDetails.
  • The tabId is now passed as a member of CSSInjection.target instead of as a method argument.

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:

  • chrome.runtime.getBackgroundPage()
  • chrome.extension.getBackgroundPage()
  • chrome.extension.getExtensionTabs()

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