This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgitcdn-button.user.js
64 lines (53 loc) · 1.62 KB
/
gitcdn-button.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ==UserScript==
// @name Github GitCDN Button
// @namespace eight04.blogspot.com
// @author Updated by Shane Gadsby from and edit by Mikhoul based on eight04.blogspot.com Userscript
// @description An userscript to add "GitCDN" button on github.
// @include https://github.com/*
// @include https://gist.github.com/*
// @version 1.0.2
// @grant none
// ==/UserScript==
"use strict";
function replace(){
// Check if raw-url button exists
var btns, i;
btns = document.querySelectorAll("#raw-url:not(.rawgit),.file-actions a:not(.rawgit)");
for (i = 0; i < btns.length; i++) {
if (btns[i].textContent == "Raw") {
createButton(btns[i]);
}
}
}
function createButton(btn) {
var url = btn.href;
if (url.indexOf("gist.github.com") >= 0) {
url = url.replace("gist.github.com", "gitcdn.xyz/repo");
} else {
url = url.replace(/github\.com\/([^/]+\/[^/]+)\/raw/, "gitcdn.xyz/repo/$1");
}
var newBtn = btn.cloneNode(false);
newBtn.href = url;
newBtn.textContent = "GitCDN";
newBtn.removeAttribute("id");
btn.parentNode.insertBefore(newBtn, btn.nextSibling);
btn.classList.add("gitcdn");
if (!btn.parentNode.classList.contains("btn-group")) {
var parent = btn.parentNode,
group = document.createElement("div");
group.className = "btn-group";
while (parent.childNodes.length) {
group.appendChild(parent.childNodes[0]);
}
parent.appendChild(group);
}
}
var container =
document.querySelector("#js-repo-pjax-container") ||
document.querySelector("#js-pjax-container");
if (container) {
new MutationObserver(function(){
replace();
}).observe(container, {childList: true, subtree: true});
}
replace();