我正在嘗試在頁面刷新時在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
結轉。我不確定爲什麼,但這是我能夠縮小到的範圍。
如果您使用的是相同的GM腳本來設置和檢索值,那麼它應該工作,從未有過任何麻煩自己。 – Jonathon
re:'「腳本太長了,不能粘貼整個腳本來處理這個問題。」......顯示的代碼看起來不錯,我們需要看到整個腳本,並可能在網站上測試它(我記得因爲有點「毛茸茸的」)。請鏈接到pastebin.com上的完整腳本。 –
在Firefox地址欄中轉到*** about:config ***(您正在使用FF而不是Chrome?)。過濾'greasemonkey.scriptvals'。你看到了什麼? (附加到問題。) –