5

私は質問をし、ここで答えを得ました: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!");
    }
}

... ...


ありがとうございました :-)

4

1 に答える 1

9

ページスコープ(クリックハンドラーなど)でGM_実行する必要のあるコードからGreasemonkeyの関数を使用するには、次の手順を実行します。timeBtn

  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

于 2013-01-10T10:14:26.233 に答える