Github GitCDN Button

An userscript to add "GitCDN" button on github.

  1. // ==UserScript==
  2. // @name Github GitCDN Button
  3. // @namespace eight04.blogspot.com
  4. // @author by Mikhoul based on eight04.blogspot.com Userscript
  5. // @description An userscript to add "GitCDN" button on github.
  6. // @include https://github.com/*
  7. // @include https://gist.github.com/*
  8. // @version 1.0.1
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. "use strict";
  13.  
  14. function replace(){
  15. // Check if raw-url button exists
  16. var btns, i;
  17. btns = document.querySelectorAll(".file-actions a:not(.rawgit)");
  18. for (i = 0; i < btns.length; i++) {
  19. if (btns[i].textContent == "Raw") {
  20. createButton(btns[i]);
  21. }
  22. }
  23. }
  24.  
  25. function createButton(btn) {
  26. var url = btn.href;
  27. if (url.indexOf("gist.github.com") >= 0) {
  28. url = url.replace("gist.github.com", "rawgit.com");
  29. } else {
  30. url = url.replace(/github\.com\/([^/]+\/[^/]+)\/raw/, "gitcdn.xyz/repo/$1");
  31. }
  32.  
  33. var newBtn = btn.cloneNode(false);
  34. newBtn.href = url;
  35. newBtn.textContent = "GitCDN";
  36. newBtn.removeAttribute("id");
  37.  
  38. btn.parentNode.insertBefore(newBtn, btn.nextSibling);
  39. btn.classList.add("rawgit");
  40.  
  41. if (!btn.parentNode.classList.contains("btn-group")) {
  42. var parent = btn.parentNode,
  43. group = document.createElement("div");
  44. group.className = "btn-group";
  45. while (parent.childNodes.length) {
  46. group.appendChild(parent.childNodes[0]);
  47. }
  48. parent.appendChild(group);
  49. }
  50. }
  51.  
  52. var container =
  53. document.querySelector("#js-repo-pjax-container") ||
  54. document.querySelector("#js-pjax-container");
  55.  
  56. if (container) {
  57. new MutationObserver(function(){
  58. replace();
  59. }).observe(container, {childList: true, subtree: true});
  60. }
  61.  
  62. replace();