gpt4 book ai didi

javascript - 如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?

转载 作者:行者123 更新时间:2023-11-28 07:30:51 26 4
gpt4 key购买 nike

我提出了一个问题并在这里得到了答案:How to call this YouTube function from Greasemonkey?

该代码有效,并向页面添加一个按钮,用于捕获视频时间。
但是,关键部分必须在目标页面范围内运行 - Greasemonkey 的 GM_ 功能在该范围内不可用。

我想使用GM_setValue()来记录视频时间。如何从按钮的 click 处理程序调用 GM_setValue()

这是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!");
}
}

... ...


谢谢:-)

最佳答案

要在必须在页面范围内运行的代码(例如 timeBtn 点击处理程序)中使用 Greasemonkey 的 GM_ 函数,请执行以下操作:

  1. 让页面范围代码使用 postMessage 以字符串格式发送数据。
  2. 让 Greasemonkey 脚本监听适当的消息并使用消息数据调用所需的 GM_ 函数。
  3. 使用 JSON 将数据安全地打包在字符串中。

在代码中添加window.postMessage()window.addEventListener("message"...到你的代码中,它就变成了:

... ...

//-- 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 - 如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com