Skip to content

Instantly share code, notes, and snippets.

@gregmac
Forked from jkp/readme.md
Last active December 19, 2015 06:39
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 gregmac/5913405 to your computer and use it in GitHub Desktop.
Save gregmac/5913405 to your computer and use it in GitHub Desktop.

Want to fork your own gists? No fork button? No problem!

Install this Gist by saving to disk (click "Raw"), and then locate it on your machine, and drag it to your Extensions tab in Chrome.

// ==UserScript==
// @name (Re)fork any gist, including your own
// @namespace https://github.com/johan
// @description Adds a "fork" button to gists missing one at gist.github.com, so you can create multiple forks
// @match https://gist.github.com/*
// @include https://gist.github.com/*
// ==/UserScript==
var resourceUri;
// Only create the button on initial load and reuse on pjax reloads
var reforkListItem = (function () {
var li = document.createElement('li')
, a = document.createElement('a')
, sp = document.createElement('span');
a.className = 'minibutton fork-button'; // Apply button look and feel
a.title = 'Create another fork of this gist'; // Set hover text
// When clicked, append an http form for the uri/fork post and submit it
a.addEventListener('click', function () {
var f = document.body.appendChild(document.createElement('form'));
f.method = 'POST';
f.action = resourceUri + '/fork';
f.appendChild(document.querySelector('input[name=authenticity_token]'));
f.submit();
return false;
});
sp.className = 'mini-icon mini-icon-fork';; // Apply the fork icon
// Create the control tree
li.appendChild(a);
a.appendChild(sp);
a.appendChild(document.createTextNode('(Re)Fork'));
return li;
})();
// If missing and appropriate, add in the fork button
function injectForkButton() {
var pageActions = document.querySelector('.pagehead-actions');
var resourceLink = document.querySelector('input[name="link-field"]');
// Only attempt to inject when on an actual gist url (https://gist.github.com/\d+) or private gist url (https://gist.github.com/username/\d+)
// and only when a '.pagehead-actions' element exists
// and only when the 'input[name="link-field"]' element exists
// and only when an existing '.fork-button' element is missing
if (/^\/([^\/]+\/)?\d+/.test(location.pathname) &&*/ pageActions && resourceLink && !document.querySelector('.fork-button')) {
resourceUri = resourceLink.getAttribute('value');
pageActions.appendChild(reforkListItem);
// Leave logging in for a while to confirm expected behavior
console.log('(Re)fork button injected');
}
}
// Initial page load injection
injectForkButton();
// Dynamic injection after pjax load and subsequent dom update of '#js-pjax-container'
//
// Observe 'childList' changes to '#js-pjax-container' and rerun injectForkButton
var target = document.querySelector('#js-pjax-container');
var JsMutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new JsMutationObserver(function (mutations) {
console.log('pjax-container mutation - checking for fork button');
injectForkButton();
});
observer.observe(target, { childList: true });
// TODO: Is it necessary to call observer.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment