Retrieve value from localStorage if previous page has specific string

Hi, i have saved two protected page, page1 and page2. I would like to set “R.D.” on field “Sezione” (selector #cdSezi) of page2 if page1 contains the string “Letteratura per ragazzi” beside “Generi”. Otherwise if page one (see here) doesn’t contain “Letteratura per ragazzi” beside “Generi” then field “Sezione” should be set to “A”.

I tried this userscript, but it gives me always “R.D.” on field “Sezione” of page2, even when the string “Letteratura per ragazzi” is not present in page1. Thanks very much!

Hi, does the user have to have visited page1 before viewing page2? Or can the user access page2 directly?

Yes, the user has to visit page1 before viewing page2

In your userscript, there is no mechanism for clearing localstorage. Could this be why once the string “Letteratura per ragazzi” is present in page1, you will always get “R.D.” on page2?

if (conditionElement && conditionElement.textContent.trim() === "Letteratura per ragazzi") {
  // As before
} else {
  // Add this
  localStorage.removeItem("cdSeziValue");
}

Yes, maybe the problem is the local storage, if I delete data on browser after “R.D.” has been set, then it sets correctly #cdSezi to “A.” if the string “Letteratura per ragazzi” is missing. I tried your suggestion, but with your modifications it gives me always “A.”. Do you mean this?

   if (conditionElement && conditionElement.textContent.trim() === "Letteratura per ragazzi") {
        // Set the value in local storage only if the condition is met
        localStorage.setItem("cdSeziValue", "R.D.");
        cdSeziValue = "R.D.";
    } else {
        // Set cdSeziValue to "A." if the condition is not met
        localStorage.removeItem("cdSeziValue");
        cdSeziValue = "A.";
    }

Yeah, that’s what I meant. If it’s not working, I would remove some of the pieces and simplify the problem. For example on page1 have your userscript pop up an alert depending on whether the initial value is present. Once that is working, have it save something to local storage (e.g. true or false) depending on the presence of the value. Once the correct value is being saved to local storage, move on to page2…

With this script the pop-up alert correctly opens when there is the string “Letteratura per ragazzi” and doesn’t when it’s not present… Now I don’t know how to go on…

 if (conditionElement && conditionElement.textContent.trim() === "Letteratura per ragazzi") {
        // Set the value in local storage only if the condition is met
        localStorage.setItem("cdSeziValue", "R.D.");
        cdSeziValue = "R.D.";

        // Pop up an alert if the condition is met
        alert("Condition 'Letteratura per ragazzi' is met!");
    } else {
        // Set cdSeziValue to "A." if the condition is not met
        localStorage.removeItem("cdSeziValue");
        cdSeziValue = "A.";

Is there any way of identifying page1 and page2?

It would help to do something like this:

if (page1) {
  // Work out what to chuck into local storage here
} else if (page2) {
  // See what's in local storage and populate the field accordingly
}

Are there any elements that are unique to either page? Or could we use the URL in some way?

Are you sure your script is not executed before the whole page is loaded?

Page1 and page2 have the same URL. In page1, for example, there is always the string “Risorsa”, in page2 the string “Inserimento nuovo inventario collocato”, if this is the question…

If I let the script to be preceded by:

document.addEventListener("DOMContentLoaded", function() {

the field #cdSezi isn’t populated at all

I don’t know how to identify pageOne and pageTwo, but I think this would be the right way. Infact if “Lettereatura per ragazzi” is present in pageOne I get correctly on console:

Condition Element: <span class=​"no-grid">​ Letteratura per ragazzi ​</span>​
cdSeziValue: R.D.
Stored cdSeziValue: R.D.

But when I am on pageTwo I get on console:

Condition Element: null
cdSeziValue: A.
Stored cdSeziValue: null

So since the element with selector #cdGenerePbb1 > span.no-grid does not exist on the pageTwo, conditionElement will be null, leading incorrectly to the “A.” value, even if “Lettereatura per ragazzi” is present in pageOne.

This works on the test pages you provided in your first post.

(function() {
  'use strict';

  const isPage1 = document.body.textContent.indexOf('Risorsa') > -1;
  const isPage2 = document.body.textContent.indexOf('Inserimento nuovo inventario collocato') > -1;

  if (isPage1) {
    const el = document.querySelector("#cdGenerePbb1 > span.no-grid");

    if (el && el.textContent.trim() === 'Letteratura per ragazzi') {
      localStorage.setItem('cdSeziValue', 'R.D.');
    } else {
      localStorage.setItem('cdSeziValue', 'A');
    }
  } else if (isPage2){
    document.querySelector('#cdSezi').value = localStorage.getItem('cdSeziValue');
    localStorage.removeItem('cdSeziValue');
  } else {
    // Neither page1, nor page2. Do something here?
  }
})();
1 Like

Wao, don’t believe it, thank you for the excellent work, I really appreciate your efforts!!!

1 Like

No worries. Glad you got it working.

1 Like

Hopefully this page will never be translated :slight_smile: