GM_setvalue 不是每次都携带值,只是有时......需要帮助
我目前正在尝试在页面刷新时在 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
继承。我不确定为什么,但这就是我能够缩小范围的范围。
I'm currently trying to carry variables over in a GM script when a page refreshes. Basically I'm using the script from "Using Greasemonkey and jQuery to intercept JSON/AJAX data from a page, and process it." -- which I used and added to quite a bit already.
I've singled out some of the variables, and would like to carry them over when the page refreshes, but they don't. It resets the variables to 0 every time it refreshes, and doesn't carry over.
This is basically what I've got...or rather the important pieces, the script is too getting too long to paste the whole script for this question.
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'....
*/
Can anyone please just show me where I might be going wrong?
Update:
Right.. here is a shortened version of the script that gives me trouble, still with the same problems:
// ==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);
}
}
});
}
The variable "my_test" is carried over, but "my_test2", which run in the json array, as well as my other variables isn't carried over by GM_setvalue
. I'm unsure why, but this is to what I was able to narrow it down to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些事情:
这些脚本正在尝试存储浮点数。
GM_setValue()
仅适用于:字符串、整数和布尔值。幸运的是,有一个扩展;更多内容见下文。
后来对 GM_setValue 的调用失败,因为它们位于事件处理程序内。
如果您一直在使用 Firebug 控制台(始终以这种方式调试!),红色错误消息滚动过去:
同样,使用
alerts()
进行测试不是很烦人吗?使用控制台功能,脚本就不必停止,并且不会出现那些讨厌的弹出窗口。那么,如何修复:
首先进行测试。
安装此脚本:
然后导航到此页面 (stackoverflow.com/q/6802750/)。
Firebug 控制台打开后,重新加载页面。
结果如何?
使用增强的 GM_setValue 库。 将此行添加到您的脚本中:
将所有
GM_setValue
替换为GM_SuperValue.set
将所有
GM_getValue
替换为GM_SuperValue.get
要解决 GM 不会让
GM_setValue
在 GM 作用域设置的事件处理程序中运行的问题(这可能是一个错误),请更改ParseJSON_Data
的调用方式...J_DataCell.bind ('DOMSubtreeModified' ...
行。timerHandle = setInterval (function() { ParseJSON_Data (); }, 444);
。J_DataCell
添加更多逻辑。,测试脚本将变为:
A few things:
Those scripts are trying to store floats.
GM_setValue()
only works on: strings, integers and booleans.Fortunately, there is an extension for that; more below.
The later calls to GM_setValue failed because they were inside an event handler.
If you had been watching with the Firebug console (always debug that way!), a red error message scrolls past:
On a similar vein, isn't testing with the
alerts()
annoying? Use the console functions and the script won't have to stop, and you won't have those pesky popups.So, how to fix:
First, a test.
Install this script:
Then navigate to this page (stackoverflow.com/q/6802750/).
With Firebug's console open, reload the page.
What are the results?
Use the enhanced GM_setValue library. Add this line to your script(s):
Replace all
GM_setValue
withGM_SuperValue.set
Replace all
GM_getValue
withGM_SuperValue.get
To address the fact that GM won't let
GM_setValue
run in event handlers set from the GM scope (this may be a bug), change the wayParseJSON_Data
is called...J_DataCell.bind ('DOMSubtreeModified' ...
line.timerHandle = setInterval (function() { ParseJSON_Data (); }, 444);
, below it.J_DataCell
.Putting it all together, the test script becomes: