Github raw.githack.com Button

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Github raw.githack.com Button
// @version 0.2.1
// @description A userscript to add raw.githack.com button on Github.
// @homepageURL https://github.com/eight04/raw-githack-button#readme
// @supportURL https://github.com/eight04/raw-githack-button/issues
// @license MIT
// @author eight <[email protected]> (https://github.com/eight04)
// @namespace https://github.com/eight04
// @include https://github.com/*
// @include https://gist.github.com/*
// @require https://cdnjs.cloudflare.com/ajax/libs/sentinel-js/0.0.7/sentinel.min.js
// @grant none
// ==/UserScript==

(() => {
  /* global sentinel */
  const SELCTOR = [
    ".file-actions a.Button[href*='/raw/']:not(.raw-githack-detected)", // gist
    "a[data-testid='raw-button']:not(.raw-githack-detected)", // github
  ].join(",")

  sentinel.on(SELCTOR, el => createButton(el));

  // replace();
  
  // function replace(btn){
  //   var btns, i;
  //   btns = document.querySelectorAll();
  //   for (i = 0; i < btns.length; i++) {
  //     if (btns[i].textContent == "Raw") {
  //       createButton(btns[i]);
  //     }
  //   }
  // }

  function createButton(btn) {
    btn.classList.add("raw-githack-detected");
    if (btn.textContent.trim() !== "Raw") {
      return;
    }

    var url = btn.href;
    if (url.indexOf("gist.github.com") >= 0) {
      url = url.replace("gist.github.com", "gist.githack.com");
    } else {
      url = url.replace(/github\.com\/([^/]+\/[^/]+)\/raw/, "raw.githack.com/$1");
    }

    var newBtn = btn.cloneNode(false);
    newBtn.href = url;
    newBtn.textContent = "Raw Githack";
    newBtn.removeAttribute("id");

    btn.parentNode.insertBefore(newBtn, btn.nextSibling);
    
    if (!/btn-group|ButtonGroup/.test(btn.parentNode.className)) {
      const parent = btn.parentNode;
      const group = document.createElement("div");
      group.className = "btn-group";
      while (parent.childNodes.length) {
        group.appendChild(parent.childNodes[0]);
      }
      parent.appendChild(group);
    }

    const group = btn.parentNode;
    for (let i = 0; i < group.children.length; i++) {
      if (i < group.children.length - 1) {
        group.children[i].classList.add("rounded-right-0", "border-right-0");
      }
      if (i >= 1) {
        group.children[i].classList.add("rounded-left-0");
      }
    }
  }
})();