1

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: <input id="print_page" type="button" value="Print" onclick="print_p()" />

JavaScript: function print_p(){ window.print();}

Any idea as to what's wrong?

  • possible duplicate of [onClick within Chrome Extension not working](http://stackoverflow.com/questions/13591983/onclick-within-chrome-extension-not-working) – Xan Jun 12 '14 at 18:29
  • @Xan: I did try that already, but didn't turn out to be helpful for me. I also tried a different approach, which again worked on a standalone html file and not in an extension. Here goes(sorry about the formatting) HTML: JS: function print_p(){ window.print(); } $("#print_page").click(function() { print_p(); }); – user3727438 Jun 12 '14 at 20:20
  • You should edit your question with that instead of commenting; besides, inline code is separated with backticks, \`. Your jQuery attempt is still wrong unless you wrap it in `$(document).ready({/*...*/});` – Xan Jun 12 '14 at 20:25
  • @Xan: I'll keep that in mind. Thank you for the effort. But I've tried that as well. I beginning to wonder if a print dialog access is not allowed from an extension in chrome. – user3727438 Jun 12 '14 at 20:37
  • I just tested it, and it doesn't work from a popup. On other extension pages it works. Is that your case? – Xan Jun 12 '14 at 20:39
  • @Xan: What exactly do you mean by your question? Could you rephrase your question, please? The only way it works right now is if I access it as a regular webpage on the browser. – user3727438 Jun 12 '14 at 20:43
  • Which extension page are you trying to do this from? A popup? – Xan Jun 12 '14 at 20:48
  • @Xan: Yup, from a popup. – user3727438 Jun 12 '14 at 20:49

1 Answers1

3

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:

  1. On a click in a popup, data to print is being sent to the background page:

    function printClick(){
      chrome.runtime.sendMessage({ print: true, data: whateverYouWantToPrint });
    }
    

    It's routed through the background page so that you don't have to worry about popup closing.

  2. In the background page, a helper page is opened:

    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") }
        );
      }
      // ...
    });
    
  3. In the print helper page, a script 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();
    });
    
  4. Back in the background page, serve the data on request from print helper:

    chrome.runtime.onMessage.addListener( function(request, sender, sendResponse){
      // ...
      if(request.getPrintData) {
        sendResponse({ data: printData });
      }
    });
    
Xan
  • 66,873
  • 13
  • 150
  • 174