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.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. // @require https://cdnjs.cloudflare.com/ajax/libs/sentinel-js/0.0.7/sentinel.min.js
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. (() => {
  17. /* global sentinel */
  18. const SELCTOR = [
  19. ".file-actions a.Button[href*='/raw/']:not(.raw-githack-detected)", // gist
  20. "a[data-testid='raw-button']:not(.raw-githack-detected)", // github
  21. ].join(",")
  22.  
  23. sentinel.on(SELCTOR, el => createButton(el));
  24.  
  25. // replace();
  26. // function replace(btn){
  27. // var btns, i;
  28. // btns = document.querySelectorAll();
  29. // for (i = 0; i < btns.length; i++) {
  30. // if (btns[i].textContent == "Raw") {
  31. // createButton(btns[i]);
  32. // }
  33. // }
  34. // }
  35.  
  36. function createButton(btn) {
  37. btn.classList.add("raw-githack-detected");
  38. if (btn.textContent.trim() !== "Raw") {
  39. return;
  40. }
  41.  
  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. if (!/btn-group|ButtonGroup/.test(btn.parentNode.className)) {
  56. const parent = btn.parentNode;
  57. const group = document.createElement("div");
  58. group.className = "btn-group";
  59. while (parent.childNodes.length) {
  60. group.appendChild(parent.childNodes[0]);
  61. }
  62. parent.appendChild(group);
  63. }
  64.  
  65. const group = btn.parentNode;
  66. for (let i = 0; i < group.children.length; i++) {
  67. if (i < group.children.length - 1) {
  68. group.children[i].classList.add("rounded-right-0", "border-right-0");
  69. }
  70. if (i >= 1) {
  71. group.children[i].classList.add("rounded-left-0");
  72. }
  73. }
  74. }
  75. })();