gpt4 book ai didi

javascript - GM_setvalue 不是每次都携带值,只有一些时候......需要帮助

转载 作者:行者123 更新时间:2023-11-29 18:32:18 27 4
gpt4 key购买 nike

我目前正在尝试在页面刷新时在 GM 脚本中传递变量。基本上我使用的脚本来自 "Using Greasemonkey and jQuery to intercept JSON/AJAX data from a page, and process it." -- 我已经使用并添加了很多。

我已经挑选出一些变量,并希望在页面刷新时将它们带走,但它们没有。每次刷新时它都会将变量重置为 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);
}
}
});
}


GM_setvalue 未保留变量“my_test”,但在 json 数组中运行的“my_test2”以及我的其他变量未保留。我不确定为什么,但这是我能够将范围缩小到的范围。

最佳答案

一些事情:

  1. 那些脚本试图存储 float 。 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 库。将此行添加到您的脚本中:

    // @require http://userscripts.org/scripts/source/107941.user.js
  3. 将所有GM_setValue替换为GM_SuperValue.set

  4. 将所有GM_getValue替换为GM_SuperValue.get

  5. 为了解决 GM 不会让 GM_setValue 在 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;}');

关于javascript - GM_setvalue 不是每次都携带值,只有一些时候......需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6802750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com