Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension: Can Javascript intercept a file download and parse the data

I'm writing a Chrome Extension to automate one of my daily tasks, part of the task is downloading a .csv file and manipulating the data. As part of the automation I want to grab the file with Javascript and make the script manipulate it.

The problem is grabbing this file with an XMLHttpRequest would be a nightmare because they send heaps of unique IDs to verify the user and it'd be a nightmare to work out where they come from.

I can easily click the button using javascript which starts the file download, but is there a way that javascript can intercept this request and parse the CSV? I'm thinking if its not possible with plain javascript I might be able to do it with the HTML5 file API?

like image 771
Sean Bannister Avatar asked Dec 17 '22 07:12

Sean Bannister


2 Answers

You might try using the webRequest.onBeforeRequest API. When the download request is you want is made you can cancel it with this method, get the complete URL being used, and make that same request from JavaScript.

like image 144
abraham Avatar answered Dec 18 '22 19:12

abraham


You can do it by accessing the file's content with an AJAX request to file://{filepath} once the file has been downloaded. You just need to add the file://*permission to your manifest file.

To get the downloaded file's path in the local system, use chrome.downloads.search and get the filenameproperty.

like image 30
WhiteFangs Avatar answered Dec 18 '22 19:12

WhiteFangs