I'm developing a Chrome extension in which I generate a CSV file and when a button is clicked, I want to store this CSV file in the user's drive.
I first tried to add the "Save to Drive" button/widget (https://developers.google.com/drive/api/v3/savetodrive) in my HTML, however I found out this won't work as my CSV file is not stored locally but generated from text content, using: blob = new Blob([csv], {type: "text/csv"}); href = window.URL.createObjectURL(blob);, where csv is a string storing the CSV formatted data. As a result of this the href variable is not a URL, but a blob URI which isn't supported as a data-src attribute when using the Save to Drive button (From the docs: "Data URIs and file:// URLs are not supported")
The second approach is to use the Google Drive API, however I haven't found any good examples on how to use this API on just purely front-end applications with no backend. Most of the documentation's example are all in Python or node. The only browser based example in the docs (https://developers.google.com/drive/api/v3/quickstart/js) requires a localhost server to run which won't be possible in a Chrome extension. Trying this example found in the docs without running a localhost server, results in the following error: Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('file://') does not match the recipient window's origin ('null').
I know that the Drive API's upload endpoint supports CORS, so I don't know why I would need a localhost server to make requests to the Drive API or to use the gapi.client from https://apis.google.com/js/api.js. In summary, I don't believe what I'm looking for is too complicated; save a CSV file to someones Google Drive without using a backend server. It seems like its possible but I haven't come across any examples or documentation on how to achieve this on purely front-end applications.
function saveToDrive() { // Function that is called when the button is clicked.
csv = document.getElementById("userInput").value // User inputs CSV formatted data, this data is captured and stored in a string
blob = new Blob([csv], {type: "text/csv"});
href = window.URL.createObjectURL(blob);
// Code to upload this file to the logged in user's Google Drive (what I need)
}