Javascript 如何称呼Greasemonkey';是否从必须在目标页面范围内运行的代码中删除GM_uu函数?
我问了一个问题,在这里得到了答案: 该代码起作用,并在页面上添加一个按钮,用于捕获视频时间。Javascript 如何称呼Greasemonkey';是否从必须在目标页面范围内运行的代码中删除GM_uu函数?,javascript,youtube,youtube-api,greasemonkey,Javascript,Youtube,Youtube Api,Greasemonkey,我问了一个问题,在这里得到了答案: 该代码起作用,并在页面上添加一个按钮,用于捕获视频时间。 但是,关键部分必须在目标页面范围内运行——在该范围内,Greasemonkey的GM\uu函数不可用 我想用GM_setValue()来记录视频时间。如何从按钮的单击处理程序调用GM\u setValue() 以下是相关部分: 谢谢:-)要使用Greasemonkey的GM函数,请执行以下操作: 让页面范围代码使用postMessage以字符串格式发送数据 让Greasemonkey脚本侦听相应的消息
但是,关键部分必须在目标页面范围内运行——在该范围内,Greasemonkey的
GM\uu
函数不可用
我想用GM_setValue()
来记录视频时间。如何从按钮的单击处理程序调用GM\u setValue()
以下是相关部分:
谢谢:-)要使用Greasemonkey的GM
函数,请执行以下操作:
让页面范围代码使用postMessage
以字符串格式发送数据
让Greasemonkey脚本侦听相应的消息,并使用消息数据调用所需的GM
函数
使用JSON安全地将数据打包为字符串
将window.postMessage()
和window.addEventListener(“消息”…
添加到代码中,它将变成:
... ...
//-- 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);
window.addEventListener ("message", receiveTimeMessage, false);
}
function activateTimeButton () {
var timeBtn = document.getElementById ("gmTimeBtn");
if (timeBtn) {
timeBtn.addEventListener ('click',
function () {
var ytplayer = document.getElementById ("movie_player");
/*-- GM_functions will not work here, so send the data
back to the GM script scope.
*/
//-- Tag the message, we may not be the only ones sending.
var messageTxt = JSON.stringify (
{currentVidTime: ytplayer.getCurrentTime ()}
);
window.postMessage (messageTxt, "*");
},
false
);
}
else {
alert ("Time button not found!");
}
}
function receiveTimeMessage (event) {
var messageJSON;
try {
messageJSON = JSON.parse (event.data);
}
catch (zError) {
// Do nothing
}
if ( ! messageJSON || ! messageJSON.currentVidTime)
return; //-- Message is not for us.
/*--- We have a time value, set it with GM_setValue ()
But, WARNING: First make sure that the stored value is
a safe string. GM_setValue() crashes on just about anything else.
*/
var safeValue = JSON.stringify (messageJSON.currentVidTime);
GM_setValue ("videoMarkedTime", safeValue);
console.log ("Video time recorded with GM_setValue ().");
}
... ...
通过打开about:config
并搜索videoMarkedTime
,可以看到存储的值我知道我告诉过你要问这个问题,但堆栈溢出不是(正常情况下)代码编写服务。你需要以其他人可能会发现对他们的问题有用的方式表达问题。你还应该展示你所做的尝试,而不仅仅是向我们吐出最后一个答案并要求提供其他功能……这个问题有被关闭为“太本地化”的危险(被其他人,而不是我)。请记住这一点。如果没有人比我强,我明天会回答。我理解,但我对JavaScript知之甚少。而且我是法国人,英语说得很少,很难表达自己的想法。我希望对问题进行重构,使其更具普遍适用性。没关系,它很有效!谢谢你的支持efactoring,这样更好。:)你知道在Chrome的什么地方可以看到关于:config的?我试着往里面看,但什么也没发现。。。我们真的需要重新字符串化、解析和字符串化吗?我只是将event.data
作为对象发送,并将字符串化到gmu setValue
。伟大的片段,顺便说一句;通用汽车的价值观是可怕的,因为他们是跨领域的,再次感谢about:config
是Firefox的东西。看见是的,在大多数这样做的情况下,您需要stringify、parse和stringify。
... ...
//-- 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);
window.addEventListener ("message", receiveTimeMessage, false);
}
function activateTimeButton () {
var timeBtn = document.getElementById ("gmTimeBtn");
if (timeBtn) {
timeBtn.addEventListener ('click',
function () {
var ytplayer = document.getElementById ("movie_player");
/*-- GM_functions will not work here, so send the data
back to the GM script scope.
*/
//-- Tag the message, we may not be the only ones sending.
var messageTxt = JSON.stringify (
{currentVidTime: ytplayer.getCurrentTime ()}
);
window.postMessage (messageTxt, "*");
},
false
);
}
else {
alert ("Time button not found!");
}
}
function receiveTimeMessage (event) {
var messageJSON;
try {
messageJSON = JSON.parse (event.data);
}
catch (zError) {
// Do nothing
}
if ( ! messageJSON || ! messageJSON.currentVidTime)
return; //-- Message is not for us.
/*--- We have a time value, set it with GM_setValue ()
But, WARNING: First make sure that the stored value is
a safe string. GM_setValue() crashes on just about anything else.
*/
var safeValue = JSON.stringify (messageJSON.currentVidTime);
GM_setValue ("videoMarkedTime", safeValue);
console.log ("Video time recorded with GM_setValue ().");
}
... ...