Chrome extension code problem

Asked

Viewed 40 times

1

Good afternoon!

I’m trying to create an extension to automatically click a one-page button every X seconds. In my case is a working tool called Servicenow, where there are panels with reports in real time, but the menu where are the calls I have to answer does not update alone, only if you click a button with Refresh icon.

The problem is that it does not perform the function it should (I believe it is a problem in querySelector).

div do botão

<div class="header-tools" style="background-color: #1d7f00">
<button tabindex="0" class="btn header-icon icon-refresh" style="color: #FFFFFF" aria-label="Atualizar Widget Dashboard Fila Atendimento - Incidentes" title="" data-original-title="Atualizar"></button>
</div>

manifest.json

{
"manifest_version": 2,
"name": "Auto Refresh - ServiceNow",
"description": "Extensão para o Google Chrome que atualiza automaticamente o ServiceNow.",
"version": "1.1",
"icons": {
"128": "128.png"
},
"content_scripts": [{
"matches": ["https://renner.service-now.com/*"],
"js": ["autorefresh-servicenow.js"]
}]
}

autorefresh-servicenow.js

;(function( doc ) {
  'use strict';
  
  console.log( "Auto Refresh - ServiceNow" );
  var $autorefreshsn;
  
  window.setInterval(function() {
    var $autorefreshsn = document.querySelector('button.btn.header-icon.icon-refresh::before');
    if( ! $autorefreshsn ) {
        return;
    }
    
    $autorefreshsn.click();
  }, 3000);
})( document );

Obs: I’ve tried without the ::before, I tried only with the class .header-tools and even by the attribute [style="background-color: #1d7f00"] but nothing helped.

1 answer

4

The problem is the selector itself, if check its return is null. You can use as selector:

button[aria-label="Atualizar Widget Dashboard Fila Atendimento - Incidentes"]

(function(doc) {
  'use strict';
  
  const query = 'button[aria-label="Atualizar Widget Dashboard Fila Atendimento - Incidentes"]'

  window.setInterval(function() {
    var $autorefreshsn = doc.querySelector(query);
    if (!$autorefreshsn) return;
    $autorefreshsn.click();
  }, 3000);

  // Só para logar quando ocorrer clique no botão. :)
  document.querySelector('button').addEventListener('click', () => {
    console.log('Fui clicado...')
  })
})(document);
<div class="header-tools" style="background-color: #1d7f00">
<button tabindex="0" class="btn header-icon icon-refresh" style="color: #FFFFFF" aria-label="Atualizar Widget Dashboard Fila Atendimento - Incidentes" title="" data-original-title="Atualizar">Botão</button>
</div>

  • Show! And how do I select with the attribute too? In case the same I put there. There are 5 buttons on this page, 4 of them in the panels that are real-time (wtf?) and the one that is left to update the Dashboard. I would have to select it in specific and the only distinction is the style="background-color: #1d7f00".

  • 1

    @Rogerwindberg the aria-label and data-original-title are the same in all?

  • The data-original-title are equal in all but the aria-label is unique to each.

  • 1

    @Rogerwindberg can make the query for him so I edited the answer.

  • 1

    @Rogerwindberg a tip: if you need to interact with the page that doesn’t need to use any "low-level" API extensions use, it’s easier to create a userscript than an extension. Even as an example, I have a script to automatically rescue channel points on Twitch (which is basically the same thing your script does): example

  • I tried everything here with the tips but it still didn’t work, it seems that he doesn’t recognize the selector at all. The goal of creating an extension is for me to be able to "fix" these page flaws and be able to share with the work team afterwards.

  • A userscript can also be installed by multiple people and versioned. As for the selector question, the question you asked, what you can answer is this.

  • I get it. I’m very uneducated in this subject of programming and this is my first attempt to build a code. How does this userscript work? I have no idea where to apply and what to do with it.

  • @Rogerwindberg in the readme of this link I gave you, has an explanation of how to install a userscript.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.