0

I use Chrome Extension manifest V3, My manifestV3 configuration is as follows

...
"background": {
        "service_worker": "background.js"
},
...
"action": {
    "default_icon": {
         "16": "img/icon.png",
          "32": "img/icon.png",
           "48": "img/icon.png",
           "64": "img/icon.png",
           "128": "img/icon.png"
     }
 },
...

My background.js is as follows:

chrome.action.onClicked.addListener(async (tab) => {
    console.log(tab);
    if (tab.url.startsWith('https://www.google.com')) {
        console.log('this is googlepage');
    } else {
        console.log('this is not googlepage');
    }
});

This is very easy, but I can't get the result of console.log. Appeal everybody!

How to solve this problem?

Dao
  • 1
  • you have to add "activeTab" or "tabs" permission in manifest to get url property. – Robbi Mar 20 '23 at 08:40
  • My configuration about permissions is as follows: "host_permissions": [ "" ], "permissions": [ "activeTab", "tabs", "scripting", "cookies" ], It contains activeTab and tabs. – Dao Mar 20 '23 at 09:27
  • Open Devtools --> Console and check if there are some errors. Do it within 30 secs 'cause you could loose the log messages. (the SW go in idle mode after about 30") – Robbi Mar 20 '23 at 09:47
  • Still no response...so strange issue – Dao Mar 20 '23 at 10:02
  • I guess you have just tried to reinstall the extension and\or restart the browser. – Robbi Mar 20 '23 at 10:09
  • Could I send you my entire code, you can run it locally and see if run successfully. But it will take you time... How about this way – Dao Mar 20 '23 at 10:23
  • Fortunately, I solved the problem. I open page "chrome://inspect/#service-workers", click the "inspect" button of the service worker. It pop up a new DevTools. Its javascript context is `background.js` (service worker). The DevTools can show console log. Thank you very much. I've enjoyed talking with you. – Dao Mar 21 '23 at 01:04

0 Answers0