Hi, could you help me, please? With a userscript I’m trying to select a radio button for the page types in the three examples below (saved pages, the original are protected), without considering the specific ID value, as I have many other pages. So I think something like this would be the starting point:
These buttons should be selected in order of priority:
When the terms “Collocazione” and “Inventari collegati” are on the same line (or <td> area), the radio button next to it must be selected, as in these two web pages: page one, page two (here is a screenshot for the two examples)
When the first case does not occur, and the terms " FA PARTE DI " and “Monografia” are on the same line (or <td> area), the radio button next to it must be selected, as in this page (see also a screenshot).
When the first two cases do not occur and the term “Monografia” is next to a radio button, then this one must be selected, as in this page (see also a screenshot).
So the radio button next to “Collana” (that is “series”) should never be selected.
Select all rows.
Search the selected for your first condition. If found, check the radio button in that row, and return.
Search for your second condition. If found, check the radio button in that row, and return.
Search for your third condition. If found, check the radio button in that row, and return.
I was trying something like this for the first two conditions, but it doesn’t work and I don’t know if it’s the right way to select a row and how to satisfy the third condition:
const selectedInput = document.querySelector('input[name="selezionato"]:checked');
var rows = document.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
var rowContent = rows[i].textContent || rows[i].innerText;
if (rowContent.includes('Inventari collegati')) {
selectedInput.value;
}
if (rowContent.includes('FA PARTE DI') && rowContent.includes('Monografia')) {
selectedInput.value;
}
}
well that doesnt… do anything. It gets the current value of whatever radio box is already checked, but doesnt do anything with it.
You’re going to need to do the for loop multiple times, because you want to check all rows for your first condition first; if you found it, stop. If you dont find it, then you need to look for the second condition, and then the third condition.
You’ve got the row; so somewhere in that row will be an input (that’s the radio button). rows[i].querySelector to find the radio button in that row, and set it’s .checked = true.
Thanks! Do you mean this for the first and second instances?
var rows = document.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
var rowContent = rows[i].textContent || rows[i].innerText;
if (rowContent.includes('Inventari collegati')) {
rows[i].querySelector('input[name="selezionato"]').checked = true;
}
if (rowContent.includes('FA PARTE DI') && rowContent.includes('Monografia')) {
rows[i].querySelector('input[name="selezionato"]').checked = true;
}
}
for (var i = 0; i < rows.length; i++) {
var rowContent = rows[i].textContent || rows[i].innerText;
if (rowContent.includes('Inventari collegati')) {
rows[i].querySelector('input[name="selezionato"]').checked = true;
}
if (rowContent.includes('FA PARTE DI') && rowContent.includes('Monografia')) {
rows[i].querySelector('input[name="selezionato"]').checked = true;
}
}
almost there.
function whateverwerecallingthisfunction() {
var rows = document.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
var rowContent = rows[i].textContent || rows[i].innerText;
if (rowContent.includes('Inventari collegati')) {
rows[i].querySelector('input[name="selezionato"]').checked = true;
return; //Found a match for condition 1. Stop looking.
}
} //If we get here, we didnt find any matches for condition 1. Start over, looking for condition 2.
for (var i = 0; i < rows.length; i++) {
var rowContent = rows[i].textContent || rows[i].innerText;
if (rowContent.includes('FA PARTE DI') && rowContent.includes('Monografia')) {
rows[i].querySelector('input[name="selezionato"]').checked = true;
return; //Found a match for condition 2. Stop looking.
}
}
}
Thanks, m_hutley, tomorrow I’ll test it and let you know. Is this the right way for the third condition?
if (rowContent.includes('Monografia') && !rowContent.includes('Inventari collegati')) && !rowContent.includes('FA PARTE DI')) && !rowContent.includes('Collana'))
{
rows[i].querySelector('input[name="selezionato"]').checked = true;
return; //Found a match for condition 3. Stop looking.
}
Well by the time you get to condition 3, you already know that the rowContent doesnt include Inventari collegati - if it did, the function would have stopped in condition 1.
You also already know that the row doesnt contain FA PARTE DI and Monografia both, or it would have matched the second condition and stopped there.
So by the time you get to the third condition, you dont need to check that those two things are false - you already know that they are.
if (rowContent.includes('Monografia') && !rowContent.includes('Collana'))