Autoclick on two consecutive buttons

Hi, I’m trying from Firefox with TamperMonkey to auto-click two buttons in succession from a page like this:

The buttons are:

  1. April la lista completa
  2. Tutte le biblioteche

I tried in vain with this script:

// @match        https://www.bibliotechediroma.it/opac/*

function wait() {
        if(document.querySelector('#biblioteche > details > summary > span') == null) {
            setTimeout(wait,1000);
        } else {
            document.querySelector('#biblioteche > details > summary > span').click();
            document.querySelector('#biblioteche > ul > li.tutte > a').click();
        }
    }
   wait();

Could you help me? Thank you!

Like this:

// ==UserScript==
// @name         New Userscript
// @version      0.1
// @description  Try to take over the world!
// @author       You
// @match        https://www.bibliotechediroma.it/opac/*
// @run-at       document-idle

// ==/UserScript==

(function() {
  'use strict';

  window.addEventListener('load', function() {
    document.querySelector('#biblioteche summary').click();
    document.querySelector('#biblioteche li.tutte a').click();
  }, false);
})();

There selectors in the code you posted were slightly wrong.

Also, if you us the run-at header to tell Tampermonkey to inject your script after the DOMContentLoaded event was dispatched and listen for the load event, you can do away with the function that waits for the elements to show up.

Let me know if that works for you.

2 Likes

Thank very much you James, it works! Is it possible let that script work also from this kind of page, where there’s only one button (Tutte le biblioteche) and there isn’t the button Apri la lista completa?

So you mean if you’re on this page:

https://www.bibliotechediroma.it/opac/resource/via-col-vento/RMB0007733

Then we need to:

  1. Expand 'Apri la lista completa ’
  2. Click on ‘Tutte le biblioteche’

Whereas on this page:

https://www.bibliotechediroma.it/opac/resource/la-ballata-del-caffe-triste-e-altri-racconti/RMB0094567

We just need to click on ‘Tutte le biblioteche’.

Correct?

Yes, if I’m on a page with those two buttons click on them, if I’m on a page with only the second button (“tutte le biblioteche”) click on it.

Ok, well the first page has this kind of structure:

<section id="biblioteche">
  <header>...</header>
  <ul>
    ...
    <li class="tutte">...</li>
  </ul>
  <details>
    <summary>...</summary>
  </details>
</section>

And the second one has this:

<section id="biblioteche">
  <header>...</header>
  <ul>
    ...
    <li class="tutte">...</li>
  </ul>
</section>

So one approach could be:

  • grab a reference to the section element
  • check if it has a details section
  • if it does, open it
  • click on the a element inside the li element with a clauss of “tutte”

In code that would be:

// ==UserScript==
// @name         New Userscript
// @version      0.1
// @description  Try to take over the world!
// @author       You
// @match        https://www.bibliotechediroma.it/opac/*
// @run-at       document-idle

// ==/UserScript==

(function() {
  'use strict';

  window.addEventListener('load', function() {
    const section = document.getElementById('biblioteche');
    const details = section.querySelector('details');
    const link = section.querySelector('li.tutte a')

    if (details) details.click();
    link.click();
  }, false);
})();
1 Like

Wao, works great, thanks really so much, James!!

No worries. Happy to help :slight_smile:

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