window.print()在浏览器扩展中不起作用



我正试图使用window.print()制作一个打印浏览器扩展,但它不起作用,而作为一个单独的HTML文件,它正在起作用。

以下是popup.jsprint.html

document.getElementById("m").addEventListener("click", myFunction);
function myFunction() {
  window.print();
}
<!DOCTYPE html>
<html>
<body>
  <h2>The window.print() Method</h2>
  <button id="m"> click me </button>
</body>
<script src="popup.js">
</script>
</html>

清单文件是

{
  "manifest_version": 2,
  "name": "print",
  "version": "0.10002",
  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": [
      "popup.js",
      "content.bundle.js"
    ],
    "run_at": "document_idle",
    "matches": ["https://*/*"]
  }],
  "browser_action": {
    "default_popup": "print.html"
  },
  "background": {
    "scripts": [
      "background.bundle.js",
      "background.js"
    ]
  },
  "permissions": [
    "storage",
    "https://*/*"
  ]
}

试试这个:

以下是script.jsfile.html

 document.getElementById("btn").onclick = () => window.print();
<!DOCTYPE html>
<html>
<body>
<p>Click the button to print the current page.</p>
<button id = "btn" >Print this page</button>
<script src = "script.js" ></script>
</body>
</html>

最新更新