使用Greasemonkey(v.1.8)和jQuery(2.0)和Firefox(20.0)我想爲頁面上的每個音樂作品添加按鈕,例如Through the Night。
我已經嘗試了幾種方法來添加按鈕。它們出現,但點擊它們沒有效果。這段代碼留下了一些功能,以使它專注於這個問題。使用Greasemonkey + jQuery添加的按鈕會出現,但在點擊時不起作用
// ==UserScript==
// @name BBC Radio 3 Through the Night
// @namespace Zahphod.beeblebrox
// @description BBC Radio 3 Through the Night
// @include http://www.bbc.co.uk/programmes/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_log
// @version 0.0.01
// ==/UserScript==
(function(){
var artist;
// wait to "show more"
waitForKeyElements ("#synopsis div.copy a.show-more-truncate", clickShowMoreLink, true);
function clickShowMoreLink (jNode) {
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent ("click", true, true);
jNode[0].dispatchEvent (clickEvent);
}
// skip if not a Through the Night playlist
if ((!document.title) || (document.title.indexOf("Through the Night") < 0)) return;
$("div.full_synopsis>div>p:gt(0)").each(function() {
artist = $(this).get(0).childNodes[2].textContent.replace(/^\n/, "");
// var new_button = $("<button>Query " + artist + "</button>");
var new_button = XPCNativeWrapper.unwrap($("<button>Query " + artist + "</button>"));
$(this).append(new_button);
$(new_button).click(function(){ alert(artist);});
});
})();
建議感激。
Brock, 這非常好。 我有很多東西需要學習。感謝您的知識,耐心和明確的解釋。 我刪除了閉包,我不得不讓代碼更簡單,但是你的方法甚至比你沒有看到的代碼更好。 – 2013-04-29 11:46:17