I have a script that downloads a urls.txt with a list of urls to images on google. How do i execute this from a .js file

if i paste this into the browser console, it works 100% – as long as i am on google images in my browser. How can i execute this from just a .js file alone and using a variable as the term to search for?

I would ideally like to call this script from the cli in vscode and pass in an argument with the search term, the script then opens google in the background and downloads images related to the search term for the first x amount of search result pages.

This is for a TensorFlow dataset – i am trying to automate the process of collecting relevant image data.

function simulateRightClick(element) {
  var event1 = new MouseEvent('mousedown', {
    bubbles: true,
    cancelable: false,
    view: window,
    button: 2,
    buttons: 2,
    clientX: element.getBoundingClientRect().x,
    clientY: element.getBoundingClientRect().y,
  })
  element.dispatchEvent(event1)
  var event2 = new MouseEvent('mouseup', {
    bubbles: true,
    cancelable: false,
    view: window,
    button: 2,
    buttons: 0,
    clientX: element.getBoundingClientRect().x,
    clientY: element.getBoundingClientRect().y,
  })
  element.dispatchEvent(event2)
  var event3 = new MouseEvent('contextmenu', {
    bubbles: true,
    cancelable: false,
    view: window,
    button: 2,
    buttons: 0,
    clientX: element.getBoundingClientRect().x,
    clientY: element.getBoundingClientRect().y,
  })
  element.dispatchEvent(event3)
}

function getURLParam(queryString, key) {
  var vars = queryString.replace(/^?/, '').split('&')
  for (let i = 0; i < vars.length; i++) {
    let pair = vars[i].split('=')
    if (pair[0] == key) {
      return pair[1]
    }
  }
  return false
}

function createDownload(contents) {
  var hiddenElement = document.createElement('a')
  hiddenElement.href = 'data:attachment/text,' + encodeURI(contents)
  hiddenElement.target = '_blank'
  hiddenElement.download = 'urls.txt'
  hiddenElement.click()
}

function grabUrls() {
  var urls = []
  return new Promise(function (resolve, reject) {
    var count = document.querySelectorAll('.isv-r a:first-of-type').length,
      index = 0
    Array.prototype.forEach.call(
      document.querySelectorAll('.isv-r a:first-of-type'),
      function (element) {
        // using the right click menu Google will generate the
        // full-size URL; won't work in Internet Explorer
        // (http://pyimg.co/byukr)
        simulateRightClick(element.querySelector(':scope img'))
        // Wait for it to appear on the <a> element
        var interval = setInterval(function () {
          if (element.href.trim() !== '') {
            clearInterval(interval)
            // extract the full-size version of the image
            let googleUrl = element.href.replace(/.*(?)/, '$1'),
              fullImageUrl = decodeURIComponent(
                getURLParam(googleUrl, 'imgurl'),
              )
            if (fullImageUrl !== 'false') {
              urls.push(fullImageUrl)
            }
            // sometimes the URL returns a "false" string and
            // we still want to count those so our Promise
            // resolves
            index++
            if (index == count - 1) {
              resolve(urls)
            }
          }
        }, 10)
      },
    )
  })
}

grabUrls().then(function (urls) {
  urls = urls.join('n')
  createDownload(urls)
})