Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

510
Vistas
How to bypass JavaScript timer with Tampermonkey?

I'm trying to bypass a 60 second countdown timer using Tampermonkey. This is the JavaScript from the page source:

var sec = 60, sInt, s = 'Please wait %s before downloading.';
function countDown() {
    var f = $('#download');
    if (sec < 0) {
        d.html('<div style="margin: 10px auto" class="center"><form action="" method="post">\
        <input type="hidden" name="hash" value="REMOVED">\
        <div id="REMOVED" style="width:450px;margin:5px auto"></div> \
        <input type="submit" class="submit" value="Get download link" name="download"> \
        </form></div>');
        hcaptcha.render('REMOVED', {
            'sitekey': 'REMOVED'
        });
        clearInterval(sInt);
        return;
    }
        d.html(s.replace('%s', sec +' second'+ (sec != 1 ? 's' : '')));
        sec--;
}
$(function() {
    sInt = setInterval(countDown, 1150);
});

(a few items were substituted with 'REMOVE' but should not have effect I don't think)

So far I have tried the following but didn't work:

// ==/UserScript==

(function() {
    'use strict';
     var sec = 0;  
})();

Any suggestions to make this work? I think any of these two methods would work, but not sure how to do it:

  1. either change the var sec to 0, or
  2. change the countDown milliseconds from 1150 to 0.

So can either of those be done? If so, how can you do it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Problem

The likely problem is the declaration of sec is in a different scope to the sec declared in the website's script. As a result you're not setting the value on the same sec.

Solution

Mutating sec

You'll need to find if it's possible to make manipulations in the same scope as where sec is declared by the website. This is only really possible if the website author has declared sec in the global scope. If they've declared it in the global scope doing window.sec = -1; should work. If not, then you may need to manipulate the timer.

Manipulating timers

Manipulating timers can be a big task. Take a look at sinonjs/fake-timers for what complexity could be involved. I don't think such a complex solution is necessary for what you want here, so let's just manipulate the timer by hijacking it. The below will hijack setInterval and cause intervals to run (as close as possible to) immediately. Caution should be taken when using this because if there is an interval that is never cleared and it performs memory intensive tasks then it could crash your browser/computer.

const hijackedInterval = window.setInterval;

window.setInterval = (fn) => {
  return hijackedInterval(fn, 0);
};

Below is a demo. There are two immediately invoked function expressions to demonstrate different scopes. The first performs the hijacking of setInterval. The second is a counter which increments every second. Once 10 seconds has passed DONE! should be logged to the console. However as we've hijacked setInterval, the log occurs immediately.

(() => {
  const hijackedInterval = window.setInterval;

  window.setInterval = (fn) => {
    return hijackedInterval(fn, 0);
  };
})();


(() => {
  let counter = 0;
  let intervalId;
  
  intervalId = setInterval(() => {
    if (counter === 10) {
      clearInterval(intervalId);
      console.log('DONE!');
      return;
    } else {
      counter++;
    }
  }, 1000);
})();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda