0

I am writing a simple clickbot in JavaScript to perform repetitive tasks on a 3rd party website. It just sets input values, calls the click() method of buttons and maybe I'll have it navigate to other URLs of the same website. I initially used Firefox but got the same behaviour with Internet Explorer.

I use plain JS so far and as long as I paste in every command by myself everything works fine but here's the problem: Any time a new page is loaded (including when JS clicks a submit button) I lose all vars and functions I defined. Note that I use the web console as opposed to a <script> tag that obyiousely would be dropped when a new DOM is loaded.

Honestly, I do not entirely understand why this happens. I looked at JavaScript scope and the documentation of window.location and document.location. This site even mentioned that "In a web browser, global variables are deleted when you close the browser window (or tab), but remain available to new pages loaded into the same window." (cf. here, but that's not the point here)

I thought it might be because strict mode might be enabled by default but that would have raised an error for name = "value" instead of silently interpreting it as a local variable.

According to this answer, any var declared outside any methods should be globals that are properties of window. Changing another propertie of window (e.g. location) should not affect them - as far as my reasoning goes - but even when assigning properties myself, they disappear when I load another page.

I suppose it could be worked around if I wrote my own website that has the site I need as an iFrame so the page my script runs on would not actually be changed. But still this is strange. Can anyone explain this behaviour? Is there another (easy) way around it?

[Edit1] Thanks to same-origine policy my proposed workaround using an own website and iframe does not work. Since the whole point of my clickbot is to be started once and click through all pages on it's own, a way to carry on data (i.e. strings including JSON) is answering this question but does not solve my problem.

[Edit2] For future visitors: After recent edits, the accepted answer does provide all information I needed. Greasemonkey is the way to go if you can use addons but the combination of bookmarklets and sessionStorage stil allow for a decent bot that performes all steps between two reloads with just one user input. Another (ugly bot possibly more powerfull) approach is to open the target website and use document.body.innerHTML and the iframe workaround. That way you bypass the same-origine policy and can build your own website as needed and still access the target website.

hajef
  • 334
  • 2
  • 13
  • 1
    basically: W3Schools is wrong. – Matt Ellen May 20 '19 at 13:55
  • @MattEllen: The answer to that question is just the starting point for mine. The only similarity is my subsidiary question "Can anyone explain this behaviour?" that is neither entirely answered thare nor the core of my question. "How long do variables live?" != "With aproach a, variables live this long (which is odd considering ...). How can I make them live that long so I can use them for case x?" – hajef May 20 '19 at 14:07

1 Answers1

1

Regarding the lifetime of a variable this SO question goes into detail, but variables don't persist across reloads.

If you're staying within the same domain (e.g. everything happens at https://example.com, so https://example.com/page1, https://example.com/page2, etc.) then you can use cookies or local storage to keep track of values.

You can't keep track of functions easily with local storage or cookies.

Cookies and local storage are not available across domains.

I would use local storage as that is easier to interface with. So instead of:

var someVar = 'hello';

Write

localStorage['someVar'] = 'hello';

Then to use the variable:

console.log(localStorage['someVar']);

Local storage has a maximum size of 10MB. Probably enough for any automation script.

If you want to persist functions across page reloads, you should use something like grease monkey to store your user scripts.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • I'd like to avoid using cookies since I'v never worked with them before, only need the data at client-side and don't want to expose them more then I have to. Local storage on the other hand sounds like it might be just what I need to make my script run. – hajef May 20 '19 at 14:48