Skip to content Skip to sidebar Skip to footer

Capture Http Status 300 For Redirect Download Links

I'm developing a Chrome Extension to download links on a webpage. I came across some links there were not a direct download, for example like this one http://downloads.sourceforge.

Solution 1:

This is possible with the use of the chrome.webRequest api. 302's and the like are normally handled silently by the browser, but you can use this method to stop them or make them more visible to your code.

chrome.webRequest.onHeadersReceived.addListener(function(details){
    var redirUrl;
    details.responseHeaders.forEach(function(v,i,a){
      if(v.name == "Location"){
       redirUrl = v.value;
       details.responseHeaders.splice(i,1);  //Kill the redirect
      }
    });
    if(redirUrl)
      details.responseHeaders.push({name:"redirUrl",value:redirUrl});
    return {responseHeaders:details.responseHeaders}; 
},
{urls: ["http://*/*"],tabId:-1},["responseHeaders","blocking"]);

I specified a tabId of -1, which means it should only work for requests not coming from a tab, i.e. a background page. This will silently prevent the redirect while allowing you access to both the 302 status and the url to redirect to.

Post a Comment for "Capture Http Status 300 For Redirect Download Links"