Pregunta

I am trying to create an extension which puts all the pdf files downloaded by user through browser into a separate directory. Any Api by which can do this or some help on how I can do it would be helpful.

¿Fue útil?

Solución

While you cannot easily write to an arbitrary path, you can redirect all PDF files into a subfolder of Downloads folder.

Check out onDeterminingFilename event of chrome.downloads and Filename Controller sample extension. Since you can indicate a relative path instead of a plain filename, this should work.

Note that the file's MIME type should be available in onDeterminingFilename, you can use that.


Code example, as requested:

var folder = "PDF_downloads";

chrome.downloads.onDeterminingFilename.addListener( 
  function (item, suggest) {
    if(isPDF(item)) suggest({filename: folder + "/" + item.filename});
    else suggest();
  }
);

function isPDF(item){
  if(item.mime === "application/pdf") return true;
  else if (item.filename.match(/\.pdf$/i)) return true;
  else return false;
}

This will not override that the browser tries to open the PDF itself instead of downloading, but attempting to download will suggest that folder.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top