Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

507
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!