Github raw.githack.com Button

A userscript to add raw.githack.com button on Github.

目前為 2019-04-03 提交的版本,檢視 最新版本

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