Javascript 如何使用Greasemonkey调用现有视频的Youtube Flash API
我想写一个Greasemonkey脚本来检测网站上的YouTube视频何时开始播放,以阻止我的winamp播放音乐。一切正常,我的脚本检测视频,启用API,并且调用Javascript 如何使用Greasemonkey调用现有视频的Youtube Flash API,javascript,youtube-api,greasemonkey,userscripts,Javascript,Youtube Api,Greasemonkey,Userscripts,我想写一个Greasemonkey脚本来检测网站上的YouTube视频何时开始播放,以阻止我的winamp播放音乐。一切正常,我的脚本检测视频,启用API,并且调用onYouTubePlayerReady事件。但是我不知道如何注册onStateChange回调,这是我的代码: unsafeWindow.onYouTubePlayerReady = function (playerId) { alert('Visible'); document.getElementById(pl
onYouTubePlayerReady
事件。但是我不知道如何注册onStateChange
回调,这是我的代码:
unsafeWindow.onYouTubePlayerReady = function (playerId)
{
alert('Visible');
document.getElementById(playerId).addEventListener('onStateChange', 'stateChanged');
alert('Not visible, so the line above crashes');
}
unsafeWindow.stateChanged = function (state)
{
alert('never called, too');
}
这个问题有解决办法吗?还是根本不可能
问题只是“onStateChange”上的侦听器
好的,为了避免一些范围问题,最好只注入与YouTube API交互的代码
以下内容适用于Firefox+Greasemonkey和Chrome或Chrome+Tampermonkey。它还应适用于任何支持用户脚本的浏览器:
function GM_main () {
window.stateChanged = function (state) {
console.log ('GM: In stateChanged(). State = ', state);
}
window.onYouTubePlayerReady = function (playerId) {
/*-- playerId is not being set by Youtube. Use
hard-coded id (movie_player) instead.
*/
var playerNode = document.getElementById ("movie_player");
if (playerNode) {
/*--- Note, inside onYouTubePlayerReady ONLY, the YouTube API
seems to override addEventListener. Hence the nonstandard
parameters.
*/
playerNode.addEventListener ('onStateChange', 'stateChanged');
console.log ('GM: Listener installed just fine.');
}
else
console.error ("GM: Player node not found!");
}
}
addJS_Node (null, null, GM_main);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement ('script');
if (runOnLoad) {
scriptNode.addEventListener ("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
不能用Greasemonkey做这件事;您必须编写Firefox扩展/加载项。Greasemonkey没有足够的系统权限与Winamp交互。另外,上面的代码中有多个错误。我将使用WifiAmp与winamp交互,问题只是“onStateChange”上的侦听器。谢谢你提供这个代码片段,你测试过这个吗?我编辑了上面的问题,添加事件侦听器时仍然会崩溃。我忘了什么吗?是的,我试过了。您将在Firebug控制台上看到
State=
消息闪过。在编辑的问题中,有许多转录错误:var
不应该存在的声明,额外的引号,等等。确切地说,从我的代码开始。在FF控制台上查看结果,或者最好安装Firebug并使用其控制台。谢谢,它成功了!我的一个问题是,我用了,而不是,在我改变后,一切都很好!如果我有时间完成,我将以greasemonkey脚本的形式发布我的最终代码…您应该更新您的示例代码,因为E4X已经从Firefox中删除。