Firefox addon 将选项卡转换为打印预览,但没有gui
我正在尝试将选项卡转换为打印预览。我正在成功地转到打印预览,如下所示:Firefox addon 将选项卡转换为打印预览,但没有gui,firefox-addon,Firefox Addon,我正在尝试将选项卡转换为打印预览。我正在成功地转到打印预览,如下所示: function noida_printpreview(aWindow) { var wbp = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebBrowserPrint); var listener = { onLocationChange: function(webProgress, reques
function noida_printpreview(aWindow) {
var wbp = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebBrowserPrint);
var listener = {
onLocationChange: function(webProgress, request, location, flags) {},
onProgressChange: function(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {},
onSecurityChange: function(webProgress, request, state) {},
onStateChange: function(webProgress, request, stateFlags, status) {},
onStatusChange: function(webProgress, request, status, message) {},
QueryInterface: function(iid) {
if (iid.equals(Ci.nsIWebProgressListener) || iid.equals(Ci.nsISupportsWeakReference)) {
return this;
}
throw Cr.NS_NOINTERFACE;
}
}
Services.prefs.setBoolPref('print.show_print_progress', false);
//XXX I would have thought this would work, instead I'm forced to use prefs service
wbp.globalPrintSettings.showPrintProgress = false;
wbp.printPreview(wbp.globalPrintSettings, aWindow, listener);
Services.prefs.clearUserPref('print.show_print_progress');
}
如果将其复制粘贴到scratchpad,请执行以下操作:
noida_printpreview(content)
你们会看到它变成了打印预览,但它并没有按钮去打印对话框,甚至并没有关闭。“后退”按钮不起作用了
你能帮我弄明白为什么它这么古怪吗
谢谢如果您只是想启动打印预览,则可以执行以下操作:
function noida_printpreview(aWindow) {
aWindow.document.getElementById("cmd_printPreview").doCommand();
}
可以通过以下方式启动打印:
function noida_print(aWindow) {
aWindow.document.getElementById("cmd_print").doCommand();
}
它们使用内置的Firefox命令执行打印或打印预览。使用内置的。它们应该比自己硬编码更适合未来(它们是一种预期的抽象,因此“可以在不需要了解其实现细节的情况下调用它”)
我在我的Firefox插件中使用了这两种功能,它将打印按钮改回实际的打印按钮,并通过按住shift键单击和/或按住ctrl键单击提供打印预览功能
单击“打印”按钮时,该插件使用以下功能执行打印或打印预览:
const extensionPrefs = "extensions." + "PrintButtonIsPrint";
function handlePrintButton(event) {
var window = event.view;
let commandId = "cmd_print";
if(
(event.ctrlKey
&& (!Services.prefs.prefHasUserValue(extensionPrefs + ".printPreview.ctrl")
|| Services.prefs.getBoolPref(extensionPrefs + ".printPreview.ctrl")))
|| (event.shiftKey
&& (!Services.prefs.prefHasUserValue(extensionPrefs + ".printPreview.shift")
|| Services.prefs.getBoolPref(extensionPrefs + ".printPreview.shift")))
) {
//The user can enable/disable being able to use shift and/or ctrl-click via
// the add-on's option dialog.
commandId = "cmd_printPreview";
}
let printCommand = window.document.getElementById(commandId);
try {
printCommand.doCommand();
} catch (e) {
//The doCommand() causes an exception on Linux, but doing the exact same thing
// as the command does work. I assume this is because of the scope of the
// window variable.
//Having this explicitly done here will require this to be changed if the method
// of calling the print command changes instead of any such changes automatically
// being incorporated by using the "cmd_print".
//Note: We try a print command here even if it is a preview which failed.
PrintUtils.print(window.gBrowser.selectedBrowser.contentWindowAsCPOW,
window.gBrowser.selectedBrowser);
}
}
发布此正在进行的工作以显示@Mayken,以便他能够提供帮助,此副本可粘贴到scratchpad
这使用了以下技术:
let b = window.gBrowser, pT = b.selectedTab;
b.getBrowserForTab((b.selectedTab = b.addTab(canvas.toDataURL("image/png")))).addEventListener("load", function() {
this.removeEventListener("load", arguments.callee, true);
let l = window.PrintPreviewListener, e = l.onExit;
l.onExit = function() {
e.call(l);
b.removeCurrentTab();
b.selectedTab = pT;
l.onExit = e;
};
window.PrintUtils.printPreview(l);
}, true );
查看它传入打印预览侦听器对象,我们可以临时覆盖它,然后在退出时还原。查看dxr上的源:
这表明我们有getSourceBrowser
,注释中说明如下:
返回包含正在打印的文档的。其类型属性必须设置为“content”、“content targetable”或“content primary”
问题是,它没有进入打印预览。这应该可以工作,因为im将getSourceBrowser
设置为iframe的设置。我得到的错误是:
[异常…”组件返回故障代码:0x80070057(NS_错误\u非法\u值)[nsIWebBrowserPrint.printPreview]“nsresult:”0x80070057(NS_错误\u非法\u值)”位置:“JS帧::chrome://global/content/browser-content.js ::enterPrintPreview::第469行“数据:否]浏览器内容。js:473:0
TypeError:设置只有getter浏览器内容的属性。js:585:9
编辑-天哪,我得到它打印一个iframe打印预览!!
将粘贴解决方案复制到scratchpad
var win = Services.wm.getMostRecentWindow('navigator:browser'); //Services.appShell.hiddenDOMWindow;
var doc = win.document;
var iframe = doc.createElement('browser');
win.nsif = iframe;
iframe.addEventListener('load', function() {
iframe.removeEventListener('load', arguments.callee, true);
var aPPListener = win.PrintPreviewListener;
var aOrigPPgetSourceBrowser = aPPListener.getSourceBrowser;
aPPListener.getSourceBrowser = function() {
var asettime = win.setTimeout(function() {
aPPListener.getSourceBrowser = aOrigPPgetSourceBrowser;
win.alert('ok restored getSourceBrowser');
}, 5000);
return iframe;
};
win.PrintUtils.printPreview(aPPListener);
}, true); // if i use false here it doesnt work
iframe.setAttribute('type', 'content');
iframe.setAttribute('src', 'data:text,html,this is test text');
doc.documentElement.appendChild(iframe); // src page wont load until i append to document
请更清楚地了解您试图实现的目标。您是否尝试启动选项卡/窗口的正常打印预览?除了标准打印预览之外,您是否尝试执行任何特殊操作?哇,非常感谢Mayken!这些方法似乎可以打印当前关注的选项卡。理想情况下,我要打印的iframe,是我附加到
Services.appShell.hiddenDOMWindow
的一个。我将其直接发送到打印机,代码如下:是否有必要将隐藏的iframe发送到任何给定的“navigator:browser”中的打印预览选项卡?Hey@mayken你能看到我在这里发布的解决方案吗?这不是一个解决方案,而是一个正在使用P的工作rintPreviewListener
对象将getSourceBrowser
更改为iframe的,并保持getPrintPreviewBrowser
不变,因此它使用窗口来显示打印预览,您能帮我删除该窗口吗?哇,我还以为我可以打开一个新窗口,使打印预览从任何窗口转到t窗,太神奇了!
var win = Services.wm.getMostRecentWindow('navigator:browser'); //Services.appShell.hiddenDOMWindow;
var doc = win.document;
var iframe = doc.createElement('browser');
win.nsif = iframe;
iframe.addEventListener('load', function() {
iframe.removeEventListener('load', arguments.callee, true);
var aPPListener = win.PrintPreviewListener;
var aOrigPPgetSourceBrowser = aPPListener.getSourceBrowser;
aPPListener.getSourceBrowser = function() {
var asettime = win.setTimeout(function() {
aPPListener.getSourceBrowser = aOrigPPgetSourceBrowser;
win.alert('ok restored getSourceBrowser');
}, 5000);
return iframe;
};
win.PrintUtils.printPreview(aPPListener);
}, true); // if i use false here it doesnt work
iframe.setAttribute('type', 'content');
iframe.setAttribute('src', 'data:text,html,this is test text');
doc.documentElement.appendChild(iframe); // src page wont load until i append to document