谷歌浏览器扩展程序:无法启动打印对话框 [英] Google Chrome Extension: Cannot launch Print dialog
问题描述
我想在我的google chrome扩展程序中点击按钮时启动打印对话框。当扩展的html文件作为独立文件打开时,代码似乎正在工作,但是当它作为扩展加载时,它不会运行。
HTML:
< input id =print_pagetype =buttonvalue =Printonclick =print_p()/>
JavaScript: 有什么不对的地方? 除了我提到的内联JavaScript问题以外,似乎从弹出窗口(或背景页面)调用打印对话框是不可能的。 解决方法是在你的扩展中有一个打印助手页面,打开一个正常的标签并打开一个打印对话框。 可能的体系结构: 点击弹出窗口中的数据将被发送到后台页面: 它通过后台页面发送,因此您不必担心弹出式关闭。 在后台页面中,打开一个帮助页面: 在打印助手页面中,脚本 返回到后台页面,根据打印帮助程序的请求提供数据: I'd like to launch the print dialog on button click in my google chrome extension. The code seems to be working when the extension's html file is opened as a standalone file, but not when it's loaded as an extension. HTML:
JavaScript: Any idea as to what's wrong? Aside from the inline JavaScript problem that I mentioned as a duplicate, it seems that invoking the print dialog from a popup (or a background page) is impossible. A workaround would be to have a "print helper" page in your extension, that opens in a normal tab and can open a print dialog. A possible architecture: On a click in a popup, data to print is being sent to the background page: It's routed through the background page so that you don't have to worry about popup closing. In the background page, a helper page is opened: In the print helper page, a script Back in the background page, serve the data on request from print helper:
这篇关于谷歌浏览器扩展程序:无法启动打印对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! function print_p(){window.print();} $ c
函数printClick(){
chrome.runtime.sendMessage({print:true,data:whateverYouWantToPrint});
}
var printData;
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
if(request.print){
printData = request.data;
chrome.tabs.create(
{url:chrome.runtime.getURL(print.html)}
);
}
// ...
});
print.js
请求数据,根据需要设置格式并调用打印对话框:
chrome.runtime.sendMessage( {getPrintData:true},function(response){
formatDataIntoPage(response.data);
window.print();
});
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
// ...
if(request.getPrintData){
sendResponse({data:printData});
}
});
<input id="print_page" type="button" value="Print" onclick="print_p()" />
function print_p(){ window.print();}
function printClick(){
chrome.runtime.sendMessage({ print: true, data: whateverYouWantToPrint });
}
var printData;
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse){
if(request.print) {
printData = request.data;
chrome.tabs.create(
{ url: chrome.runtime.getURL("print.html") }
);
}
// ...
});
print.js
requests the data, formats it as required and invokes the print dialog:chrome.runtime.sendMessage({ getPrintData: true }, function(response){
formatDataIntoPage(response.data);
window.print();
});
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse){
// ...
if(request.getPrintData) {
sendResponse({ data: printData });
}
});