Disable script on condition

Good evening, could you help me, please? From this saved (static) page, page two, I’m running a script that extracts, in “Nome” field, the publisher from the bibliographic item (Feltrinelli, in the example). I would like the script to be NOT executed when on the previous page, page one, there is the expression “Editore moderno” (that is the publisher) under the heading “Persone, enti e famiglie”, that is in “fieldset.legami_tito”).

In Page one I tried to set a local variable if the expression “Editore moderno” is found:

var elencoLabel = document.querySelectorAll("span.grid-6");
    if (elencoLabel.length > 8)
    {
        localStorage.removeItem("voce");
        if (elencoLabel[8].innerText == "Editore moderno")
        {
            localStorage.setItem("voce","Editore moderno");        
        }
    }

In Page two I tried with this script (replacing the last line of the script):

 function getNome(tmp) {

    var idx = tmp.indexOf('. ((');

    if(idx > -1){
    tmp = tmp.substr(0, idx);
    }

    tmp = tmp.split('. - ');

    switch(tmp.length){
    case 3:
    tmp = tmp[1];
    break;
    case 4:
    tmp = tmp[2];
    break;
    default:
    tmp = "";
    break;
    }

    if(tmp !== ''){
        tmp = tmp.substr(tmp.indexOf(' : ') + 2);
        console.log(tmp);
        if(tmp.indexOf('.') != -1 && tmp.split('.').length == 2){
            tmp = tmp.substr(tmp.indexOf('. ') + 1, tmp.indexOf(', ') -3);
            tmp = tmp.trim();
        }
        else {
            tmp = tmp.split(",")[0];
            tmp = tmp.trim();
        }
    }
    return tmp;
}
function impostaNome(tmp) {
    Array.from(document.querySelectorAll('article section.grid_container form div.grid-row label span')).filter( e => e.innerText.trim() === 'Nome')[0].parentNode.querySelector('input').value = tmp;
}

var elencoLabel = document.querySelectorAll("span.grid-6");
if (elencoLabel.length < 8 )
{    
    var miaVoce = localStorage.getItem("voce");    
    if (miaVoce == "Editore moderno")
    {        
        impostaNome(getNome(document.querySelector('div.meta.tito div.evidence.isbd').innerText));
    }
}

But with these changes the publisher is never extracted.

It would be easier probably to do something like fetch page 1 from the page 2 script, and find if the words “Editore moderno” appear in the response.

Thanks for the answer, m_hutley, so I tried replacing the last line of the script with:

var elencoLabel = document.querySelector("span.grid-6.label");   
if (elencoLabel.textContent.trim() !== "Editore moderno") {
   impostaNome(getNome(document.querySelector('div.meta.tito div.evidence.isbd').innerText));
}

but didn’t solve.

Well document.querySelector can only find elements on the CURRENT page. It cant query something on a different page.

I dont know what the relationship between the two pages is in the actual deployment, so i’m speculating with your example only at this point.

fetch("page_one.html")
.then((response) => response.text())
.then((text) => {
   if (!text.includes("Editore moderno"))  { 
     impostaNome(getNome(document.querySelector('div.meta.tito div.evidence.isbd').innerText));
   }
});

Thanks, m_hutley, unfortunally page_one.html is a saved page, all pages in the original site (password protected) have the same URL.

Right… and this is where “There should be an API for this” comes into play. If the whole thing’s a dynamic load-and-replace on a single URL, you’re going to have a lot of trouble trying to reference between two different loads of data.

Does the system not expose an API for accessing the data programatically?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.