popup.js 中的代码不是 运行(chrome/edge 扩展名)

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

相关:

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/",
  });
});

扩展似乎不可调试:

控制台没有错误(网页和后台脚本);为什么单击弹出窗口时 url 没有打开(文本形式 popup.html 出现在新选项卡中)。

根据您的描述和您提供的清单代码,屏幕截图显示“Inspect popup”选项被禁用(灰色),所以我认为您的问题是没有 "default_popup" 声明manifest。或者您可以使用chrome.browserAction.setPopup来解决问题。

这是一个类似的案例:Disable "inspect popup" menu entry in chrome extensions.

设法运行如期获得;从上面的 manifest.json 中删除 chrome_url_overrides 并将以下内容添加到 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/",
    });
  }
});