Greasy Fork is available in English.

Дискусии » Заявки за създаване

How to select a random generated ID?

§
Публикуван на: 06.01.2022

I want to click on a button automatically, but the button has a random generated ID each time like 081ab90900313289a80c11ce8af2b7ccfcbb0ecef313807f2fa8e0b229cb2fc1. I know there has to be an elegant way how to achieve that, but didn't find it myself yet. I want to use it like this:
(function (){document.querySelector('button[id="randomID"]').click();
Thank you!

§
Публикуван на: 06.01.2022
const idRegex = /[a-z0-9]{64}/;
const buttons = document.querySelectorAll('button');

for (const btn of buttons) {
  if (idRegex.test(btn.id)) {
    btn.click();
    break;
  }
}
§
Публикуван на: 06.01.2022
Редактиран на: 06.01.2022

This should click on first closest button from querySelectorAll('button') with id such like that

§
Публикуван на: 06.01.2022
const idRegex = /[a-z0-9]{64}/;
const buttons = document.querySelectorAll('button');

for (const btn of buttons) {
  if (idRegex.test(btn.id)) {
    btn.click();
    break;
  }
}

Awesome! It works! Thank you very much!
Is it possible to add some wait time before the click somewhere?

window.addEventListener('load', function() {

const idRegex = /[a-z0-9]{64}/;
const buttons = document.querySelectorAll('button');

for (const btn of buttons) {
  if (idRegex.test(btn.id)) {
    btn.click();
    break;
  }
}

(function (){
    document.querySelector('button').click();
  

  
})();
  }, false);
§
Публикуван на: 06.01.2022
window.addEventListener('load', function() {
  const someWaitTime = 1600;
  const idRegex = /[a-z0-9]{64}/;
  const buttons = document.querySelectorAll('button');

  for (const btn of buttons) {
    if (idRegex.test(btn.id)) {
      setTimeout(() => btn.click(), someWaitTime);
      break;
    }
  }
});
§
Публикуван на: 06.01.2022

Thank you very much!

Публикувайте отговор

Влезте, за да публикувате отговор.