2012-05-03 76 views
1

我创建了一个Greasemonkey的脚本的目的:的Greasemonkey:添加打印按钮 - 打印和“打印友好”的风格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(); 

回答

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(); 
}); 
+0

谢谢!我会给出一个镜头...不能相信拼写错误会消耗多少时间。 – Bubnoff

+0

作品,谢谢你! – Bubnoff

+0

不客气,乐意效劳。 –