我在这里问了一个问题并得到了答案:如何从 Greasemonkey 调用这个 YouTube 功能?
该代码起作用并向页面添加了一个按钮,该按钮捕获了视频时间。
但是,关键部分必须在目标页面范围内运行——Greasemonkey 的GM_
功能不可用的地方。
我想用来GM_setValue()
记录视频时间。如何GM_setValue()
从按钮的click
处理程序中调用?
... ...
//-- Only run in the top page, not the various iframes.
if (window.top === window.self) {
var timeBtn = document.createElement ('a');
timeBtn.id = "gmTimeBtn";
timeBtn.textContent = "Time";
//-- Button is styled using CSS, in GM_addStyle, below.
document.body.appendChild (timeBtn);
addJS_Node (null, null, activateTimeButton);
}
function activateTimeButton () {
var timeBtn = document.getElementById ("gmTimeBtn");
if (timeBtn) {
timeBtn.addEventListener ('click',
function () {
var ytplayer = document.getElementById ("movie_player");
//-- IMPORTANT: GM_functions will not work here.
console.log ("getCurrentTime(): ", ytplayer.getCurrentTime() );
alert (ytplayer.getCurrentTime() );
},
false
);
}
else {
alert ("Time button not found!");
}
}
... ...
谢谢 :-)