Intercept download and change file name

Viewed 306

I maintain a Google Chrome extension that fixes multiple annoyances with a specific web-based product. One such annoyance is that downloaded files' names are useless so I want to be able to intercept the download and change the file name to a useful one.

As per https://julienharbulot.com/chrome-extension-gutenberg.html and multiple StackOverflow posts, I have:

  1. Updated manifest.json to include "permissions": [ "<URL>", "downloads"] and "background": { "scripts": ["background.js"], "persistent": false }
  2. Created background.js with the following content just as a proof of concept but nothing gets logged to the console so I can only assume that the product doesn't use chrome.downloads or something:

    chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
        console.log(item.referrer);
        console.log(item.filename);
    });
    
  3. Updated the content script to include the following but nothing relevant seems to get logged to the console when I trigger a download:

    MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
    
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (!mutation.addedNodes) return
    
            for (var i = 0; i < mutation.addedNodes.length; i++) {
                var node = mutation.addedNodes[i];
    
                console.log(node);          
            }
        })
    });
    
    observer.observe(document, {
        subtree: true,
        attributes: true,
        childList: true
    });
    

Is there another way that I can detect how the product is invoking downloads and override it?

0 Answers
Related