Greasemonkey:添加打印按钮 - 打印和'打印友好'样式div

时间:2012-05-03 03:45:37

标签: greasemonkey

我为了以下目的创建了一个greasemonkey脚本:

  • 将打印收据按钮添加到第三方Web应用程序页面
  • 在新窗口中为div添加收据打印机友好样式
  • 打印此窗口

按钮显示,但它不会触发我的功能,此时只打印div。

这是我在的地方:

var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';

scriptElement.innerHTML =  'function printReceipt() { \
var divToPrint=document.getEelementById("loanTable"); \
newWin= window.open(""); \
newWin.document.write(divToPrint.outerHTML); \
newWin.print(); \
newWin.close(); \
}';

document.getElementsByTagName("head")[0].appendChild(scriptElement);

window.addButton = function () {
    // Get the location on the page where you want to create the button
    var targetDiv = document.getElementById('newcheckout');

    // Create a div to surround the button
    var newDiv = document.createElement('div');
    newDiv.setAttribute('id', 'autoCheckOrder');

    // Create the button and set its attributes
    var inputButton = document.createElement('input');
    inputButton.name = 'autoCheckOrderButton';
    inputButton.type = 'button';
    inputButton.value = 'Print Receipt?';
    inputButton.setAttribute("onclick", "printReceipt();");

    // Append the button to the div
    newDiv.appendChild(inputButton); 
    targetDiv.appendChild(newDiv);
}
addButton();

1 个答案:

答案 0 :(得分:2)

这一行有一个错字:

var divToPrint=document.getEelementById("loanTable"); \

将其更改为:

var divToPrint=document.getElementById("loanTable"); \



或者,将此行添加到脚本的metadata section

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

然后整个脚本变成:

$("#newcheckout").append ('<div id="autoCheckOrder"></div>');
$("#autoCheckOrder").append ('<button>Print Receipt?</button>');

$("#autoCheckOrder button").click ( function () {
    var divToPrint  = document.getElementById ("loanTable"); 
    var newWin      = window.open (""); 
    newWin.document.write (divToPrint.outerHTML); 
    newWin.print(); 
    newWin.close(); 
} );