JS-Snippet – ReLink URLs For A Quick Download From GitHub

Naturally GitHub is not “THE” place for binary files sharing,
but it used quite often as one (yep! even Google does it..).

It is a great place to put files.. by downloading it,
is not always the fastest way, unless the author had some basic understanding,
and provided you with direct-links to the files, which may be easily put in the README..

this is probably what you’ll normally do..

walking through:


Since the structure of the direct-download links is known,
at least for now (it did changed few times before..)
you can “fix” the links, to point to the direct URL..

not only does it relink, so you can click and download
or copy the links to your download-manager (one by one),
it also generates a links, and outputs it to the console.

amm…naturally it is an ad-hoc solution..

NodeList.prototype.map = Array.prototype.map;

var list = document.querySelectorAll('[href*="/blob/"]').map(function(a){
  var URL = a.href; //js-object .href includes the domain for relative URLs.

  //fix URL to point to the file, directly.
  URL = URL.replace(/http\:/g, "https:")                                                 //make sure it is https:
           .replace(/^https\:\/\/github.com\//gi, "https://raw.githubusercontent.com/")  //this is the current way to download
           .replace(/\/blob\//g, "/")                                                     //remove this too...
  a.setAttribute("href", URL);  //explicitly set "href" attribute.

  //will also help downloading.
  a.setAttribute("download", a.innerText.replace(/[\r\n]/g));
  a.setAttribute("type", "application/octet-stream");

  return URL;