Search code examples
javascriptgoogle-chrome-extension

Clicking in random time durations to a button with Chrome extension


I am creating a chrome extension to calculate the time range based on the user input and press the button on the webpage. For an example; user enters the duration as 5 minutes. The program sets the lower boundary as duration-1 (4), upper boundary as duration+1 (6). And it will randomly create a time duration (example 4 mins 23 secs.). So far it is randomly pressing the button but when I test the durations its not matching. Sometimes it is pressing completely random, not with the calculated random duration. So, I am confused. What am I doing wrong? I think my math is correct for calculating the duration but not sure..


function startClicking(duration) {
    const userDuration = duration || 5; // Default user input is 5 if no duration is provided
    
    const lowerBoundary = Math.max(1, userDuration - 1); // Ensure lower boundary minimum is 1
    const upperBoundary = userDuration + 1; // Upper boundary (6)
  
    clickTargetButton(); // Click the button immediately
  
    setTimeout(function clickWithRandomDuration() {
      const randomizedMinutes = Math.floor(Math.random() * (upperBoundary - lowerBoundary)) + lowerBoundary;
      const randomizedSeconds = Math.floor(Math.random() * 60);
      const randomizedDuration = randomizedMinutes * 60 * 1000 + randomizedSeconds * 1000;
  
      clickTargetButton(); // Click the button after the randomized duration
      console.log(`Randomized/Next click in: ${randomizedMinutes} minutes ${randomizedSeconds} seconds`);
  
      setInterval(clickWithRandomDuration, randomizedDuration);
    }, userDuration * 60 * 1000); // Initial delay for the first random click
  }


Solution

  • To fix this issue, you can use setTimeout instead of setInterval. Here's an updated version of your startClicking function:

    function startClicking(duration) {
      const userDuration = duration || 5; // Default user input is 5 if no duration is provided
    
      const lowerBoundary = Math.max(1, userDuration - 1); // Ensure lower boundary minimum is 1
      const upperBoundary = userDuration + 1; // Upper boundary (6)
    
      function clickWithRandomDuration() {
        const randomizedMinutes = Math.floor(Math.random() * (upperBoundary - lowerBoundary)) + lowerBoundary;
        const randomizedSeconds = Math.floor(Math.random() * 60);
        const randomizedDuration = randomizedMinutes * 60 * 1000 + randomizedSeconds * 1000;
    
        clickTargetButton(); // Click the button after the randomized duration
        console.log(`Randomized/Next click in: ${randomizedMinutes} minutes ${randomizedSeconds} seconds`);
    
        setTimeout(clickWithRandomDuration, randomizedDuration); // Use setTimeout instead of setInterval
      }
    
      clickTargetButton(); // Click the button immediately
      setTimeout(clickWithRandomDuration, userDuration * 60 * 1000); // Initial delay for the first random click
    }
    

    In this updated code, I've replaced setInterval with setTimeout inside the clickWithRandomDuration function. This ensures that the next click is scheduled after the randomized duration, without creating multiple overlapping intervals.