我已经使用 chrome.app.window.create API 创建了新的子窗口,以便使用 window.print() 打印窗口的内容。但我的问题是如何传递 html.please 中的内容任何人都可以帮助我。
最佳答案
新窗口的 DOM 可通过 window.document
访问.
这里的窗口不是传递给 chrome.app.window.create
的窗口回调函数(称之为 createdWindow
),而是 createdWindow.contentWindow
.
所以,您要查找的 DOM 是 createdWindow.contentWindow.document
。根据需要修改该 DOM,然后调用 createdWindow.contentWindow.print()
.
更新——这是我上面所说的代码:
window.onload = function() {
chrome.app.window.create('printwindow.html', {},
function (createdWindow) {
var win = createdWindow.contentWindow;
win.onload = function () {
win.document.querySelector('#content').innerHTML =
'<p>Here is something to print.</p>';
win.print();
}
}
);
};
printwindow.html 是:
<!DOCTYPE html>
<html>
<head>
<title>WindowToPrint</title>
</head>
<body>
<div id="content"></div>
</body>
</html>
我已经测试了这段代码并且它有效。我知道OP采取了另一种方式,但如果可以将其标记为答案,那么对于其他人看这个问题来说会很好。
关于javascript - 如何使用chrome打包的应用程序在子窗口中写入内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25487556/