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 Updated by Shane Gadsby from and edit 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.2
  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.  
  18. btns = document.querySelectorAll("#raw-url:not(.rawgit),.file-actions a:not(.rawgit)");
  19. for (i = 0; i < btns.length; i++) {
  20. if (btns[i].textContent == "Raw") {
  21. createButton(btns[i]);
  22. }
  23. }
  24. }
  25.  
  26. function createButton(btn) {
  27. var url = btn.href;
  28. if (url.indexOf("gist.github.com") >= 0) {
  29. url = url.replace("gist.github.com", "gitcdn.xyz/repo");
  30. } else {
  31. url = url.replace(/github\.com\/([^/]+\/[^/]+)\/raw/, "gitcdn.xyz/repo/$1");
  32. }
  33.  
  34. var newBtn = btn.cloneNode(false);
  35. newBtn.href = url;
  36. newBtn.textContent = "GitCDN";
  37. newBtn.removeAttribute("id");
  38.  
  39. btn.parentNode.insertBefore(newBtn, btn.nextSibling);
  40. btn.classList.add("gitcdn");
  41.  
  42. if (!btn.parentNode.classList.contains("btn-group")) {
  43. var parent = btn.parentNode,
  44. group = document.createElement("div");
  45. group.className = "btn-group";
  46. while (parent.childNodes.length) {
  47. group.appendChild(parent.childNodes[0]);
  48. }
  49. parent.appendChild(group);
  50. }
  51.  
  52. }
  53.  
  54. var container =
  55. document.querySelector("#js-repo-pjax-container") ||
  56. document.querySelector("#js-pjax-container");
  57.  
  58. if (container) {
  59. new MutationObserver(function(){
  60. replace();
  61. }).observe(container, {childList: true, subtree: true});
  62. }
  63.  
  64. replace();