Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Forked from antichris/about.md
Last active April 13, 2022 17:52
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Noitidart/8794639 to your computer and use it in GitHub Desktop.
Save Noitidart/8794639 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)}());
@Noitidart
Copy link
Author

the original had some issues:

  1. did not work when if you the page you are trying to run it on was already starred by you
  2. if there were 0 stars but were fork count > 0, the fork count would not be a clickable link (note: if there are 0 forks, the link created will still not be clickable)

@szeitlin
Copy link

Help - I'm trying to follow your instructions in Chrome, but it's not working? My gist has not been starred or forked previously.

@Kazark
Copy link

Kazark commented Mar 1, 2015

Not working for me on Firefox 36. :( I had a user style installed for GitHub, but after I disabled it and restarted, it still didn't work. I have NoScript installed; would that be a problem? It doesn't show as blocking anything. I tried both installing the addon and pasting into the URL.

@Kazark
Copy link

Kazark commented Mar 1, 2015

Okay, got it to work by pasting it into the Scratchpad. Thanks!

@BrainCrumbz
Copy link

A typo in Firefox section: 'swwitch' -> 'switch'

@rocketraman
Copy link

Thanks!

@tyrasd
Copy link

tyrasd commented Aug 6, 2015

This doesn't work anymore, does it? 😕

@adg29
Copy link

adg29 commented Aug 19, 2015

Did not work as intended for me. Instead saw a second Star button added.

@mayankagarwal2402
Copy link

Nope. Doesn't work for me as well. (using Google Chrome v55.0)

@Alynva
Copy link

Alynva commented Aug 16, 2021

You can’t perform that action at this time.

@ackvf
Copy link

ackvf commented Apr 13, 2022

Don't know about you but jQuery is not present in my github gists page.

@ackvf
Copy link

ackvf commented Apr 13, 2022

Yeah, it seems to not be possible at all any more anyway.

community/community#14882

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