2011-07-23 53 views
1

我正在嘗試在頁面刷新時在GM腳本中攜帶變量。基本上我使用"Using Greasemonkey and jQuery to intercept JSON/AJAX data from a page, and process it."這個腳本 - 我已經使用了它,並且已經添加了很多。GM_setvalue不會每次都帶值,只需要一些時間......需要幫助

我已經指出了一些變量,並希望在頁面刷新時執行它們,但它們不會。它會在每次刷新時將變量重置爲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結轉。我不確定爲什麼,但這是我能夠縮小到的範圍。

+0

如果您使用的是相同的GM腳本來設置和檢索值,那麼它應該工作,從未有過任何麻煩自己。 – Jonathon

+0

re:'「腳本太長了,不能粘貼整個腳本來處理這個問題。」......顯示的代碼看起來不錯,我們需要看到整個腳本,並可能在網站上測試它(我記得因爲有點「毛茸茸的」)。請鏈接到pastebin.com上的完整腳本。 –

+0

在Firefox地址欄中轉到*** about:config ***(您正在使用FF而不是Chrome?)。過濾'greasemonkey.scriptvals'。你看到了什麼? (附加到問題。) –

回答

3

有幾件事情:

  1. 這些腳本試圖存儲浮動。 GM_setValue() only works on: strings, integers and booleans
    幸運的是,這有一個擴展;更多下面。

  2. 稍後調用GM_setValue失敗,因爲它們在事件處理程序中。
    如果你已經看與Firebug console(總是調試方式!),一個紅色的錯誤信息滾動經過:

    Greasemonkey access violation: unsafeWindow cannot call GM_setValue. 
    
  3. 在此類似,不與alerts()煩人的測試?使用console functions,腳本不必停下來,而且不會有那些討厭的彈出窗口。

那麼,如何解決

  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庫。該行添加到您的腳本(S):

    // @require http://userscripts.org/scripts/source/107941.user.js 
    
  3. 替換所有GM_setValueGM_SuperValue.set

  4. 替換所有GM_getValueGM_SuperValue.get

  5. 爲了解決這樣的事實,通用汽車不會讓GM_setValue運行在事件處理程序從GM範圍設置(這可能是一個bug),改變方式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;}'); 
+0

@ brock,謝謝,它現在在about:config中顯示值,只是一個小錯誤,有時會多次運行「if(a_tst2)」部分,實際上很多很多次,在腳本中我都會注意到它有時會發出警報2或3次,但在你的版本似乎放大..認爲「如果(a_tst2)」在右括號的錯誤一側將只是糾正它添加另一個如果或兩個,只想存儲一個值時,其中之一auctios去,或計時器運行到「0」。我今晚會處理它並修復它......無需擔心......再次感謝.. :) – Ludwig