Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Forked from Noitidart/about.md
Created April 14, 2016 16:49
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 RichardBronosky/17aa992fcd1539d339b31ea64f12fd78 to your computer and use it in GitHub Desktop.
Save RichardBronosky/17aa992fcd1539d339b31ea64f12fd78 to your computer and use it in GitHub Desktop.
Adds a fully functional "Fork" button to your own Gist.

Fork your own Gist

This is a script 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). Meaning you will have to run the script every new page load.

Firefox

Copy the contents from bookmarklet.js, open Scracthpad (Ctrl+F4), paste it there. Back in browser, swwitch to tab with your Gist you want to fork. Back in Scratchpad, "Run" it. Save and/or bookmark the Scratchpad file for future use.

An addon to do this automatically is available. It is called ghForkable and is found at AMO here.

In browsers that support bookmarlets (IE, Google Chrome, Opera)

To run this bookmarklet, switch to tab with your Gist you want to Fork. Then copy paste the Closure Compiler optimized version provided below into you URL bar and hit enter.

Note for Firefox Firefox does not allow bookmarklets, meaning you cannot paste this in your URL bar, and thus you cannot bookmark it. You have to open the scratchpad (Shift + F4) and then paste it there and run it. Can copy and paste either version, bookmarklet.js or closure-compiled.js to scratchpad. You can save the scratchpad file and reuse it, just like a bookmark.

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;
case 'unstar':
star = this;
break;
}
}
});
if (!fork) {
if (!star) {
alert('No "Star" or "Unstar" button on this page. Leading me to believe this is not a page that can be forked.');
return;
}
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' || a == 'unstar' ? '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'));
if (count[0].tagName == 'SPAN') {
count.addClass('js-social-count'); //if its a span it doesnt have this class
count.replaceWith(function () {
var attrs = {};
$.each(this.attributes, function (index, attr) {
attrs[attr.name] = attr.value;
});
return $('<a>', attrs).append(count.contents().clone(true, true));
});
}
$('.pagehead-actions li:last').parent().append(fork);
}());
javascript:(function(){var e,g,d,a,c,f,b;$(".pagehead-actions li").each(function(){var h=$(this).find("form").attr("action");if(h){switch(h.split("/").pop()){case"fork":e=$(this);g=true;break;case"star":d=this;break;case"unstar":d=this;break}}});if(!e){if(!d){alert('No "Star" or "Unstar" button on this page. Leading me to believe this is not a page that can be forked.');return}e=$(d).clone()}a=e.find(".minibutton");if(g){a.focus();return}c=e.find("form");c.attr("action",c.attr("action").split("/").map(function(h){return h=="star"||h=="unstar"?"fork":h}).join("/"));a.html('<span class="octicon octicon-git-branch"></span>Fork');f=$(".counter").filter(function(){var h=$(this).parent().attr("href");return h?h.split("/").some(function(i){return i==="forks"}):false});b=e.find(".social-count");b.html(f.text()||0);b.attr("href",f.parent().attr("href"));if(b[0].tagName=="SPAN"){b.addClass("js-social-count");b.replaceWith(function(){var h={};$.each(this.attributes,function(j,i){h[i.name]=i.value});return $("<a>",h).append(b.contents().clone(true,true))})}$(".pagehead-actions li:last").parent().append(e)}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment