-
-
Save fearphage/227881 to your computer and use it in GitHub Desktop.
Diff for gist.github for Opera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Diff for gists for Opera | |
// @include http://gist.github.com/* | |
// @include https://gist.github.com/* | |
// @ujs:modified 2010-09-10 | |
// @ujs:download http://gist.github.com/raw/227881/53ad5dff46b825ea3facd77532b57da2ab445425/diff_for_gist.user.js | |
// ==/UserScript== | |
(function(window, $, rev) { | |
if (!$ || ((rev = $('#revisions li')).length < 2)) return; | |
var diffSelect = function(e) { | |
var me = e.target, c = $('#revisions li input:checked'), length = c.length; | |
if (length > 2) | |
c.each(function() { if (this != me) this.checked = false; }); | |
$('#diffExec').attr('disabled', (length != 2)); | |
}, | |
diffcontent, diffscript, $files = $('#files'), diffsrc = 'http://gist.github.com/105908.txt', count, maxCount, last, | |
diffExec = function() { | |
if (!window.Diff && !diffscript) { | |
return (diffscript = $.getScript(diffsrc, diffExec)); | |
} | |
count = 0; | |
var selected = $('#revisions').find('input:checkbox:checked'), | |
link = selected.map(function() {return this.value.replace(/(https?:\/\/[^\/]+\/)/, '$1raw/')}), | |
desc = selected.map(function() { return $(this).parent().text().replace(/\s+/g, ' '); }), | |
req = function(url,func){ | |
var x = new XMLHttpRequest(); | |
x.onload = func; | |
if (url.indexOf(location.protocol) == -1) { | |
url = location.protocol + url.split(':').slice(1).join(':'); | |
} | |
x.open('GET',url,false); | |
x.send(null); | |
return x; | |
}, | |
clog1,clog2,flog1,flog2,raw1,raw2, | |
log = function(){ | |
setTimeout(function(){ | |
var r1 = clog1.responseText.split(/\n/)[0].split(/\s/)[1]; | |
var r2 = clog2.responseText.split(/\n/)[0].split(/\s/)[1]; | |
flog1 = req(link[0].replace(/[^\/]*$/, r1) + '/meta', function(){ | |
flog2 = req(link[1].replace(/[^\/]*$/, r2) + '/meta',fil); | |
}); | |
},100); | |
}, | |
fil = function(){ | |
setTimeout(function() { | |
flog1 = flog1.responseText.split(/\n/); | |
flog2 = flog2.responseText.split(/\n/); | |
maxCount = Math.min(flog1.length, flog2.length) | |
setTimeout(fil2, 100); | |
}, 100); | |
}, | |
fil2 = function() { | |
var $diff = $(diffcontent = $files.find('div.file')[count *2].cloneNode(true)); | |
if (!last) { | |
$diff.prependTo($files).hide(); | |
} | |
else { | |
$diff.insertAfter(last).hide(); | |
} | |
last = diffcontent; | |
pres = diffcontent.getElementsByTagName('pre'); | |
pres[0].parentNode.replaceChild(pres[0].cloneNode(false),pres[0]); | |
pres[1].parentNode.replaceChild(pres[1].cloneNode(false),pres[1]); | |
var r1 = flog1[count].split(/\s/); | |
var r2 = flog2[count].split(/\s/); | |
raw1 = req(link[0].replace(/[^\/]*$/, r1[2]) + '/' + r1[3], function(){ | |
raw2 = req(link[1].replace(/[^\/]*$/, r2[2]) + '/' + r2[3], dif); | |
}); | |
}, | |
dif = function(){ | |
setTimeout(function(){ | |
var pre = $(pres[1]); | |
var udiff = new UnifiedDiff(raw2.responseText, raw1.responseText, 4).toString(); | |
udiff = '--- ' + desc[1] + '\n' + '+++ ' + desc[0] + '\n' + pre.text(udiff).html(); | |
var len = udiff.split(/\n/).length; | |
if(len < 5000) { | |
udiff = udiff.replace(/^(\+.*)$/mg, '<span class="gi">$1</span>') | |
.replace(/^(\-.*)$/mg, '<span class="gd">$1</span>') | |
.replace(/^(\@.*)$/mg, '<span class="gu">$1</span>') | |
.replace(/^(.*)\n/mg, '<div class="line">$1</div>'); | |
pres[0].innerHTML = Array(len-1).join(',').split(',').map(function(a,i){return '<span>'+(1+i)+'</span>'}).join('\n'); | |
} | |
pre.html(udiff); | |
$(diffcontent).slideDown('normal'); | |
if (count++ < maxCount && ((count*2) < $files.find('div.file').length)) { | |
fil2(); | |
} | |
},100); | |
}; | |
clog1 = req(link[0] + '/meta',function(){ | |
clog2 = req(link[1] + '/meta',log); | |
}); | |
}; | |
$('<input type="button" id="diffExec" value="Compare" disabled="disabled"/>') | |
.appendTo('#revisions') | |
.click(diffExec); | |
rev.each(function() { | |
$('<input type="checkbox" name="diff" />') | |
.val($(this).find('.id').attr('href')) | |
.prependTo(this) | |
.click(diffSelect); | |
}); | |
})(this, this.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment