First of all, this question is not a duplicate because the suggest function has no callback against 'chooseEntry'.
I'm trying to write an extension for Chrome which by default overwrites files instead of suggesting a copy with trailing numbers like '(1)'.
The extension should:
- Include an option to toggle this future
- Suggest the filename without trailing number, when turned on
- Do, when turned off
- Remember the latest download location
- Always open a file selector with this location
This was my first approach:
chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
chrome.storage.local.get(function(options) {
// retrieve preference from option page
var suggestion = {filename: item.filename};
if (options["opOverwrite"]) suggestion["conflictAction"] = "overwrite";
suggest(suggestion);
});
return true;
});
which worked fine but doesn't remember the last download location and opens the default download directory instead – which I don't wanted. So I tried to extend this by querying the last successfull download and its location (what seems to be the only way).
chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
// retrieving latest successfull download <-> filename not empty
var query = {filenameRegex: ".+", orderBy: ["-startTime"], limit: 1};
chrome.downloads.search(query, function(results) {
// results : Array
// Cutting of the filename, leaving the actual location – empty if none found
var lastLocation = (results.length > 0 ? results[0]["filename"].match(/.+\\/)[0] : "");
chrome.storage.local.get(function(options) {
var suggestion = {filename: lastLocation};
if (options["opOverwrite"]) suggestion["conflictAction"] = "overwrite";
suggest(suggestion);
});
});
return true;
});
which now works as expected, EXCEPT it throws
Unchecked runtime.lastError while running downloadsInternal.determineFilename: Invalid filename
at suggest(suggestion);
!
Actually the chrome extension documentation declares that "Absolute paths, empty paths, and paths containing back-references '..' will be ignored." (It opens the absolute path in the file selector anyway). My download locations however are not always subdirectories of the default download folder and therefore can't be stated relative.
So how do I 'catch' this error? The extension works fine on the frontend. Others suggested to check if runtime.lastError has been set, but I don't understand where I should test this.
Alternatively, is there a way to make this code error free while keeping said features?