I am trying to build a simple extension, but I get "Service worker registration failed" error on the background.js line. I read some other posts using a wrapper file to load the background.js but I am using chrome version 105 and shouldn't need to use the wrapper fix.
manifest.jason:
{
"name": "Test",
"description" : "Test",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
}
}
background.js:
chrome.action.onClicked.addListener(tab => {
chrome.tabs.update({url: 'http://example.com'});
});
CodePudding user response:
How to investigate service worker registration error
If you click Errors
in chrome://extensions page you'll see:
Uncaught TypeError: Cannot read properties of undefined (reading 'onClicked')
Click it and you'll see the source:
chrome.action.onClicked.addListener(tab => {
It means that chrome.action
is undefined.
Problem
Lots of API inside chrome
are present only if your extension's manifest.json lists its associated permission name in "permissions"
or in a special key - this is indicated at the beginning of the official documentation page for an API.
Solution
Add "action": {}
to manifest.json.
You can also specify a title and an icon inside, see the documentation, but be careful with default_popup
- it disables onClicked listener when specified i.e. if you want to use default_popup to show html in this standard popup you'll need to move the inside of your onClicked listener from background.js to the beginning of the popup.js script for your popup.html, which will run every time the popup is shown. In your case it's chrome.tabs.update({url: 'http://example.com'});