3

I'm trying to create an extension that will prevent Chrome from downloading any files but I haven't been successful.

Here's my manifest

{"name": "Download Cancel",
 "description": "Prevents Downloads",
 "version": "0.1",
 "background": {"scripts": ["background.js"], "persistent": false},
 "permissions": ["downloads", "tabs", "http://*/*","https://*/*"],
 "manifest_version": 2}

and background.js

chrome.downloads.onCreated.addListener(function(item) {
  chrome.downloads.cancel(item.id);
  alert("Downloading files is not permitted");
  chrome.downloads.removeFile(item.id);
});

Can anyone offer some suggestions as to what I am doing wrong?

  • Any reason you're not doing this onCreated instead of onDeterminingFilename? – Sperr Jan 03 '14 at 17:38
  • I've tried both (with just swapping out OnDeterminingFile with onCreated) and haven't had any luck with it either. – user3158161 Jan 03 '14 at 18:59
  • 1
    Use lowercase `item.id` instead of `item.ID`. You would have spotted the error if you [looked at the console of the background page](http://stackoverflow.com/a/10258029/938089) for errors. The extension won't work as expected though: When you close Chrome, all download dialogs will appear again (not sure whether it's a bug or a feature). – Rob W Jan 03 '14 at 20:51
  • Thanks for the suggestion @Rob. Now I've come to a issue. If the user if downloading something very small like an image, it can't cancel it in time. When this happens, I would like to remove the file. The only problem is when I look at item.state throughout the whole event listener it says it's in_progress. If I simply add a removeFile call after cancel, it will usually crash the browser unless the download actually finished. Can anyone help me with getting state to so I can know if I should be removing the file or not? (I've updated the code in my original question to what I currently have) – user3158161 Jan 03 '14 at 21:44
  • @user3158161 A crash? Create a minimal extension that reproduces the problem and report it at http://crbug.com/new. Have you already tried to see what happens if the user has set the preference to "Ask where to save each file before downloading" (at `chrome://settings` -> "Show advanced settings" -> "Downloads")? I guess that onCreated is triggered after the dialog has opened, which is probably too late. Why do you want to prevent downloads? – Rob W Jan 03 '14 at 21:56
  • @RobW I'll try to recreate the issue and report it. I haven't tried that preference, I may give it a shot. I'm trying to prevent downloads on a terminal server's installation of Chrome. I used to be able to prevent downloads yet allow uploads by using GPO to set the download directory to a nonexistent location, but an update made that trick not work anymore so I'm trying to create an extension to do the same thing. – user3158161 Jan 03 '14 at 23:29

1 Answers1

1

As you have already blocked downloading the file but to prevent it from crashing, you can check if the download has been completed or not and if yes, then remove it inside that if block.

chrome.downloads.onCreated.addListener(function(item) {
    chrome.downloads.cancel(item.id);
    if(item.state == "complete"){
        chrome.downloads.removeFile(item.id);
    }
});
Ashish Bansal
  • 79
  • 1
  • 6