forked from Mottie/GitHub-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist-raw-links.user.js
125 lines (115 loc) · 3.73 KB
/
gist-raw-links.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
124
125
// ==UserScript==
// @name Gist Raw Links
// @version 0.2.2
// @description Add a button that contains a list of gist raw file links
// @license MIT
// @author Rob Garrison
// @namespace https://github.com/Mottie
// @include https://gist.github.com/*
// @run-at document-idle
// @grant GM_addStyle
// @grant GM.addStyle
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// @connect api.github.com
// @connect assets-cdn.github.com
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
// @icon https://github.githubassets.com/pinned-octocat.svg
// @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/gist-raw-links.user.js
// @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/gist-raw-links.user.js
// @supportURL https://github.com/Mottie/GitHub-userscripts/issues
// ==/UserScript==
/* global GM */
(() => {
"use strict";
GM.addStyle(`
.ghrl-get-list, .ghrl-files a { cursor:pointer; }
.ghrl-files div { text-align:center; }
.gist-count-links { z-index: 21; }
`);
const item = document.createElement("li");
item.className = "d-inline-block mr-3";
function addButton(node) {
const button = item.cloneNode();
button.innerHTML = `
<details class="details-reset details-overlay select-menu ghrl-wrapper">
<summary class="select-menu-button" aria-haspopup="menu">
<span class="ghrl-get-list" data-menu-button>🍣 Raw urls</span>
</summary>
<details-menu class="select-menu-modal position-absolute ghrl-files" style="z-index: 99;" role="menu" aria-label="Raw gist links">
<div class="select-menu-list">
<img src="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif" width="32">
</div>
</details-menu>
</details>`;
node.insertBefore(button, node.childNodes[0]);
}
function update() {
const gists = $$(".gist-snippet");
let indx = gists.length;
if (indx) {
while (indx--) {
// only save dabblet files from list
if (!$(".ghrl-get-list", gists[indx])) {
addButton($(".gist-snippet-meta ul", gists[indx]));
}
}
}
}
function addList(link, files) {
let html = "";
Object.keys(files).forEach(file => {
// remove version sha from raw_url to always point at
// the latest version of the file - see #18
const url = files[file].raw_url.replace(/raw\/\w+\//, "raw/");
html += `
<a href="${url}" class="select-menu-item ghrl-file" role="menuitem">
${file}
</a>`;
});
$(".ghrl-files", link.closest("li")).innerHTML = html;
}
function loadFileList(link) {
let el = link.closest("li");
el = $("a", el && el.nextElementSibling);
if (el) {
const gistid = el.href.split("/").slice(-1);
GM.xmlHttpRequest({
method : "GET",
url : `https://api.github.com/gists/${gistid}`,
onload : response => {
if (response.status !== 200) {
$(".ghrl-files", link.parentNode).innerHTML = response.message;
return console.error(response);
}
let json = false;
try {
json = JSON.parse(response.responseText);
} catch (err) {
return console.error(`Invalid JSON for gist ${gistid}`);
}
if (json && json.files) {
addList(link, json.files);
}
}
});
}
}
function addBindings() {
document.addEventListener("click", function(event) {
const target = event.target.closest("details");
if (target && target.classList.contains("ghrl-wrapper")) {
loadFileList(target);
}
});
}
function $(str, el) {
return (el || document).querySelector(str);
}
function $$(str, el) {
return Array.from((el || document).querySelectorAll(str));
}
document.addEventListener("pjax:end", update);
update();
addBindings();
})();