Skip to content

Instantly share code, notes, and snippets.

@denilsonsa
Forked from noromanba/README.md
Last active August 15, 2023 05:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denilsonsa/f2e5b9ee53e633e20165 to your computer and use it in GitHub Desktop.
Save denilsonsa/f2e5b9ee53e633e20165 to your computer and use it in GitHub Desktop.
add permanently HEAD links on Gist for UserScript

gist raw perm HEAD

Install

About

Visit Gist and see 'raw' link, It's including SHA-1 long hash name like this.

https://gist.github.com/raw/2370972/47150711fedb7375d19d761ab122485b8105678d/gist-raw-perm-HEAD.user.js

gist-raw-HEAD

This user.js provide 'HEAD' links. It's takes shrinked permanent raw link and shrinked named raw link when you browse Gist files.

HEAD

True Permalink: This is absolutery permanent link but it's always filetype: txt.

https://gist.github.com/raw/2370972/

HEAD(named)

Almost Permalink: This is almost permanent link. It's strict filetype by extension. This link broken when filename changed.

https://gist.github.com/raw/2370972/gist-raw-perm-HEAD.user.js

This link most useful for install user.js from Gist.

See also

// ==UserScript==
// @name gist raw perm HEAD
// @namespace http://www.hatena.ne.jp/noromanba/
// @description Add permanently HEAD link on Gist for UserScript
// @include https://gist.github.com/*
// @exclude https://gist.github.com/*/*/revisions
// @grant none
// @version 2023.05.24.0
// @homepage https://gist.github.com/2370972
// @downloadURL https://gist.github.com/denilsonsa/f2e5b9ee53e633e20165/raw/gist-raw-perm-HEAD.user.js
// @license MIT License http://nrm.mit-license.org/2012
// @author noromanba http://noromanba.flavors.me
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Color_icon_orange_v2.svg/32px-Color_icon_orange_v2.svg.png
// @icon64 https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Color_icon_orange_v2.svg/64px-Color_icon_orange_v2.svg.png
// ==/UserScript==
// Icon (PD by Mizunoryu, Badseed, Jacobolus)
// https://commons.wikimedia.org/wiki/File:Color_icon_orange_v2.svg
(function () {
var appendButton = function code() {
if (!/^https:\/\/gist\.github\.com\/[-\w]+\/\w+/.test(location.href)) {
return;
}
var links = document.body.querySelectorAll('.file-header .file-actions a.Button');
if (!links) {
return;
}
Array.prototype.forEach.call(links, function (hashlink) {
// <a class="Button--secondary Button--small Button" href="/noromanba/2370972/raw/87006e128def317d4e10e8fce2ebe62d0530c214/README.md">
// <span class="Button-content">
// <span class="Button-label">
// Raw
// </span>
// </span>
// </a>
// RegExp groups:
// 1 = prefix
// 2 = hash
// 3 = filename
var match = /(.*\/raw)\/([0-9a-fA-F]+)\/([^/]+)$/.exec(hashlink.href);
if (!match) {
return;
}
var perm = document.createElement('a');
perm.textContent = 'HEAD';
perm.classList.add('Button', 'Button--small', 'Button--secondary');
perm.href = match[1] + '/' + match[3];
perm.title = match[3];
// Inserting the HEAD button right after the Raw button.
hashlink.parentNode.insertBefore(perm, hashlink.nextSibling);
});
};
appendButton();
// FIXME 100% cpu usage
/*
// c.f. MutationObserver
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
// http://caniuse.com/#feat=mutationobserver
new MutationObserver(function (records) {
//records.forEach(function (record) {
appendButton();
//});
}).observe(document.body, { childList: true, subtree: true });
*/
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment