Skip to content

Instantly share code, notes, and snippets.

@themadsens
Last active February 15, 2017 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themadsens/d37d5c1ed2f5124919928b41fb51c76a to your computer and use it in GitHub Desktop.
Save themadsens/d37d5c1ed2f5124919928b41fb51c76a to your computer and use it in GitHub Desktop.
Install: Copy from 'javascript:' onwards, and paste into a bookmark. Usage: Triple click filename in github and click bookmark
// javascript:
(function() {
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
var rex = /<a href="\/(.+?)\/(.+?)\/(?!releases\/)(?:(?:blob|raw)\/)?(.+?)\/([^"]+)"/i;
var sel = getSelectionHtml();
var val = sel.match(rex);
var res = val ? 'Copy URL:\nhttps://rawgit.com/'+val[1]+'/'+val[2]+'/'+val[3]+'/'+val[4]
: "Triple click file to select, then click this bookmarklet";
console.log("RES", val, res);
alert(res);
res = null;
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment