chrome extension change download file name form
chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
if (item.referrer.search("gutenberg.org") == -1) {
// If the file does not come from gutenberg.org, suggest nothing new.
suggest({filename: item.filename});
} else {
// Otherwise, fetch the book's title in storage...
chrome.storage.local.get([item.referrer], function(result) {
if (result[item.referrer] == null) {
// ...and if we find don't find it, suggest nothing new.
suggest({filename: item.filename});
console.log('Nothing done.');
}
else {
// ...if we find it, suggest it.
fileExt = item.filename.split('.').pop();
var newFilename = "gutenberg/" + result[item.referrer] + "." + fileExt;
suggest({filename: newFilename});
console.log('New filename: ' + newFilename);
}
});
// Storage API is asynchronous so we need to return true
return true;
}
});