Reputation: 654
I'm making a Chrome Extension and I'm using this code to "catch" a download on request.
chrome.downloads.onCreated.addListener(function(downloadItem) {
});
What I want is to stop the download from appearing on the download bar, read the file in my extension and then delete the file from the computer.
I've tried using chrome.extensions.download.erase() and chrome.browsingData.remove() but to no avail.
How can I do this?
Upvotes: 2
Views: 3030
Reputation:
A better event is onDeterminingFilename
, since this happens before the download starts:
chrome.downloads.onDeterminingFilename.addListener(function (item) {
chrome.downloads.cancel(item.id);
});
https://developer.chrome.com/extensions/downloads#event-onDeterminingFilename
Upvotes: 5
Reputation: 3919
In a content script:
document.querySelector("a").addEventListener("click",function(){
chrome.runtime.sendMessage({action: "download", url: this.href});
});
//may be you should restrict the selector to the specific links that you want to download with your extension.
In a background page:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.action == "download" ){
//here download via xhr, then read the file as you plan to do
}
});
Upvotes: 0