How do I detect if the browser is downloading a Javascript file?

Asked

Viewed 102 times

0

I am creating an application where I need to detect if the browser is downloading any file, be it script, image, audio or source. How can I do this in Javascript?

I’d like something like that:

const download = browser_is_downloading();

if (download) {
    // do something...
}

  • I believe it can only be done using an extension https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/downloads/download. . But I guess I’ve never done anything like this so there might be another solution that I don’t know about.

  • 2

    What is this? Javascript running in the browser or running on Puppeteer?

  • In the browser...

  • 2

    It is impossible, first that it would violate the user’s privacy, second that this whole control is solved via HTTP, the download manager may even come embedded in the browser, but it is not part of the communication with the directly rendered web pages, although using the same session the download is shifted to another process to solve it, the 5 years I have developed 2 web browsers for specific use, Qtwebkit (is the safari Webkit but within Qt) and another with Chromium (actually is the Qtwebengine), there is no download manager, you only had the interfaces ...

  • 1

    ... CSS, JS, Cookies, network, etc to control, so the download of course never occurred, but of course had the API to receive such a download, I wrote in C++ and Qt my own manager, that received the API response when the user requested the download and redirected the HTTP request and session to it, so it worked equally what happens in a browser, built the manager and was solved. When I migrated from Qtwebkit to Qtwebegnine it was enough to adjust the API, the manager was the same, but of course the Qtwebengine network system is different from Qtwebkit ...

  • 1

    ... then in the user’s view the process was the same, but underneath it was not. This all just told you to explain, what happens in download managers is really something on the side, sometimes "someone" can even provide an API in the browser that manages this in a way that you control in the browser, but most of the time it is third party, how to create a browser extension that intercepts downloads and sends via postMessage to all web pages in an event something started.

  • I understand, but why would a website check to see if there are downloads being made in the session violate user privacy if such files would come from the site itself? For example, if I create a website that has audios and I want to know if the browser is downloading these audios, because it would violate privacy and the audios are coming from my server ?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.