Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active September 2, 2023 13:03
Show Gist options
  • Save noromanba/2370972 to your computer and use it in GitHub Desktop.
Save noromanba/2370972 to your computer and use it in GitHub Desktop.
add permanently HEAD links on Gist for UserScript

gist raw perm HEAD userscripts.org #130781

Install

When you install user.js strongly recommends on userscripts.org. NOT HERE.

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://raw.github.com/gist/2370972/

HEAD(named)

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

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

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

Known Problems

Multifiles Gist

HEAD can accessible 1st file only. That is Gist API constraints.

Binary 1st file

e.g. https://gist.github.com/890279/

Access to raw: https://raw.github.com/gist/890279/. Expected https://raw.github.com/gist/890279/simple320_320.png, but actual API return 2nd text file data.

Gist API

Multifiles Gist has problem. Gist API is not provide true permalink(called HEAD) for 2nd or more files.

Include content and history. it's too long too much than v1(closed).

See also

Gist HTTP

e.g. https://gist.github.com/2370972

  • GET 'raw' => HTTP 302

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

  • => HTTP 200 OK

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

Bookmarklet

Base code is small Bookmarklet, you can see based rev.. Latest rev. permalink is gist raw perm HEAD - Hatena::Let.

// ==UserScript==
// @name gist raw perm HEAD
// @namespace https://www.hatena.ne.jp/noromanba/
// @description Add permanently HEAD links on Gist for UserScript
// @include https://gist.github.com/*
// @version 2012.12.23.3
// @homepage https://userscripts.org/scripts/show/130781
// @downloadURL https://userscripts.org/scripts/source/130781.user.js
// @installURL https://userscripts.org/scripts/source/130781.user.js
// @updateURL https://userscripts.org/scripts/source/130781.meta.js
// @license MIT License http://nrm.mit-license.org/2012
// @copyright (c) 2012 noromanba https://www.hatena.ne.jp/noromanba/
// @author noromanba
// @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 (Public Domain by Mizunoryu, Badseed, Jacobolus)
// https://commons.wikimedia.org/wiki/File:Color_icon_orange_v2.svg
// Devel
// https://gist.github.com/2370972
// TODO smart pjax handling
(function() {
var appendButton = function () {
if (!(/^https:\/\/gist\.github\.com\/[\w-]+/.test(location.href))) return;
// TODO refactor to Gist{}
var gistfile = function (rawNode) {
var raw = rawNode,
meta = raw.pathname.match(/\/raw\/([\w-]+)\/[\w-]+\/(.*)/),
baseurl = 'https://raw.github.com/gist/',
id = meta[1],
basename = meta[2];
return {
raw: raw,
id: id,
name: basename,
data: {
name: basename.slice(basename.lastIndexOf('/') + 1),
type: basename.split('.').length > 1 ? basename.split('.').pop() : 'N/D'
},
permalink: baseurl + id + '/'
};
};
var gist = []; // more smart, into a gist{}
Array.prototype.forEach.call(document.querySelectorAll('.actions .raw-url'), function (raw) {
var namedHead = raw.cloneNode(true);
var file = gistfile(raw);
gist.push(file);
// TODO remove redundant attr
namedHead.href = file.permalink + file.name;
namedHead.textContent = 'HEAD(named)';
namedHead.title = file.name;
// Firefox must be needs 2nd args c.f. https://developer.mozilla.org/en/DOM/element.insertBefore
raw.parentNode.insertBefore(namedHead, null);
});
// https://raw.github.com/gist/ID/ => return 1st non-binary file. Gist API v3 protocol
gist.some(function (file) {
// TODO xhr to Gist API and get filetype, parse json or response header
if (/(?:png|bmp|jpe?g|gif)$/i.test(file.data.type)) return;
var head = file.raw.cloneNode(true);
head.href = file.permalink;
head.textContent = 'HEAD';
head.title = file.id + '.txt';
file.raw.parentNode.insertBefore(head, file.raw.nextSibling);
return true;
});
};
appendButton();
// or .site-content
/*/
Array.prototype.forEach.call(document.querySelectorAll('a[class="js-current-repository"], .gist-item .creator a'), function (trigger) {
trigger.addEventListener('click', function () {
appendButton();
}, false);
});
//*/
})();
<h3>About</h3>
<strong>Add permanently HEAD link on Gist</strong>
<p>This user.js add 'HEAD' link. It's takes <strong>shrinked permanent raw link</strong> like this:</p>
<pre>
https://raw.github.com/gist/2370972/gist-raw-perm-HEAD.user.js
</pre>
<p><div>
<img src="http://cdn-ak.f.st-hatena.com/images/fotolife/n/noromanba/20120413/20120413072715.png" alt="gist-raw-perm-HEAD" width="600px" />
</div></p>
<h3>Detail</h3>
<p>Show technical details on <a href="https://gist.github.com/2370972" target="_blank">Gist: 2370972</a>.</p>
<h3>:-)</h3>
<p>Enjoy Gist and Coding!</p>
Regards,<br />
noromanba
@LouCypher
Copy link

The new Gist broke your script \( ><)シ

@noromanba
Copy link
Author

@LouCypher
thank you for your infomation :-)

broken

I know, maybe gist API changes too. but GitHub not provide information. hum..
a similar kind as shown below

feed => HTTP406

can't get gists feed

https://gist.github.com/noromanba.atom

Subscribe Gists - Hatena::Let

i.e. mulfunction open feed on flavors.me

blame UI

where is panel which put log?

too drastic.. I’m stucking X-(
I don't have much time now, a correction takes time.

FYI

didn't notification by Github, I'm late.
try @USERNAME with comments

Regards,
noromanba

@noromanba
Copy link
Author

@LouCypher
quick fix 🚑 now. but incompletly yet. pjax handling.........🐌
if unworks it, plz try reload

@LouCypher
Copy link

Thanks for the updates
       «:dancers::dancers:ヾ

@noromanba
Copy link
Author

@LouCypher
somehow handle pjax:sleepy::dizzy: thx for your patience:pray:

@jerone
Copy link

jerone commented Jan 29, 2013

Would be great if you get this to work with the new style and upload new version to USO.

@noromanba
Copy link
Author

stuck in... 💭 take away freedom by CSP 🔒

@noromanba
Copy link
Author

@jerone sry I'm late. thx. USO has gone, and fork sites not good enough. e.g. GF; extreme security

@denilsonsa
Copy link

Hey, @noromanba, I've updated the script to work on the current GitHub markup. Look at https://gist.github.com/denilsonsa/f2e5b9ee53e633e20165 and please update your version. :)

@denilsonsa
Copy link

FYI, it was broken again, and I've fixed it again: https://gist.github.com/denilsonsa/f2e5b9ee53e633e20165

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