I asked a question and got an answer here: How to call this YouTube function from Greasemonkey?
That code works and adds a button to the page, which captures the video time.
But, the key part must run in the target-page scope -- where Greasemonkey's GM_
functions are not available.
I want to use GM_setValue()
to record the video time. How do I call GM_setValue()
from my button's click
handler?
Here is the relevant part of the complete script (right-click to save):
... ...
//-- 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!");
}
}
... ...
Thank you :-)