Skip to content

Instantly share code, notes, and snippets.

@Cologler
Last active September 13, 2021 02:19
Show Gist options
  • Save Cologler/c7c2deaf2a70bdebfe93f0bf797c369c to your computer and use it in GitHub Desktop.
Save Cologler/c7c2deaf2a70bdebfe93f0bf797c369c to your computer and use it in GitHub Desktop.
add 'Raw (Newest)' button to gist.
// ==UserScript==
// @name gist: Raw (Newest) button
// @namespace https://github.com/cologler/
// @version 0.2.0
// @description add 'Raw (Newest)' button to gist.
// @author Cologler
// @match https://gist.github.com/*/*
// @connect gist.githubusercontent.com
// @noframes
// @license MIT
// @require https://cdn.jsdelivr.net/gh/Cologler/[email protected]/dist/dom.js
// ==/UserScript==
(function() {
'use strict';
function getRawNewestUrl(url) {
let m = url.match(/\/raw\/[0-9a-f]{40}\//);
return url.replace(m[0], '/raw/');
}
function addNewestRawButton(cloneSrc, url) {
const newBtn = cloneSrc.cloneNode(true);
newBtn.style.marginRight = '6px';
const a = newBtn.querySelector('a');
a.textContent = 'Raw (Newest)';
a.href = getRawNewestUrl(url);
cloneSrc.parentElement.insertBefore(newBtn, cloneSrc.nextSibling);
}
function main() {
if (location.hostname === 'gist.github.com') {
Dom.on('div.file-actions', z => {
for (const link of z.querySelectorAll('a')) {
if (link && link.textContent?.trim() === 'Raw') {
const url = link.href;
addNewestRawButton(z, url);
break;
}
}
});
}
}
main();
})();
@Cologler
Copy link
Author

If anyone sees two Copy buttons on files, it is caused by Refined Github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment