Skip to content

Instantly share code, notes, and snippets.

@gordonwoodhull
Forked from jlewin/readme.md
Last active December 23, 2015 06:59
Show Gist options
  • Save gordonwoodhull/6597973 to your computer and use it in GitHub Desktop.
Save gordonwoodhull/6597973 to your computer and use it in GitHub Desktop.

Want to fork your own gists? No fork button? No problem! Install this user script by clicking refork.user.js' "raw" link down below:

Starting with Chrome 21, userscript installation is blocked on all sites other than the Chrome Web Store. The new approach requires the user to download the script file and drag & drop it into the Extensions tab.

GW - updated to URL changes of 2013 which add the username

// ==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+)
// 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 (/^\/\w+\/\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