- All background scripts must be written as service workers, as required by Manifest V3.
- Do not use DOM APIs (e.g.,
window,document) in background scripts.
- Do not use
chrome.webRequestorchrome.webRequestBlockingin background scripts, as these are restricted in Manifest V3 and do not allow access to request/response bodies. - Use
chrome.declarativeNetRequestfor request blocking or redirection, if needed.
A minimal, compatible background script should look like this:
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// Example: handle messages from content or popup scripts
if (request.greeting === "hello") {
sendResponse({ reply: "hi from background!" });
}
});- If you need to capture POST data or request/response bodies, use a content script to intercept form submissions or XHR/fetch requests, then communicate with the background script via
chrome.runtime.sendMessage.
- If you see
Service worker registration failed. Status code: 15, check for incompatible code or APIs in your background script. - Ensure all permissions in
manifest.jsonare appropriate for Manifest V3.
Last updated: 2024-06-10