0

I am currently writing code for a chrome extension where it switches to random tabs and performs various actions. I am writing the code in Javascript but I am willing to use python. How can I make the code scroll down to the bottom of the page and back up within a certain time?

Here is the background.js file of the extension:


// Set up a timer to perform actions every 5 seconds
setInterval(performActions, 5000);

function performActions() {
  // Get a list of all open tabs in the current window
  chrome.tabs.query({currentWindow: true}, function(tabs) {
    // Select the third tab with a higher probability
    var tab;
    if (Math.random() < 0.8) {
      tab = tabs[2];
    } else {
      tab = tabs[Math.floor(Math.random() * (tabs.length - 1))];
    }

    // Activate the selected tab
    chrome.tabs.update(tab.id, {active: true});

    // Send a message to the selected tab's content script
    chrome.tabs.sendMessage(tab.id, {action: 'scroll'});
  });
}

I tried to use the "window.scrollTo" and it didn't work. I also tried to use the pynput module. I don't know what the issue is.

  • Here's a tutorial on an extension that calls Python to simulate keystrokes. [How to make Chrome Extension 69 IME on/off and print dialog](https://youtu.be/oFPvMWidx2U) You can simulate the End key and Home key by referring to this. – Norio Yamamoto Dec 25 '22 at 05:40
  • You need a content script with an [onMessage listener](https://developer.chrome.com/extensions/messaging), inside which you will use scrollTo. – wOxxOm Dec 25 '22 at 07:56
  • "switches to random tabs and performs various actions" --- That's a bit vague, can you be more specific? – Thomas Mueller Dec 25 '22 at 07:57
  • Do any of [these answers](https://stackoverflow.com/questions/11715646/scroll-automatically-to-the-bottom-of-the-page) help? – FiddlingAway Dec 25 '22 at 10:48

0 Answers0