Github raw.githack.com Button

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

目前为 2024-11-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Github raw.githack.com Button
  3. // @version 0.2.0
  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. /* global sentinel */
  17. const SELCTOR = [
  18. ".file-actions a.Button[href*='/raw/']:not(.raw-githack-detected)", // gist
  19. "a[data-testid='raw-button']:not(.raw-githack-detected)", // github
  20. ].join(",")
  21.  
  22. sentinel.on(SELCTOR, el => createButton(el));
  23.  
  24. // replace();
  25. // function replace(btn){
  26. // var btns, i;
  27. // btns = document.querySelectorAll();
  28. // for (i = 0; i < btns.length; i++) {
  29. // if (btns[i].textContent == "Raw") {
  30. // createButton(btns[i]);
  31. // }
  32. // }
  33. // }
  34.  
  35. function createButton(btn) {
  36. btn.classList.add("raw-githack-detected");
  37. if (btn.textContent.trim() !== "Raw") {
  38. return;
  39. }
  40.  
  41. var url = btn.href;
  42. if (url.indexOf("gist.github.com") >= 0) {
  43. url = url.replace("gist.github.com", "gist.githack.com");
  44. } else {
  45. url = url.replace(/github\.com\/([^/]+\/[^/]+)\/raw/, "raw.githack.com/$1");
  46. }
  47.  
  48. var newBtn = btn.cloneNode(false);
  49. newBtn.href = url;
  50. newBtn.textContent = "Raw Githack";
  51. newBtn.removeAttribute("id");
  52.  
  53. btn.parentNode.insertBefore(newBtn, btn.nextSibling);
  54. if (!/btn-group|ButtonGroup/.test(btn.parentNode.className)) {
  55. const parent = btn.parentNode;
  56. const group = document.createElement("div");
  57. group.className = "btn-group";
  58. while (parent.childNodes.length) {
  59. group.appendChild(parent.childNodes[0]);
  60. }
  61. parent.appendChild(group);
  62. }
  63.  
  64. const group = btn.parentNode;
  65. for (let i = 0; i < group.children.length; i++) {
  66. if (i < group.children.length - 1) {
  67. group.children[i].classList.add("rounded-right-0", "border-right-0");
  68. }
  69. if (i >= 1) {
  70. group.children[i].classList.add("rounded-left-0");
  71. }
  72. }
  73. }
  74. })();