Skip to content

Instantly share code, notes, and snippets.

@mferrier
Forked from antichris/about.md
Last active August 29, 2015 14:06
Show Gist options
  • Save mferrier/31652fa8dc01d2fce54d to your computer and use it in GitHub Desktop.
Save mferrier/31652fa8dc01d2fce54d to your computer and use it in GitHub Desktop.

Fork your own Gist

This is a bookmarklet that adds a fully functional Fork button to your own Gist.

If a Fork button is already present in the page, this bookmarklet will set focus to it instead of adding another one.

The change is temporary and the button will disappear as soon as you navigate away from that Gist (clicking the Fork button does this for you as well).


A Closure Compiler optimized version provided below for convenient copy'n'paste.

javascript: (function () {
var fork, present, star, button, form, forks, count;
$('.pagehead-actions li').each(function () {
var action = $(this).find('form').attr('action');
if (action) {
switch (action.split('/').pop()) {
case 'fork':
fork = $(this);
present = true;
break;
case 'star':
star = this;
break;
}
}
});
if (!fork) {
fork = $(star).clone();
}
button = fork.find('.minibutton');
if (present) {
button.focus();
return;
}
form = fork.find('form');
form.attr('action', form.attr('action').split('/').map(function (a) {
return a === 'star' ? 'fork' : a;
}).join('/'));
button.html('<span class="octicon octicon-git-branch"></span>Fork');
forks = $('.counter').filter(function () {
var href = $(this).parent().attr('href');
return href ? href.split('/').some(function (a) {
return a === 'forks';
}) : false;
});
count = fork.find('.social-count');
count.html(forks.text() || 0);
count.attr('href', forks.parent().attr('href'));
$('.pagehead-actions li:last').parent().append(fork);
}());
javascript:(function(){var b,e,f,c,d;$(".pagehead-actions li").each(function(){var a=$(this).find("form").attr("action");if(a)switch(a.split("/").pop()){case "fork":b=$(this);e=!0;break;case "star":f=this}});b||(b=$(f).clone());c=b.find(".minibutton");e?c.focus():(d=b.find("form"),d.attr("action",d.attr("action").split("/").map(function(a){return"star"===a?"fork":a}).join("/")),c.html('<span class="octicon octicon-git-branch"></span>Fork'),c=$(".counter").filter(function(){var a=$(this).parent().attr("href");return a?a.split("/").some(function(a){return"forks"===a}):!1}),d=b.find(".social-count"),d.html(c.text()||0),d.attr("href",c.parent().attr("href")),$(".pagehead-actions li:last").parent().append(b))})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment