0

I have a simple Javascript code I want to run once per browser session, in a given website.

To clarify:

  1. I open a browser session (window), say, Firefox.
  2. I go to a certain webpage matched in Greasemonkey (say, google.com).
  3. My script runs.
  4. I fully refresh the wepbage (CTRL+F5), and the script no longer runs because it runs only once per session.

How can this be done? I mean, how can a simple script consisted only of one function, could be limited to run only once in a browser session, in a particular website matched in Greasemonkey?

J. Doe
  • 25
  • 5
  • Are the `DOMContentLoaded` event handlers currently dispatched more than once? What is purpose of calling `document.querySelector(".header-nav-item-sign-in").click()` and `document.querySelector("[type=submit]").click()` within `DOMContentLoaded` event handlers? – guest271314 Jul 16 '17 at 22:51
  • One takes you to the sign in page, one clicks submit for details already saved. – J. Doe Jul 16 '17 at 23:02

2 Answers2

0

You can use LocalStorage to do that. I have the whole logic explained in the comments.

if (typeof(Storage) !== "undefined") {
  // First timer
  if (!localStorage.getItem("oldUser")) {
    localStorage.setItem("oldUser", true);
    executeYourCode();
  }
} else {
  // Sorry! No Web Storage support..
  // Use the above cookie method.
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Use window.localStorage to set a parameter isCalled = true

Here docs https://developer.mozilla.org/en/docs/Web/API/Window/localStorage