Disable scripts based on condition

Joined
Dec 31, 2022
Messages
12
Reaction score
1
Good evening, could you help me, please? From this page I run this script, which extracts, in the "Nome" field, the publisher from the bibliographic item (Feltrinelli, in the example). I would like the script to be executed only once, ie it has to be disabled when on the previous page there is the expression "Editore moderno" under the heading "Persone, istituzioni e famiglie". In this case, in fact, I would have to extract (manually) in the "Nome" field, the author from the bibliographic item. I tried replacing the last line of the script with this:

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

but with this change the publisher is not extracted the first time either.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
To check if the "Editore moderno" text is present, you should use the textContent property instead of includes method:

Code:
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));
}

This will compare the textContent of the elencoLabel element against the string "Editore moderno". If they match, the script will not run. If they don't match, the script will run and extract the publisher name.
 
Joined
Dec 31, 2022
Messages
12
Reaction score
1
Thanks for the answer, Kuncode, but the publisher is not extracted the first time either. I tried with this two scripts, the first one, referred to the first page, sets a local variable:

JavaScript:
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");
        }
    }


The second script, referred to the second page, reads the local variable:

JavaScript:
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));  }}

so I replaced the last line of the script with these last 5 lines (starting frome var elencoLabel), but the result is the same, the publisher isn't extracted.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top