os0x (owner)

Fork Of

Forks

Revisions

gist: 110472 Download_button fork
public
Public Clone URL: git://gist.github.com/110472.git
Embed All Files: show embed
diff_for_gist.user.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ==UserScript==
// @name Diff for gist.github for Opera & Chrome
// @namespace http://ss-o.net/
// @include http://gist.github.com/*
// @include https://gist.github.com/*
// ==/UserScript==
 
(function(){
var f = function($) {
var rev = $('#revisions li');
if(!rev.length || rev.length == 1) return;
var diffSelect = function(e) {
var me = e.target;
var c = $('#revisions li input:checked');
if(c.length > 2)
c.each(function(i) { if(c[i] != me) c[i].checked = false; });
$('#diffExec').attr('disabled', (c.length != 2));
};
var diffcontent;
var files = document.getElementById('files');
var diffsrc = 'http://gist.github.com/105908.txt';
var diffscript;
var diffExec = function() {
if (!window.Diff && !diffscript) {
var script = diffscript = document.createElement('script');
script.type = 'text/javascript';
script.onload = diffExec;
script.src = diffsrc;
document.body.appendChild(script);
return;
}
if(!diffcontent) {
diffcontent = $('#files div.file')[0].cloneNode(true);
}
var pres = diffcontent.getElementsByTagName('pre');
pres[0].parentNode.replaceChild(pres[0].cloneNode(false),pres[0]);
pres[1].parentNode.replaceChild(pres[1].cloneNode(false),pres[1]);
files.insertBefore(diffcontent,files.firstChild);
$(diffcontent).hide();
var selected = $('#revisions').find('input:checkbox:checked');
var link = selected.map(function() {return this.value.replace(/(https?:\/\/[^\/]+\/)/, '$1raw/')});
var desc = selected.map(function() { return $(this).parent().text().replace(/\s+/g, ' '); });
var req = function(url,func){
var x = new XMLHttpRequest();
x.onload = func;
x.open('GET',url,false);
x.send(null);
return x;
};
var clog1,clog2,flog1,flog2,raw1,raw2;
var 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), function(){
flog2 = req(link[1].replace(/[^\/]*$/, r2),fil);
});
},100);
};
var fil = function(){
setTimeout(function(){
var r1 = flog1.responseText.split(/\n/)[0].split(/\s/)[2];
var r2 = flog2.responseText.split(/\n/)[0].split(/\s/)[2];
raw1 = req(link[0].replace(/[^\/]*$/, r1), function(){
raw2 = req(link[1].replace(/[^\/]*$/, r2),dif);
});
},100);
};
var 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="gd">$1</span>')
.replace(/^(\-.*)$/mg, '<span class="gi">$1</span>')
.replace(/^(\@.*)$/mg, '<span class="gu">$1</span>')
.replace(/^(.*)\n/mg, '<div class="line">$1</div>');
pres[0].innerHTML = new Array(len-1).join(',').split(',').map(function(a,i){return '<span>'+(1+i)+'</span>'}).join('\n');
}
pre.html(udiff);
$(diffcontent).slideDown('normal');
},100);
};
clog1 = req(link[0],function(){
clog2 = req(link[1],log);
});
};
$('#revisions').append(
$('<input type="button" />')
.attr('name', 'diffExec')
.attr('id', 'diffExec')
.val('Compare')
.bind('click', diffExec)
.attr('disabled', 'disabled')
);
rev.each(function() {
var r = $(this);
r.prepend(
$('<input type="checkbox" />')
.attr('name', 'diff')
.val(r.find('.id').attr('href'))
.bind('click', diffSelect)
);
});
};
var script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = '(' + f.toString() + ')(jQuery);';
document.body.appendChild(script);
})();