-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_copy_code.user.js
123 lines (110 loc) · 4.34 KB
/
github_copy_code.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ==UserScript==
// @name GitHub Copy Code
// @description Add a "copy" button when browsing GitHub source code.
// @namespace https://github.com/Skinner927/greasemonkey-scripts
// @updateURL https://github.com/Skinner927/greasemonkey-scripts/raw/master/github_copy_code.user.js
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @author skinner927
// @version 1.0
// @match *://github.com/*
// @match *://gist.github.com/*
// @connect github.com
// @connect raw.githubusercontent.com
// @connect gist.github.com
// @connect gist.githubusercontent.com
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function () {
var rawCopyClass = "raw-copy-button";
var disabled = "disabled";
var greenBg = "#a8f1c6";
var redBg = "#f6a7a3";
function resetCopyBg(element) {
if (!element) {
return;
}
if (element.bgTimeout) {
window.clearTimeout(element.bgTimeout);
element.bgTimeout = null;
}
var svg = button.querySelector("svg");
if (svg) {
svg.style.display = "none";
}
element.style.background = "";
element.classList.remove(disabled);
}
function copyDone(success, button) {
if (!button) {
return;
}
resetCopyBg(button);
button.style.background = success ? greenBg : redBg;
button.bgTimeout = window.setTimeout(resetCopyBg.bind(null, button), 1000);
}
function copyClickedHandler(url) {
return function copyClickedAction(e) {
if (!e || !e.target || !e.target.classList.contains(rawCopyClass)) {
return;
}
var svg = e.target.querySelector("svg");
if (!svg) {
return;
}
resetCopyBg(e.target);
e.preventDefault();
if (e.target.classList.contains(disabled)) {
return;
}
e.target.classList.add(disabled);
svg.style.display = "block";
// Request the raw page
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function copyOnLoad(result) {
GM_setClipboard(result.responseText, "text/plain");
copyDone(true, e.target);
},
onerror: copyDone.bind(null, false, e.target),
});
};
}
function addCopyButton(rawButton, isGist) {
/* <a class="btn-sm btn BtnGroup-item">Raw</a>*/
var a = document.createElement("A");
a.classList.add("btn-sm", "btn", rawCopyClass);
if (!isGist) {
a.classList.add("BtnGroup-item");
}
a.style.position = "relative";
// https://github.com/SamHerbert/SVG-Loaders
a.innerHTML =
'Copy<svg stroke="currentColor" style="position: absolute; top: 0; right: 0; display: none;" width="16" height="16" viewBox="0 0 44 44" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke-width="2"><circle cx="22" cy="22" r="1"><animate attributeName="r" begin="0s" calcMode="spline" dur="1.8s" keySplines="0.165, 0.84, 0.44, 1" keyTimes="0; 1" repeatCount="indefinite" values="1; 20"/><animate attributeName="stroke-opacity" begin="0s" calcMode="spline" dur="1.8s" keySplines="0.3, 0.61, 0.355, 1" keyTimes="0; 1" repeatCount="indefinite" values="1; 0"/></circle><circle cx="22" cy="22" r="1"><animate attributeName="r" begin="-0.9s" calcMode="spline" dur="1.8s" keySplines="0.165, 0.84, 0.44, 1" keyTimes="0; 1" repeatCount="indefinite" values="1; 20"/><animate attributeName="stroke-opacity" begin="-0.9s" calcMode="spline" dur="1.8s" keySplines="0.3, 0.61, 0.355, 1" keyTimes="0; 1" repeatCount="indefinite" values="1; 0"/></circle></g></svg>';
a.addEventListener("click", copyClickedHandler(rawButton.href + ""));
rawButton.parentElement.appendChild(a);
}
setInterval(function startGitHubRawDownload() {
// Check if our button is on the page already
if (document.querySelector("." + rawCopyClass)) {
return;
}
if (0 == window.location.host.indexOf("gist.")) {
// Find the "raw" button on gists
var elements = document.querySelectorAll(".file-actions > a");
if (!elements) {
return;
}
for (var i = 0; i < elements.length; i++) {
if ("Raw" == elements[i].innerText) {
addCopyButton(elements[i], true);
}
}
} else {
// "Raw" button
var rawButton = document.getElementById("raw-url");
addCopyButton(rawButton, false);
}
}, 250);
})();