javascriptgoogle-chrome-extensionmicrosoft-edgemicrosoft-edge-extension

Code in popup.js not being run (chrome/edge extension)


Related to:

The Chrome extension popup is not working, click events are not handled

Javascript in Google Chrome popup extension not running

Cannot get Chrome popup.js to use console.log

manifest.json

{
    "permissions": [
        "activeTab",
        "tabs",
        "storage",
        "<all_urls>",
        "*://*.youtube.com/*",
    ],
    "chrome_url_overrides": {
        "newtab": "popup/popup.html"
    },
    "browser_action": {
        "default_title": "My extension"
    },
    "background": {
        "scripts": [
            "background/background.js"
        ]
    },
    "content_scripts": [{
        "js": [
            "content/content.js"
        ],
        "matches": [
            "<all_urls>",
            "*://*.youtube.com/*"
        ],
        "run_at": "document_end",
        "all_frames": true,
        "match_about_blank": true
    }],
    "web_accessible_resources": [
        "*.html",
        "images/*"
    ],
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

popup.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Extension</title>
</head>

<body>
<div>dkafafdafldafkla This shows</div>
</body>

<script src="popup.js">
</script>

</html>

popup.js

document.addEventListener("DOMContentLoaded", () => {
  console.log("Adding dom load event listener; we don't get here :(");
  chrome.tabs.create({
    active: true,
    url: "https://my.site.com/",
  });
});

The extension does not seem debugable:

enter image description here

There are no errors in the console (both web page and background scripts); why isn't the url opened when the popup is clicked (the text form popup.html appears in the new tab).


Solution

  • Managed to get it running as intended; removed chrome_url_overrides from manifest.json above and added the following to background.js:

    chrome.tabs.onCreated.addListener(function(tab) {
      console.log("Current tab: ", tab.url);
      if (tab.url === "edge://newtab/") {
        chrome.tabs.update(tab.id, {
          url: "https://my.site.com/",
        });
      }
    });