Block default chrome Downloader - google-chrome

I'm writing a download manager app for Linux so I use a chrome extension to add listener to all download links in page and and when a download link clicked link send to a native app. everything is ok but I want to prevent show default chrome save dialog when download links clicked . how? thank you.

A quite limited approach to intercept single-clicked links would be to inject a content script with:
window.addEventListener("mousedown", interceptFunction); // intercept mouseclicks
window.addEventListener("keydown", interceptFunction); // intercept Enter key on a link
The interceptFunction would check if the link is likely to be a downloadable file judging by the url (file extension) and cancel the event:
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
Then it would send the url to your background page which will tell your native app to start the download.
To make the decision whether to download or not more reliable you may perform a XMLHttpRequest with method: "HEAD" and analyze the response headers (you'll have to deal with CORS though) like Content-Disposition: attachment; filename=<file name.ext> and others.

Related

HTLM Download a file typing the url in browser

Good Morning,
I would like to know how to do in HTML to type the folowing url of a file in a browser:
http://localhost:7000/pictures/picture01.png
And to automatically download it
I do not want to create a link like that:
download my picture
Is that possible?
The browser will not download the picture automatically for you, the address bar will just use the URL provided, and do the DNS lookup and render/download the data/image/html in the client machine from the server.
And for this reason, only we have clientside scripting language to tell what to do with the downloaded contents. Now to download the image automatically what you can do is download a javascript file that will execute in the client machine and download the image automatically.
just trigger the download on javascript/jquery onLoad event
refer this for more info: Force Download an Image Using Javascript
If I understand your question right, you want GET to a certain URL to trigger the download of an asset.
Yes, it's possible. To achieve this you need to configure your server to respond with octet stream. This way, the browser will show download window automatically.
You can find more details & some discussion here.

Async document download in browser

We have a web app with html pages 1>2>3
Page 2 has a link to download a pdf - I've used an anchor tag in the form to do this. The response is of type application/pdf. If I wait on page 2 my pdf eventually shows as downloaded, however I want to be able to move to page 3 with the pdf download happening asynchronously...
I can get the server to do it but as soon as I request page 3 the browser (Chrome) shows the GET request (for the pdf) as status: (canceled) :-(
I've tried both HTTP Request and AJAX Request (type: document and xhr) any suggestions?
So far I can only get it to work when the request is in a new tab - but that's not what I want as the pdf is downloaded and not opened in the new tab.
If your app is SPA, you can create an iframe and set src attribute with the download url.
If it is not SPA, and you used anchor <a> tag then use target="_blank" attribute to download it from a new window in which case, new tab will open and starting the download and the tab will be closed by letting the download happened in the background so that you can change anything on the main page.
If you want to download rather than show the pdf, I'd look into getting the server setting the http response header
Content-Disposition: attachment; filename="yourname.pdf"
so the browser should suggest "save as" and open a dialog to choose the save location - the download will usually continue running when navigating away from the page where the download was initiated.
I'm sorry I have no idea if you can achieve this purely on the browser side.
Solution to my problem was to get access to the HttpServletResponse (server side), set the header content type ("application/pdf") and disposition ("attachment;filename='...'") and flush the outputStream.
This must happen ahead of writing the PDF byte array to the outputStream.
The above allowed the browser to show my PDF download as 'started' and I could then navigate away from that page while the download continued.

Set Headers for a URL opened from chrome extension

I will open a URL in a new tab from my chrome extension using chrome.tabs.create() method. My problem is that I need to set a header on opening the URL, so that it is listened from the server side. How to set a header for the URL that is been opening?
I'm assuming you mean the HTTP Request headers, in which case there is a way to modify HTTP Request headers for requests as they go out the door, using the chrome.webRequest.onBeforeSendHeaders API found here. Let's say that you know you're going to create a new tab with a specific URL. You can setup a listener for the onBeforeSendHeaders API prior to creating the new tab. In the handler for that, you would only consider URLs that match that URL. You could then modify the header, and optionally un-instrument your onBeforeSendHeaders API (might need to do via a setTimeout or something).
This is a little unclear. Could you give me the source code of your extension? I suppose you could do it with the <title> HTML element, if that's what you're asking

Download .html file as attachment instead of opening

When I send a .html file as an attachment it opens in a new browser tab when it is clicked on. I want it to download as normal attachments are.
Is there a solution to this problem. Perhaps a programming solution? Or perhaps a file extension that downloads from emails but opens normally in a browser when clicked on?
I know that the recipient can right click and click download or something but I want the sender to be able to control whether or not it is downloaded or opened in a new tab.
Does a file extension that opens with browsers by default but downloads normally from emails exist?
Your webserver is sending a content-type header of type "text/html", and your clients' browsers are acting on that header. If you can tell your server to send "application/octet-stream", then the browsers will ask the users what to do with the file.
Maybe try changing the file extension to .php might work. PHP files are basically html files, but they expect PHP code in there - doesn't mean there has to be though. (This may work for other code language extensions too).
Not sure if the whole workflow will play out the way you want it, but worth a try...

Redirection with chrome extension

I'm really new to the chrome app dev part. I want to know how can I make a chrome application that will read my current URL and redirect me to another.
For instance if I am on www.stackoverflow.com and user clicks on the extension icon then it will redirect him to wwww.stackoverflow**NEW**.com
Thank you..
Follow these steps and you will be good.
Create a background script and define an entry for background script in manifest.json
In background script, define a handler on chrome.browserAction.onClicked.
Inside that handler, extract the url of current tab and check if it is the expected url, then just change the url of that tab.