谷歌浏览器扩展程序:无法启动打印对话框 [英] Google Chrome Extension: Cannot launch Print dialog

查看:1357
本文介绍了谷歌浏览器扩展程序:无法启动打印对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的google chrome扩展程序中点击按钮时启动打印对话框。当扩展的html文件作为独立文件打开时,代码似乎正在工作,但是当它作为扩展加载时,它不会运行。



HTML:
< input id =print_pagetype =buttonvalue =Printonclick =print_p()/>



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

有什么不对的地方?

解决方案

除了我提到的内联JavaScript问题以外,似乎从弹出窗口(或背景页面)调用打印对话框是不可能的

解决方法是在你的扩展中有一个打印助手页面,打开一个正常的标签并打开一个打印对话框。



可能的体系结构:


  1. 点击弹出窗口中的数据将被发送到后台页面:

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

    它通过后台页面发送,因此您不必担心弹出式关闭。

  2. 在后台页面中,打开一个帮助页面:

      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. 在打印助手页面中,脚本 print.js 请求数据,根据需要设置格式并调用打印对话框:

      chrome.runtime.sendMessage( {getPrintData:true},function(response){
    formatDataIntoPage(response.data);
    window.print();
    });


  4. 返回到后台页面,根据打印帮助程序的请求提供数据:

      chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
    // ...
    if(request.getPrintData){
    sendResponse({data:printData});
    }
    });



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?

解决方案

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

这篇关于谷歌浏览器扩展程序:无法启动打印对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆