Updates that are unrelated to other issues

> [!NOTE]
> **Note:** Manifest V3 is supported generally in Chrome 88 or later. For extension features added in later Chrome versions, see the [API reference documentation](https://developer.chrome.com/docs/extensions/reference) for support information. If your extension requires a specific API, you can specify [a minimum chrome version](https://developer.chrome.com/docs/extensions/mv3/manifest/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](https://developer.chrome.com/docs/extensions/develop/migrate/blocking-web-requests.md.txt) and [improving security](https://developer.chrome.com/docs/extensions/develop/migrate/improve-security.md.txt).

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

In Manifest V3, `executeScript()` moves from the `tabs` API to the [`scripting`](https://developer.chrome.com/docs/extensions/reference/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`](https://developer.chrome.com/docs/extensions/reference/scripting#type-ScriptInjection) object instead of [`InjectDetails`](https://developer.chrome.com/docs/extensions/reference/extensionTypes#type-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

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

browser.tabs.executeScript(
  tab.id,
  {
    file: 'content-script.js'
  }
);
```
In a background script file.
Manifest V3

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

browser.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](https://developer.chrome.com/docs/extensions/reference/scripting). 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`](https://developer.chrome.com/docs/extensions/reference/scripting#type-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

```javascript
browser.tabs.insertCSS(tabId, injectDetails, () => {
  // callback code
});
```
In a background script file.
Manifest V3

```javascript
const insertPromise = await browser.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](https://developer.chrome.com/docs/extensions/reference/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](https://developer.chrome.com/docs/extensions/reference/action#emulating-pageactions-with-declarativecontent) 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](https://developer.chrome.com/docs/extensions/reference/action).
Manifest V2

```json
{
  ...
  "page_action": { ... },
  "browser_action": {
    "default_popup": "popup.html"
   }
  ...
}
```
Manifest V3

```json
{
  ...
  "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

```javascript
browser.browserAction.onClicked.addListener(tab => { ... });
browser.pageAction.onClicked.addListener(tab => { ... });
```
Manifest V3

```javascript
browser.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](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Using_promises). 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](https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/sample.optional_permissions), `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

```javascript
browser.permissions.request(newPerms, (granted) => {
  if (granted) {
    console.log('granted');
  } else {
    console.log('not granted');
  }
});
```
Promise

```javascript
const newPerms = { permissions: ['topSites'] };
browser.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](https://developer.chrome.com/docs/extensions/mv3/messaging). Consequently, you'll need to replace calls that expect a background context, specifically:

- `browser.runtime.getBackgroundPage()`
- `browser.extension.getBackgroundPage()`
- `browser.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 `browser.runtime.onMessage` in your extension service worker. Long term, you should plan to replace these calls with [`postMessage()`](https://developer.mozilla.org//docs/Web/API/Client/postMessage) and a service worker's [message event handler](https://developer.mozilla.org/docs/Web/API/ServiceWorkerGlobalScope/message_event).

## Replace unsupported APIs

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

| Manifest V2 method or property | Replace with |
|---|---|
| `browser.extension.connect()` | `browser.runtime.connect()` |
| `browser.extension.connectNative()` | `browser.runtime.connectNative()` |
| `browser.extension.getExtensionTabs()` | `browser.extension.getViews()` |
| `browser.extension.getURL()` | `browser.runtime.getURL()` |
| `browser.extension.lastError` | Where methods return promises, use `promise.catch()` |
| `browser.extension.onConnect` | `browser.runtime.onConnect` |
| `browser.extension.onConnectExternal` | `browser.runtime.onConnectExternal` |
| `browser.extension.onMessage` | `browser.runtime.onMessage` |
| `browser.extension.onRequest` | `browser.runtime.onMessage` |
| `browser.extension.onRequestExternal` | `browser.runtime.onMessageExternal` |
| `browser.extension.sendMessage()` | `browser.runtime.sendMessage()` |
| `browser.extension.sendNativeMessage()` | `browser.runtime.sendNativeMessage()` |
| `browser.extension.sendRequest()` | `browser.runtime.sendMessage()` |
| `browser.runtime.onSuspend` (background scripts) | Not supported in extension service workers. Use the [`beforeunload`](https://developer.mozilla.org/docs/Web/API/Window/beforeunload_event) document event instead. |
| `browser.tabs.getAllInWindow()` | `browser.tabs.query()` |
| `browser.tabs.getSelected()` | `browser.tabs.query()` |
| `browser.tabs.onActiveChanged` | `browser.tabs.onActivated` |
| `browser.tabs.onHighlightChanged` | `browser.tabs.onHighlighted` |
| `browser.tabs.onSelectionChanged` | `browser.tabs.onActivated` |
| `browser.tabs.sendRequest()` | `browser.runtime.sendMessage()` |
| `browser.tabs.Tab.selected` | `browser.tabs.Tab.highlighted` |