chrome 扩展弹出窗口.location 更改

时间:2021-01-28 14:02:34

标签: javascript html google-chrome

我正在为浏览器开发扩展程序,但我在使用 Google Chrome 时遇到了问题。 在 manifest.json 文件中,我已经声明了 default_popup 页面,并且在该页面中我有一个按钮,该按钮将在单击时更改 window.location.href。问题是当我点击那个按钮时,弹出页面没有改变。我如何在 Google Chrome 上实现这一目标?在 Firefox 上运行良好。

这是代码

ma​​nifest.json

  "manifest_version": 2,
  "name": "NewExt",
  "version": "1.0",

  "description": "My best project",

  "icons": {
    "48": "icons/icon-48.png"
  },
  "browser_action": {
    "browser_style": true,
    "default_popup": "popup.html",
    "default_icon": {
      "48": "icons/icon-48.png"
    }
  }
}

popup.html

<!DOCTYPE html>
<html>
  <head> </head>
  <body>
    <button id="btn">Click</button>
    <script src="main.js" type="text/javascript"></script>
  </body>
</html>

ma​​in.js

function redirect() {
  window.location.href = "http://example.org";
  console.log("works");
}
document.getElementById("btn").addEventListener("click", function () {
  redirect();
});

0 个答案:

没有答案