将选项卡转换为打印预览,但没有 gui
Converting tab to print preview, but no gui
我正在尝试将标签转换为打印预览。我成功地转向了这样的打印预览:
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');
}
如果您将其复制粘贴到暂存器,请执行以下操作:
noida_printpreview(content)
您会看到它转到打印预览,但没有按钮可以转到打印对话框,甚至没有关闭按钮。后退按钮不再起作用。
你能帮我弄清楚为什么它这么古怪吗?
谢谢
如果您只想启动打印预览,则可以执行以下操作:
function noida_printpreview(aWindow) {
aWindow.document.getElementById("cmd_printPreview").doCommand();
}
可以通过以下方式启动打印:
function noida_print(aWindow) {
aWindow.document.getElementById("cmd_print").doCommand();
}
这些使用内置的 Firefox 命令来执行打印或打印预览。使用内置 command interface。它们应该比自己硬编码更具前瞻性(它们是一种有意的抽象,因此它 "can be invoked without needing to know about the details of its implementation."
我在我的 Firefox 附加组件中使用了这两个 Print Button is Print,它将打印按钮改回实际打印按钮,打印预览功能可通过 shift[=28 使用=]-点击and/orctrl-点击.
Print Button is Print 加载项使用以下功能在单击打印按钮时执行打印或打印预览:
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 展示,以便他可以提供帮助,这是可复制粘贴到暂存器的。
这使用了这种技术:
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 上的源代码:
https://dxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#3217
它表明我们有 getSourceBrowser
,在评论中表示为:
https://dxr.mozilla.org/mozilla-central/source/toolkit/components/printing/content/printUtils.js#159
Returns the that contains the document being printed. This must have its type attribute set to "content", "content-targetable", or "content-primary"
var win = Services.wm.getMostRecentWindow('navigator:browser'); //Services.appShell.hiddenDOMWindow;
var doc = win.document;
var iframe = doc.createElementNS('http://www.w3.org/1999/xhtml', 'iframe');
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.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation);
};
win.PrintUtils.printPreview(aPPListener);
}, true); // if i use false here it doesnt work
iframe.setAttribute('src', 'data:text,html,this is test text');
// iframe.setAttribute('style', 'display:none');
doc.documentElement.appendChild(iframe); // src page wont load until i append to document
问题是,它没有进入打印预览。这应该有效,因为我将 getSourceBrowser
设置为 iframe 的设置。我得到的错误是:
[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIWebBrowserPrint.printPreview]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://global/content/browser-content.js :: enterPrintPreview :: line 469" data: no] browser-content.js:473:0
TypeError: setting a property that has only a getter browser-content.js:585:9
编辑 - 天哪,我得到它来打印一个 iframe 来打印预览!!
复制粘贴解决方案到暂存器!
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
我正在尝试将标签转换为打印预览。我成功地转向了这样的打印预览:
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');
}
如果您将其复制粘贴到暂存器,请执行以下操作:
noida_printpreview(content)
您会看到它转到打印预览,但没有按钮可以转到打印对话框,甚至没有关闭按钮。后退按钮不再起作用。
你能帮我弄清楚为什么它这么古怪吗?
谢谢
如果您只想启动打印预览,则可以执行以下操作:
function noida_printpreview(aWindow) {
aWindow.document.getElementById("cmd_printPreview").doCommand();
}
可以通过以下方式启动打印:
function noida_print(aWindow) {
aWindow.document.getElementById("cmd_print").doCommand();
}
这些使用内置的 Firefox 命令来执行打印或打印预览。使用内置 command interface。它们应该比自己硬编码更具前瞻性(它们是一种有意的抽象,因此它 "can be invoked without needing to know about the details of its implementation."
我在我的 Firefox 附加组件中使用了这两个 Print Button is Print,它将打印按钮改回实际打印按钮,打印预览功能可通过 shift[=28 使用=]-点击and/orctrl-点击.
Print Button is Print 加载项使用以下功能在单击打印按钮时执行打印或打印预览:
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 展示,以便他可以提供帮助,这是可复制粘贴到暂存器的。
这使用了这种技术:
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 上的源代码:
https://dxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#3217
它表明我们有 getSourceBrowser
,在评论中表示为:
https://dxr.mozilla.org/mozilla-central/source/toolkit/components/printing/content/printUtils.js#159
Returns the that contains the document being printed. This must have its type attribute set to "content", "content-targetable", or "content-primary"
var win = Services.wm.getMostRecentWindow('navigator:browser'); //Services.appShell.hiddenDOMWindow;
var doc = win.document;
var iframe = doc.createElementNS('http://www.w3.org/1999/xhtml', 'iframe');
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.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation);
};
win.PrintUtils.printPreview(aPPListener);
}, true); // if i use false here it doesnt work
iframe.setAttribute('src', 'data:text,html,this is test text');
// iframe.setAttribute('style', 'display:none');
doc.documentElement.appendChild(iframe); // src page wont load until i append to document
问题是,它没有进入打印预览。这应该有效,因为我将 getSourceBrowser
设置为 iframe 的设置。我得到的错误是:
[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIWebBrowserPrint.printPreview]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://global/content/browser-content.js :: enterPrintPreview :: line 469" data: no] browser-content.js:473:0 TypeError: setting a property that has only a getter browser-content.js:585:9
编辑 - 天哪,我得到它来打印一个 iframe 来打印预览!!
复制粘贴解决方案到暂存器!
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