Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
UserScript to show JSFiddle revisions for each fiddle
// ==UserScript==
// @name Show JSFiddle revisions for each fiddle
// @namespace webapps.se
// @author brasofilo
// @include http://jsfiddle.net/user/*
// @description Add revision history
// @require //cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==
/* Stores the API list result */
jsf_titles = [];
/**
* Sometimes the Revision URL is anonymous, without /username/
* this function tests the URL before navigating
* if it is a 404, we strip the username from the URL
*
* http://stackoverflow.com/a/22097991/1287812
*/
testRevisionURL = function( url )
{
var request;
if(window.XMLHttpRequest)
request = new XMLHttpRequest();
else
request = new ActiveXObject("Microsoft.XMLHTTP");
request.open('GET', url, false);
request.send();
if (request.status === 404) {
window.location = url.replace( dashboardPage() + '/', '' );
} else {
window.location = url;
}
}
/**
* Build the Revision links
*/
appendRevisionLinks = function( $div, $entry ) {
if( parseInt( $entry.version ) > 0 ) {
var start = 1;
var total = parseInt( $entry.version );
if( total > 50 ) start = total - 50;
$div.parent().next('p.info').append( ' - Revisions: | ' );
for (i = start; i <= total; i++) {
var click_url = ' onClick="testRevisionURL(\'' + $entry.url + i + '\')"';
$div.parent().next('p.info').append( '<a href="javascript:void(0)" ' + click_url + '><b style="font-size:.8em">' + i + '</b></a> | ' );
}
} else {
$div.parent().next('p.info').append( ' - No revisions ' );
}
}
/**
* Callback for jQuery loaded
* Search all fiddle titles on the page and compare with the API list
*/
function jqueryCallback() {
jQ('.fiddleList .item h3 a:first-child').each(function() {
var $this = jQ(this);
var $url = $this.attr('href');
jsf_titles.forEach(function(entry) {
if( entry.url.indexOf($this.attr('href')) > -1 ) {
appendRevisionLinks( $this, entry );
jsf_titles.remove(entry);
}
});
});
}
/**
* Load jQuery and do callback when jQuery has finished loading
* Note, jQ replaces $ to avoid conflicts.
*
* http://stackoverflow.com/a/3550261/1287812
*/
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js");
script.addEventListener('load', function () {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
/**
* JSFiddle API callback function
* Builds jsf_titles custom array with JSONP result
*/
fiddleObjects = function( json ){
json.list.forEach(function(entry) {
jsf_titles.push({'title': entry.title, 'version': entry.latest_version, 'url': entry.url });
});
addJQuery( jqueryCallback );
};
/**
* Call API
*
* http://stackoverflow.com/a/2887218/1287812
*/
callApi = function( user, page )
{
var elm = document.createElement("script");
elm.setAttribute("type", "text/javascript");
var start = (page - 1) * 10;
elm.src = "http://jsfiddle.net/api/user/" + user + "/demo/list.json?callback=fiddleObjects&order=desc&start=" + start + "&limit=10";
document.body.appendChild(elm);
}
/**
* Remove element from array
* http://www.simonewebdesign.it/how-to-remove-element-from-array/
*/
Array.prototype.remove = function(value) {
var idx = this.indexOf(value);
if (idx != -1) {
return this.splice(idx, 1); // The second parameter is the number of elements to remove.
}
return false;
}
/**
* Which page of the Dashboard we're in
* not used yet, but useful if start and limit are to be used on the API request
*/
actualPage = function(){
var pathname = window.location.pathname.split('/');
if( pathname[pathname.length-2] == "dashboard" )
return 1;
else {
var pagenum = parseInt( pathname[pathname.length-2] );
return isNaN( pagenum ) ? 1 : pagenum;
}
}
/**
* Public page or dashboard
*/
dashboardPage = function(){
var pathname = window.location.pathname.split('/');
if( pathname[2] == "dashboard" )
return false;
else
return pathname[2];
}
/**
* Start up
*/
if( dashboardPage() ) {
callApi( dashboardPage(), actualPage() );
// alert('user name: ' + dashboardPage() + "\n page:" + actualPage() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment