简体   繁体   English

Chrome扩展程序:Javascript可以拦截文件下载并解析数据

[英]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. 我正在编写一个Chrome扩展程序来自动完成我的一项日常任务,部分任务是下载.csv文件并操纵数据。 As part of the automation I want to grab the file with Javascript and make the script manipulate it. 作为自动化的一部分,我想用Javascript抓取文件并让脚本操作它。

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. 问题是使用XMLHttpRequest获取此文件将是一场噩梦,因为它们会发送大量唯一ID来验证用户,并且找出它们来自哪里是一场噩梦。

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? 我可以使用javascript轻松点击按钮启动文件下载,但有没有办法让javascript拦截这个请求并解析CSV? I'm thinking if its not possible with plain javascript I might be able to do it with the HTML5 file API? 我想如果用普通的javascript不可能我可以用HTML5文件API做到这一点?

You might try using the webRequest.onBeforeRequest API. 您可以尝试使用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. 当您想要下载请求时,您可以使用此方法取消它,获取正在使用的完整URL,并从JavaScript发出相同的请求。

You can do it by accessing the file's content with an AJAX request to file://{filepath} once the file has been downloaded. 您可以通过在下载文件后使用AJAX请求访问文件的内容来执行此file://{filepath} You just need to add the file://* permission to your manifest file. 您只需要将file://*权限添加到清单文件中。

To get the downloaded file's path in the local system, use chrome.downloads.search and get the filename property. 要在本地系统中获取下载文件的路径,请使用chrome.downloads.search并获取filename属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM