2012-05-03 78 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

不客氣,樂意效勞。 –