Ludwig
Ludwig

Reputation: 161

How to remember variables with Greasemonkey script when a page reloads

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?

Upvotes: 1

Views: 1615

Answers (3)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

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)

Upvotes: 2

Developer Guy
Developer Guy

Reputation: 11

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...

Upvotes: 1

wimh
wimh

Reputation: 15232

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...

Upvotes: 0

Related Questions