`chrome.downloads.search` is missing file results

169 Views Asked by At

In the context of a Chrome extension, I want to map some local downloaded files to in-storage data.

I'm having trouble understanding why a file that is clearly in the desired folder according to my OS would not be detected by chrome.downloads.search?

❯ ls ~/Downloads/PaperMemoryStore/*2101*
/Users/victor/Downloads/PaperMemoryStore/2101.02464.pdf
const allfiles = await new Promise((resolve) => {
    chrome.downloads.search(
        { filenameRegex: "PaperMemoryStore/.*" }, 
        (p) => resolve(p))
    )
});
allfiles.filter(f => f.filename.includes("2101")) // [] <- empty filter

I understand why allfiles may have more results than what the filesystem shows (because of DownloadItem.state for instance) but not why it would show fewer.

Is it possible that chrome.downloads.search would not return files that have not been downloaded but just put in a folder? I could not find any such information in the docs.

Scenario:

  1. I downloaded a pdf (2101.02464.pdf)
  2. Which I copy-pasted in a sub-folder of Downloads/ (PaperMemoryStore)
  3. I'm running Brave Version 1.36.122 Chromium: 99.0.4844.88 (Build officiel) (x86_64)
1

There are 1 best solutions below

0
On

I faced the same issue with the error and state property. Can't search chrome DownloadQuery with the error: property I think it's a bug. To overcome this issue, search with the blank query to get all download items and then loop across all items and check the array one by one.

On manifest v3, chrome.downloads.search return results as promised. Just omit the callback.

let results = chrome.downloads.search({});
let filters = results.filter(log => log.filename.includes("2101"));