2

現在、ページが更新されたときにGMスクリプトで変数を引き継ごうとしています。基本的に、「GreasemonkeyとjQueryを使用してページからJSON / AJAXデータをインターセプトし、それを処理する」のスクリプトを使用しています。--これを使用して、すでにかなり追加しました。

いくつかの変数を選び出し、ページが更新されたときにそれらを引き継ぎたいのですが、そうではありません。更新するたびに変数を0にリセットし、繰り越しません。

これは基本的に私が持っているものです...というより重要な部分ですが、スクリプトが長すぎて、この質問のスクリプト全体を貼り付けることができません。

var A12_old1 = GM_getValue('A12_old1', 0);
var A12_old2 = GM_getValue('A12_old2', 0);
var A12_old3 = GM_getValue('A12_old3', 0);

//then further on...
A12_current = parseFloat(singleAuctionData[8]);
A12_rest = singleAuctionData[1];
if (t_int < 1) {
    if (t_test) {
        alert_test = true;
        t_test = false;
        A12reset_go = true;
        A12_old3 = A12_old2;
        A12_old2 = A12_old1;
        A12_old1 = A12_current;
    }
}

/* so basically doing some calculations as to what the values should be then to
     carry them over, at almost the end of the script, but still running every
     second, there is:
*/
if (alert_test) {
    alert_test = false;
    alert(A12_old1 + '  ' + A12_old2 + '  ' + A12_old3);
}

GM_setValue('A12_old1', A12_old1);
GM_setValue('A12_old2', A12_old2);
GM_setValue('A12_old3', A12_old3);
}

/*but it isn't carrying the 3 values over when the page refreshes.
    It resets to '0'....
*/

誰かが私がどこで間違っているのか教えてくれませんか?

アップデート:

そうです..これは私に問題を与えるスクリプトの短縮版ですが、それでも同じ問題があります:

// ==UserScript==
// @name            setvalue test
// @include         http://www.trada.net/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==
var auctiontyp = 0;
var my_test = GM_getValue("tsttst", 0);
var my_test2 = GM_getValue("tsttst2", 0);
var h = 0;
var m = 0;
var s = 0;
var t_int = 0;
var t_str = '';
var A12_current = 0;
var a_tst = true;
var a_tst2 = true;
var A12_old1 = GM_getValue("A12_old1", 0);
var A12_old2 = GM_getValue("A12_old2", 0);
var A12_old3 = GM_getValue("A12_old3", 0);

if (a_tst) {
    alert(my_test);
    GM_setValue("tsttst", 5);
    a_tst = false;
}
//--- Create a cell for transmitting the date from page scope to GM scope.
$('body').prepend('<div id="LatestJSON_Data"></div>');

var J_DataCell = $('#LatestJSON_Data');

//--- Evesdrop on the page's AJAX calls and paste the data into our special div.
unsafeWindow.$('body').ajaxSuccess(

function (event, requestData) {
    J_DataCell.text(requestData.responseText);
} );

//--- Listen for changes to the special div and parse the data.
J_DataCell.bind('DOMSubtreeModified', ParseJSON_Data);

function ParseJSON_Data() {
    //--- Get the latest data from the special cell and parse it.
    var myJson = J_DataCell.text();
    var jsonObj = $.parseJSON(myJson);

    //--- The JSON should return a 2-D array, named "d".
    var AuctionDataArray = jsonObj.d;

    //--- Loop over each row in the array.
    $.each(AuctionDataArray, function (rowIndex, singleAuctionData) {

        //--- Print the 7th column.
        console.log('Row: ' + (parseInt(rowIndex) + 1) + ' Column: 7  Value: ' + singleAuctionData[6]);

        if (a_tst2) {
            alert(my_test2);
            GM_setValue("tsttst2", 15);

            alert(A12_old1 + '  ' + A12_old2 + '  ' + A12_old3);
            a_tst2 = false;
        }

        t_str = singleAuctionData[10];
        var time = t_str.split(":");
        h = 3600 * parseInt(time[0], 10);
        m = 60 * parseInt(time[1], 10);
        s = parseInt(time[2], 10);
        t_int = h + m + s;

        auctiontyp = parseInt(singleAuctionData[4]);
        if (auctiontyp == 4) {
            A12_current = parseFloat(singleAuctionData[8]);

            if (t_int < 1) {
                A12_old3 = A12_old2;
                A12_old2 = A12_old1;
                A12_old1 = A12_current;
                GM_setValue("A12_old1", A12_old1);
                GM_setValue("A12_old2", A12_old2);
                GM_setValue("A12_old3", A12_old3);
            }
        }
    });
}


変数「my_test」は引き継がれますが、json配列で実行される「my_test2」と他の変数は引き継がれませんGM_setvalue。理由はわかりませんが、これが私が絞り込めたものです。

4

1 に答える 1

4

いくつかのこと:

  1. これらのスクリプトは float を格納しようとしています。 GM_setValue()のみで動作します: 文字列、整数、およびブール値
    幸いなことに、そのための拡張機能があります。詳細は以下をご覧ください。

  2. その後の GM_setValue の呼び出しは、イベント ハンドラー内にあったため失敗しました。Firebug コンソール
    で監視していた場合(常にそのようにデバッグしてください!)、赤いエラー メッセージがスクロールして過ぎていきます。

    Greasemonkey access violation: unsafeWindow cannot call GM_setValue.
    
  3. alerts()同様に、煩わしいテストではありませんか? コンソール機能を使用すると、スクリプトを停止する必要がなくなり、厄介なポップアップが表示されることもありません。

したがって、修正方法

  1. まず、テスト。

    1. このスクリプトをインストールします。

      // ==UserScript==
      // @name            Super GM_setValue and GM_getValue TEST SHELL
      // @namespace       DEBUG
      // @include         https://stackoverflow.com/questions/*
      // @require         http://userscripts.org/scripts/source/107941.user.js
      // ==/UserScript==
      
      /*--- Run the test cases to make sure that the GM_setValue and GM_getValue
          extensions are able to run on this browser.
      */
      GM_SuperValue.runTestCases  (0);
      
    2. 次に、このページ ( stackoverflow.com/q/6802750/ ) に移動します。

    3. Firebug のコンソールを開いた状態で、ページをリロードします。

    4. 結果は?


  2. 強化された GM_setValue ライブラリを使用します。次の行をスクリプトに追加します。

    // @require http://userscripts.org/scripts/source/107941.user.js
    
  3. すべてGM_setValueGM_SuperValue.set

  4. すべてGM_getValueGM_SuperValue.get

  5. GM_setValueGM がGM スコープから設定されたイベント ハンドラーで実行できないという事実に対処するには(これはバグである可能性があります)、ParseJSON_Data呼び出される方法を変更します...

    1. 行をコメントアウトしJ_DataCell.bind ('DOMSubtreeModified' ...ます。
    2. その下に、を追加timerHandle = setInterval (function() { ParseJSON_Data (); }, 444);します。
    3. の周りにさらにロジックを追加しJ_DataCellます。

すべてをまとめると、テスト スクリプトは次のようになります。

// ==UserScript==
// @name            _setvalue test
// @include         http://www.trada.net/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// @require         http://userscripts.org/scripts/source/107941.user.js
// ==/UserScript==

var auctiontyp  = 0;
var my_test     = GM_SuperValue.get("tsttst", 0);
var my_test2    = GM_SuperValue.get("tsttst2", 0);
var h           = 0;
var m           = 0;
var s           = 0;
var t_int       = 0;
var t_str       = '';
var A12_current = 0;
var a_tst       = true;
var a_tst2      = true;
var A12_old1    = GM_SuperValue.get("A12_old1", 0);
var A12_old2    = GM_SuperValue.get("A12_old2", 0);
var A12_old3    = GM_SuperValue.get("A12_old3", 0);

if (a_tst) {
    console.log(my_test);
    GM_SuperValue.set("tsttst", 5);
    a_tst = false;
}
//--- Create a cell for transmitting the date from page scope to GM scope.
$('body').prepend('<div id="LatestJSON_Data"></div>');

var J_DataCell = $('#LatestJSON_Data');

//--- Evesdrop on the page's AJAX calls and paste the data into our special div.
unsafeWindow.$('body').ajaxSuccess(
    function (event, requestData) {
        J_DataCell.text(requestData.responseText);
} );

//--- Listen for changes to the special div and parse the data.
//J_DataCell.bind ('DOMSubtreeModified', {StoreValFunc: GM_SuperValue.set}, ParseJSON_Data);

timerHandle = setInterval (function() { ParseJSON_Data (); }, 444);

function ParseJSON_Data () {
    //--- Get the latest data from the special cell and parse it.
    var myJson  = J_DataCell.text();
    if (!myJson  ||  /^\s*$/.test (myJson) )
        return
    else
        J_DataCell.text (" ");

    var jsonObj = $.parseJSON(myJson);

    //--- The JSON should return a 2-D array, named "d".
    var AuctionDataArray = jsonObj.d;

    //--- Loop over each row in the array.
    $.each(AuctionDataArray, function (rowIndex, singleAuctionData) {

        //--- Print the 7th column.
        //console.log('Row: ' + (parseInt(rowIndex) + 1) + ' Column: 7  Value: ' + singleAuctionData[6]);

        if (a_tst2) {
            console.log('******** ', my_test2);
            GM_SuperValue.set ("tsttst2", 15);

            console.log (A12_old1 + '  ' + A12_old2 + '  ' + A12_old3);
            a_tst2 = false;
        }

        t_str       = singleAuctionData[10];
        var time    = t_str.split(":");
        h           = 3600 * parseInt(time[0], 10);
        m           = 60 * parseInt(time[1], 10);
        s           = parseInt(time[2], 10);
        t_int       = h + m + s;

        auctiontyp = parseInt(singleAuctionData[4]);
        if (auctiontyp == 4) {
            A12_current = parseFloat(singleAuctionData[8]);

            if (t_int < 1) {
                A12_old3 = A12_old2;
                A12_old2 = A12_old1;
                A12_old1 = A12_current;
                GM_SuperValue.set ("A12_old1", A12_old1);
                GM_SuperValue.set ("A12_old2", A12_old2);
                GM_SuperValue.set ("A12_old3", A12_old2);
            }
        }
    });
}

GM_addStyle ('#LatestJSON_Data {display:none;}');
于 2011-07-25T14:23:07.033 に答える