0

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).

Sebi
  • 4,262
  • 13
  • 60
  • 116

2 Answers2

1

Based on your description and the manifest code you provided, and the screenshot shows the "Inspect popup" option is disabled (greyed out), so I think your problem is that there is no "default_popup" declaration in the manifest. Or you can use chrome.browserAction.setPopup to solve the problem.

This is a similar case:Disable "inspect popup" menu entry in chrome extensions.

Xudong Peng
  • 1,463
  • 1
  • 4
  • 9
  • Added the `default_popup` the html is shown but the code in `popup.js` is not run. Added an answer; not sure if it's consistent with chrome extension design. – Sebi Oct 07 '21 at 18:04
0

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/",
    });
  }
});
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • Thanks for posting the solution for this issue. You can mark your answer as an accepted answer when it is available to mark. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Xudong Peng Oct 08 '21 at 01:35