我在这里问了一个问题并得到了答案:How to call this YouTube function from Greasemonkey?
该代码有效并向页面添加了一个按钮,用于捕获视频时间。
但是,关键部分必须在目标页面范围内运行——在那里 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!");
}
}
... ...
谢谢 :-)