Search code examples
javascriptnode.jsautomation

Way to automatically open browser at certain time of day using JavaScript?


Is there a way to open browser and automate tasks using JavaScript while my computer is on but without having anything opened? Perhaps using NodeJS? Just to clarify, I'm not talking about JS methods like window.open(), I'm talking about automating tasks on hardware level.

For instance:

  1. Open Chrome at 12:00PM on Wednesday.
  2. Go to google.com.
  3. Sign in.

If JavaScript can't do it, what language should I learn that's closest to JavaScript syntax?


Solution

  • In Node Js, you need to spin up a server that will do this job for you. Here is the code i have used to open up a browser with a URL

    const { platform } = require('os'); const { exec } = require('child_process');

    const WINDOWS_PLATFORM = 'win32'; const MAC_PLATFORM = 'darwin';

    const osPlatform = platform();
    const args = process.argv.slice(2);
    const [url] = args;
    
    if (url === undefined) {
      console.error('Please enter a URL, e.g. "http://www.opencanvas.co.uk"');
      process.exit(0);
    }
    
    let command;
    
    if (osPlatform === WINDOWS_PLATFORM) {
      command = `start microsoft-edge:${url}`;
    } else if (osPlatform === MAC_PLATFORM) {
      command = `open -a "Google Chrome" ${url}`;
    } else {
      command = `google-chrome --no-sandbox ${url}`;
    }
    
    console.log(`executing command: ${command}`);
    
    exec(command);
    

    Now you can get time with following code

    new Date().getTime()
    

    Combination of these will be hopefully work the thing you need. thanks Please follow this article for further information on how to sign into a website https://marian-caikovski.medium.com/automatically-sign-in-with-google-using-puppeteer-cc2cc656da1c