页面重新加载时如何使用 Greasemonkey 脚本记住变量

发布于 2024-11-15 08:52:57 字数 959 浏览 1 评论 0原文

我当前在直接在电脑的 Firefox 浏览器中运行的移动网站上遇到问题。每次单击按钮时,页面都会重新加载,从而重置我的变量。我有这个脚本:

// ==UserScript==
// @name        trada.net autoclick 55_1min_mobile
// @namespace   airtimeauction auto click
// @include http://www.trada.net/Mobile/
// @version     0.1
// @description Automatically click // ==/UserScript==

var interval            = 57000;
var bidClickTimer       = setInterval (function() {BidClick (); }, interval);
var numBidClicks        = 0;

function BidClick ()
{var bidBtn1=document.getElementById("ctl00_mainContentPlaceholder_AirtimeAuctionItem7_btn_bidNow");







    numBidClicks++;
    if (numBidClicks > 500)
    {
        clearInterval (bidClickTimer);
        bidClickTimer   = "";
    }
    else
    {
        bidBtn1.click (1);

    }
};



BidClick();

它应该每 57 秒单击一次按钮,但单击按钮时,页面会重新加载,从而重置变量。我怎样才能让greasemonkey“记住”或在重新加载时将变量转移到下一页/脚本?会不会和GM_setValue有关系?这只是这几个变量,但第二个问题或问题将是,它会从“57”秒中减去页面重新加载所需的几秒吗?我该如何补偿?

Ive got an problem currently on an mobile site that i'm running directly in my pc's firefox browser. Everytime a button is clicked, the page reloads, thus resetting my variables. I've got this script:

// ==UserScript==
// @name        trada.net autoclick 55_1min_mobile
// @namespace   airtimeauction auto click
// @include http://www.trada.net/Mobile/
// @version     0.1
// @description Automatically click // ==/UserScript==

var interval            = 57000;
var bidClickTimer       = setInterval (function() {BidClick (); }, interval);
var numBidClicks        = 0;

function BidClick ()
{var bidBtn1=document.getElementById("ctl00_mainContentPlaceholder_AirtimeAuctionItem7_btn_bidNow");







    numBidClicks++;
    if (numBidClicks > 500)
    {
        clearInterval (bidClickTimer);
        bidClickTimer   = "";
    }
    else
    {
        bidBtn1.click (1);

    }
};



BidClick();

It should click the button every 57 seconds, but the moment it clicks the button, the page reloads, thus resetting the variables. How can i get greasemonkey to "remember" or carry over the variables to the next page/script when it reloads? Will it have something to do with GM_setValue? It will only be this few variables, but the second problem or question wil be, will it subtract the few seconds it takes the page to reload from the "57" seconds? How do i compensate for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-11-22 08:52:57

除了 GM_setValue...
您还可以使用新的 Javascript“localStorage”对象或 SQL Javascript API。

SQL 方法的优点是它在脚本中的资源消耗非常少(想一想;您可以隐藏每个结果,并在需要时通过精确查询调用它,而不是连接一个巨大的结果字符串。你必须设置一个 SQL 服务器,但使用像 SQLite 这样的东西现在已经不是什么大问题了,甚至 postgres 或 mysql 也可以在笔记本电脑上快速运行......

In addition to GM_setValue...
you also can use the new Javascript "localStorage" object, or a SQL Javascript API.

The advantage of the SQL approach is it is very meager in its resource consumption in a script (think about it; rather than concatenating a humongous string of results, you can tuck away each result and recall it if needed with a precise query. The downside is you have to set up a SQL server, but using something like SQLite that's not a big deal these days. Even postgres or mysql can be quickly spun on a laptop...

半城柳色半声笛 2024-11-22 08:52:57

GM.setValue 将无限期地设置一个值,并且作用范围为脚本,但如果您的脚本跨多个域运行,则该脚本将起作用。

window.localStorage将无限期地设置一个值,并且范围仅限于页面的域,因此不能跨域工作,但如果您需要多个 GreaseMonkey 脚本来访问相同的值,则可以工作。

window.sessionStorage仅当窗口或选项卡打开时才设置一个值,并且范围仅限于该域的该窗口或选项卡。

document.cookie可以无限期地或仅在浏览器打开时设置值,并且范围可以跨子域、单个域、路径或单个页面。

这些是用于跨页面加载存储值的主要客户端机制,旨在实现此目的。然而,还有另一种方法有时是可行的(如果页面本身没有使用它),并且也非常有用; 窗口名称

window.name 的范围仅限于窗口或选项卡,但也可以跨域工作。如果您需要存储多个值,那么可以将它们放入一个对象中,然后您可以存储该对象的 JSON 字符串。例如window.name = JSON.stringify(obj)

GM.setValue will set a value indefinitely and is scoped to the script, but will work if your script runs across multiple domains.

window.localStorage will set a value indefinitely and is scoped to the domain of the page, so will not work across domains, but will work if you need several GreaseMonkey scripts to access the same value.

window.sessionStorage will set a value only while the window or tab is open and is scoped to only that window or tab for that domain.

document.cookie can set a value indefinitely or only while the browser is open, and can be scoped across subdomains, or a single domain, or a path, or a single page.

Those are the main client-side mechanisms for storing values across page loads that are intended for this purpose. However, there is another method which is sometimes possible (if the page itself is not using it), and can also be quite useful; window.name.

window.name is scoped to the window or tab, but will work across domains too. If you need to store several values, then they can be put into an object and you can store the object's JSON string. E.g. window.name = JSON.stringify(obj)

梦忆晨望 2024-11-22 08:52:57

是的,我认为你必须使用 GM_setValue/GM_getValue

如果您必须每 57 秒执行一次操作,则计算重新加载后应执行下一个操作的时间,并使用 GM_setValue 存储它。
当你的脚本启动时,首先读取是否存储了下一个动作,如果是,则使用该时间来安排下一个动作,并计算之后的动作的时间,依此类推......

Yes, I think you have to use GM_setValue/GM_getValue.

And if you have to do something exactly every 57 seconds, then calculate the time when the next action should take place after the reload, and store it using GM_setValue.
When your script starts, read first if the next action is stored, if it is, use that time to schedule the next action, and calculate the time for the action after that, and so on...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文