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

Rom*_*ric 5 javascript youtube greasemonkey youtube-api

我问了一个问题并在这里得到了答案:如何从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!");
    }
}

... ...
Run Code Online (Sandbox Code Playgroud)


谢谢 :-)

Bro*_*ams 9

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

  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 ().");
}

... ...
Run Code Online (Sandbox Code Playgroud)


您可以通过打开about:config和搜索来查看存储的值videoMarkedTime.