Javascript 添加了Greasemonkey和x2B的按钮;jQuery出现,但不';我一点也不工作
使用Greasemonkey(v.1.8)、jQuery(2.0)和Firefox(20.0),我想在页面上为每个音乐片段添加按钮,例如。Javascript 添加了Greasemonkey和x2B的按钮;jQuery出现,但不';我一点也不工作,javascript,jquery,click,greasemonkey,Javascript,Jquery,Click,Greasemonkey,使用Greasemonkey(v.1.8)、jQuery(2.0)和Firefox(20.0),我想在页面上为每个音乐片段添加按钮,例如。 我尝试了几种添加按钮的方法。它们会出现,但单击它们没有效果。这段代码省略了一些函数,以使其专注于问题 // ==UserScript== // @name BBC Radio 3 Through the Night // @namespace Zahphod.beeblebrox // @descriptio
我尝试了几种添加按钮的方法。它们会出现,但单击它们没有效果。这段代码省略了一些函数,以使其专注于问题
// ==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);});
});
})();
/==UserScript==
//@name英国广播公司第三台彻夜直播
//@namespace Zahphod.beeblebrox
//@description英国广播公司第三台彻夜直播
//@包括http://www.bbc.co.uk/programmes/*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
//@需要https://gist.github.com/raw/2625891/waitForKeyElements.js
//@grant GM_log
//@version 0.0.01
//==/UserScript==
(功能(){
艺术家;
//等待“显示更多”
waitForKeyElements(#synosis div.copy a.show-more-truncate),单击showmorelink,true);
功能单击ShowMoreLink(jNode){
var clickEvent=document.createEvent('MouseEvents');
clickEvent.initEvent(“单击”,真,真);
jNode[0]。dispatchEvent(clickEvent);
}
//如果不是通宵播放列表,请跳过
如果((!document.title)| |(document.title.indexOf(“穿越黑夜”)<0))返回;
$(“div.full_synosis>div>p:gt(0)”。每个(函数(){
艺术家=$(this).get(0).childNodes[2].textContent.replace(/^\n/,“”);
//var new_按钮=$(“查询”+艺术家+”);
var new_button=XPCNativeWrapper.unwrap($(“查询“+artist+”);
$(此).append(新建按钮);
$(新建按钮)。单击(函数(){alert(artist);});
});
})();
感谢您的建议。按钮由XPCNativeWrapper包裹在Greasemonkey下
请尝试XPCNativeWrapper.unwrap,如中所述。按钮由XPCNativeWrapper在Greasemonkey下包裹
试试XPCNativeWrapper.unwrap,就像它在中所描述的那样,这不是激活您添加的许多按钮的好方法。并且,正在以几种不常见的方式阻止该代码 这方面有几个问题:
$(new_button).click(function(){ alert(artist);});
新建按钮
已经是jQuery对象。您将使用new\u按钮。除了其他问题外,请单击(…
)artist
将不是你想象的那样。你至少需要一个结尾alert()
!因此无论发生什么,您都不会看到消息。无论如何,使用alert()
进行调试是一个糟糕的做法。学会爱上Firebug的控制台并使用控制台。
日志记录函数系列点击(或其他)处理程序,每个按钮对应一个!这会阻塞内存,使事情陷入困境,并使调试更加困难
jQuery的.on()
非常适合于此
该代码的其他问题:
$(this).get(0)
,在一个循环中。each()
循环可以归结为this
GM\u addStyle()
).on()
还有一个原因总而言之,将代码的最后一部分更改为:
$("div.full_synopsis>div>p:gt(0)").each ( function () {
var artist = this.childNodes[2].textContent.replace(/^\n/, "");
$(this).append (
'<button class="gmArtistQryBtn" data-artist="' + artist
+ '">Query ' + artist + '</button>'
);
} );
$("div.full_synopsis").on (
"click",
"button.gmArtistQryBtn",
function (zEvent) {
var artist = $(zEvent.target).data ("artist") || "OOPSIE!";
console.log ("artist: ", artist);
}
);
$(“div.full\u大纲>div>p:gt(0)”)。每个(函数(){
var artist=this.childNodes[2].textContent.replace(/^\n/,“”);
$(此)。附加(
“查询”+艺术家+“”
);
} );
$(“部门完整摘要”)。在(
“点击”,
“button.gmArtistQryBtn”,
功能(zEvent){
var artist=$(zEvent.target).data(“artist”)| |“OOPSIE!”;
console.log(“艺术家:”,艺术家);
}
);
请注意我们是如何传递艺术家数据的。这种方法在浅层HTML克隆(页面可能正在这样做)中幸存下来。这不是激活您添加的许多按钮的好方法。而且,它以几种不常见的方式阻碍了代码的运行 这方面有几个问题:
$(new_button).click(function(){ alert(artist);});
new\u按钮
已经是jQuery对象。您可以使用new\u按钮。除了其他问题外,请单击(…
)artist
将不是你想象的那样。你至少需要一个结尾alert()
!因此无论发生什么,您都不会看到消息。无论如何,使用alert()
进行调试是一个糟糕的做法。学会爱上Firebug的控制台并使用控制台。
日志记录函数系列点击(或其他)处理程序,每个按钮对应一个!这会阻塞内存,使事情陷入困境,并使调试更加困难
jQuery的.on()
非常适合于此
该代码的其他问题:
$(this).get(0)
,在一个循环中。each()
循环可以归结为this
GM\u addStyle()
).on()
还有一个原因普蒂