-
-
Save syoichi/1020405 to your computer and use it in GitHub Desktop.
最近のGistの仕様変更に暫定的に対応した。Chrome 12.0.742.91で動作を確認。Operaでは動作を確認していない。
This file contains hidden or 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 gist.github for Chrome | |
// @namespace http://ss-o.net/ | |
// @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 = 'https://raw.github.com/gist/105908/diff.js'; | |
var diffscript; | |
var d = document.createRange(); | |
d.selectNodeContents(document.body); | |
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}); | |
var desc = selected.map(function() { return $(this).parent().text().replace(/\s+/g, ' '); }); | |
var req = function(url,func){ | |
var x = new XMLHttpRequest(); | |
x.onload = function(){ | |
files.appendChild(d.createContextualFragment(x.responseText).querySelector('.file')).style.position = 'fixed'; | |
func(); | |
}; | |
x.open('GET',url,false); | |
x.send(null); | |
}; | |
var dif = function(){ | |
setTimeout(function(){ | |
var pre = $(pres[1]); | |
var udiff = new UnifiedDiff( | |
document.querySelector('.file:last-child div > pre').innerText, | |
document.querySelector('.file:nth-last-child(2) div > pre').innerText, | |
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); | |
}; | |
req(link[0], function(){ | |
req(link[1] ,dif); | |
}); | |
}; | |
var button = document.createElement('button'); | |
button.name = button.id = 'diffExec'; | |
button.textContent = 'Compare'; | |
button.onclick = diffExec; | |
button.disabled = true; | |
$('#revisions')[0].appendChild(button); | |
rev.each(function() { | |
var check = document.createElement('input'); | |
check.type = 'checkbox'; | |
check.name = 'diff'; | |
check.value = this.querySelector('a.id').href; | |
check.onclick = diffSelect; | |
this.insertBefore(check, this.firstChild); | |
}); | |
}; | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.textContent = '(' + f.toString() + ')(jQuery);'; | |
document.body.appendChild(script); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment