javascripthtmlgoogle-chromegoogle-chrome-extensiongoogle-drive-shared-drive

How to programmatically Download a file from Google Drive, using Javascript and HTML?


Goal:

  1. In my Chrome Browser, open this Public Google Drive Folder

  2. Then run a javascript to:

P.S: I do NOT want to use the GoogleDriveAPI.

Steps:

1. Select file

2. Download file, using one of the following options:

What's working:

Select the file using:

document.querySelector('div[jsname=LvFR7c]').click()

Expand the file options menu using:

document.getElementsByClassName("pc7nUb I5YcP Dk9rmd").click()

What's NOT working:

Clicking Download button from page or from file options menu

Conclusion:

I noticed that the Google Drive HTML elements are mostly with aria roles like button, menu, & menuitem. Some of these elements have a jsaction attribute. When these elements are clicked via code, some of them behave as they do when clicking with the mouse, and others do nothing when clicked via code. So I'm just confused and am not sure what to try next.

I feel like this is a trivial task, but this is my first time working in Javascript & HTML, so I'm sure I might be missing something simple.

Download Button HTML Elements:

Page Download button HTML:

document.querySelector('div[aria-label=Download]')

File Options Menu Download button HTML:

document.querySelectorAll('div[aria-label=Download]')[3]

Things I've tried on the above Download Button HTML Elements:

element.click()
element.dispatchEvent(new Event('click'))
element.dispatchEvent(new Event('keydown')) // (with more options in the Event initializer)
element.dispatchEvent(new Event('keyup'))
element.focus()
element.setAttribute('aria-expanded', true)
element.setAttribute('aria-pressed', true)
element.ariaPressed = true

Solution

  • You can try this way:

    1. Find unique id of images

      let imgDiv = document.querySelectorAll('c-wiz>div[data-id]');
      var imgIds = [];
      imgNodes.forEach(img => {
          let imgId = img.getAttribute('data-id');
          // console.log(imgId);
          imgIds.push(imgId);
      });
      
    2. use fetch api to if you want to show as blob url, otherwise open url in new tab as below

      // imgIds[0] = 1-XLXQ04HZtNmPgC-zpYnQEDq3w5aPlDR
      let downloadLink = `https://drive.google.com/uc?export=download&id=${imgIds[0]}`;
      window.open(downloadLink, '_blank');