edabg / jsprintsetup

JSPrintSetup Firefox addon
Mozilla Public License 2.0
76 stars 39 forks source link

Is there any way to catch end of async printing event in Firefox? #28

Closed survtur closed 7 years ago

survtur commented 7 years ago

All methods I found run some code after printer settings dialog closed (or almost immediately in case of silent printing).

I need it to print page, change page content via JS and print again.

mitkola commented 7 years ago

You can use progressListener to 'know' when print is done. But in fact this is not fully enough, the is need to add little more wait after this event. Here is sample code which print same content multiple times, which you can adapt with content modification.

// define progress listener object
var progressListener = {
    stateIsRequest: false,
    printing: false,

    QueryInterface : function(aIID) {
        if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
            aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
            aIID.equals(Components.interfaces.nsISupports))
            return this;
        throw Components.results.NS_NOINTERFACE;
    },

    onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {
//      alert('State Change -> State Flags:'+aStateFlags+' Status:'+aStatus);
        if (aStateFlags == 131088) // 0x20010 = STATE_IS_DOCUMENT | STATE_STOP
            this.printing = false;
        return 0;
    },

    onLocationChange : function(aWebProgress, aRequest, aLocation) {
        return 0;
    },

    onProgressChange : function(aWebProgress, aRequest,
        aCurSelfProgress, aMaxSelfProgress,
        aCurTotalProgress, aMaxTotalProgress){
//      alert('Self Current:'+aCurSelfProgress+' Self Max:'+aMaxSelfProgress
//              +' Total Current:'+aCurTotalProgress+' Total Max:'+aMaxTotalProgress);
//      if (!this.printStarted)
//          alert('started:'+this.copies);
        this.printing = true;
    },

    onStatusChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {
//      alert('Status Change -> State Flags:'+aStateFlags+' Status:'+aStatus);
        return 0;
    },

    onSecurityChange : function(aWebProgress, aRequest, aState){
        return 0;
    },

    //onLinkIconAvailable : function(a){},

};

var mPrint = {
    copies: 0,

    print : function(copies) {
        try {
            jsPrintSetup.setGlobalOption('DEBUG', true);
            jsPrintSetup.setPrinter('PDFCreator');
            jsPrintSetup.clearSilentPrint();
            jsPrintSetup.setPrintProgressListener(progressListener);
//          jsPrintSetup.setOption('printSilent', 1);
            this.copies = copies;
            this.printNext();
//          jsPrintSetup.setOption('printSilent', 0);
        } catch (err) {
            alert(err);
        }   
    },

    checkPrintNext: function() {
        // check to printing on every 500ms  
        if (progressListener.printing) {
            setTimeout('mPrint.checkPrintNext()', 500);
        } else
            mPrint.printNext();
    },

    printNext : function() {
        if (this.copies > 0) {
            jsPrintSetup.setOption('printSilent', 1);
            jsPrintSetup.print();
            jsPrintSetup.setOption('printSilent', 0);
            this.copies--;
            if (this.copies > 0) {
                // give a time to browser to start printing and continue JS execution
                setTimeout('mPrint.checkPrintNext()', 1000);
            }   
        }   
    }, 

};

You can read more about progress listener at: https://github.com/edabg/jsprintsetup/wiki#setprintprogresslistener https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebProgressListener

survtur commented 7 years ago

Thank you very very much. I wonder why didn't I notice it in the docs. I have to donate this extension.