Github raw.githack.com Button

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

Verze ze dne 20. 11. 2024. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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");
      }
    }
  }
})();