将标签转换为打印预览,但没有gui

时间:2015-09-16 12:39:24

标签: firefox-addon

我正在尝试将标签转换为打印预览图。我成功地转向了这样的打印预览:

       SELECT ParallelPeriod ([Date].[Calendar].[Calendar Semester]
       , 3
       , [Date].[Calendar].[Month].[October 2007])
       ON 0

   FROM [Adventure Works]


results:
April 2006
$882,899.94

如果您将粘贴复制到暂存器,请执行以下操作:

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');
}

你会看到它转向打印预览,但它没有按钮去打印对话框,甚至关闭。后退按钮不再起作用了。

你能帮助我弄清楚为什么它如此古怪。

由于

2 个答案:

答案 0 :(得分:1)

如果您只想发起打印预览,则可以执行以下操作:

function noida_printpreview(aWindow) {
    aWindow.document.getElementById("cmd_printPreview").doCommand();
}

可以通过以下方式启动打印:

function noida_print(aWindow) {
    aWindow.document.getElementById("cmd_print").doCommand();
}

这些使用内置的Firefox命令来执行打印或打印预览。使用内置的command interface。它们应该比自己硬编码更具有前瞻性(它们是一种预期的抽象,因此“可以调用它而无需了解其实现的细节。”

我在我的Firefox附加组件Print Button is Print中使用了这两个,它将打印按钮更改为实际上是一个打印按钮,打印预览功能可用 shift -click和/或 ctrl -click。

单击打印按钮时,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);
    }
}

答案 1 :(得分:1)

<击> 发布这项工作正在进行以显示@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

  

返回包含正在打印的文档的内容。这必须将其type属性设置为&#34; content&#34;,&#34; content-targetable&#34;或&#34; content-primary&#34;

        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的 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 。我得到的错误是:

<击>   

[例外...&#34;组件返回失败代码:0x80070057(NS_ERROR_ILLEGAL_VALUE)[nsIWebBrowserPrint.printPreview]&#34; nsresult:&#34; 0x80070057(NS_ERROR_ILLEGAL_VALUE)&#34; location:&#34; JS frame :: chrome://global/content/browser-content.js :: enterPrintPreview :: line 469&#34; data:no] browser-content.js:473:0   TypeError:设置一个只有getter browser-content.js的属性:585:9   

编辑 - 神圣的哎呀,我得到它打印iframe打印预览!!

将粘贴解决方案复制到暂存器!

addGlobalMonitorForEventsMatchingMask