Created
June 4, 2010 03:03
-
-
Save qufighter/424875 to your computer and use it in GitHub Desktop.
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 Upload UserScript from GitHub to UserScripts | |
// @namespace vidzbigger.com | |
// @description Allows one to easily move user.js files to their userscripts.org profile. Publish updates with ease. All you have to do is add a link to your profile in your script description or project README file on github | |
// @include http://github.com/*/* | |
// @include http://gist.github.com/* | |
// @include http://userscripts.org/scripts/edit_src/* | |
// @include http://userscripts.org/scripts/new* | |
// @match http://github.com/*/* | |
// @match http://gist.github.com/* | |
// @match http://userscripts.org/scripts/edit_src/* | |
// @match http://userscripts.org/scripts/new* | |
// @version 1.0.0 | |
// ==/UserScript== | |
// Using this script with : : ::::::::::::::::::::::::::::::::: | |
/* Selected Script Id is determined below | |
YOU MUST FIRST HAVE A USERSCRIPT.ORG PROFILE AND UPLOADED THE SCRIPT ONCE | |
Projects & Forks (github.com/user_name/project_name/) | |
Add the URL to your script on userscripts.org to the README file | |
for the branch. This is the URL format we need: | |
http://userscripts.org/scripts/show/99999 | |
Then the link to install any user.js files will show up. | |
Has the added convenience of directing users to install your script! | |
if you add the URL to your script, it will work on other pages instead | |
Gists (gist.github.com) | |
Visit the gist you want to install. The description OR | |
The body of the script should contain the URL to the script | |
http://userscripts.org/scripts/show/99999 | |
Click the install button to transfer the script to UserScripts.org | |
Branches: coming soon? | |
*/ | |
GM_addStyle('.unchanged-line{display:none;}') | |
GM_addStyle('.line-no{float:left;width:50px;text-align:right;margin-right:10px;padding-right:5px;border-right:1px solid white;}') | |
function _ge(g){ | |
return document.getElementById(g); | |
} | |
function _gcn(g,s){ | |
if( typeof(s) == 'undefined' ) s=document; | |
return s.getElementsByClassName(g); | |
} | |
function _gtn(g,s){ | |
if( typeof(s) == 'undefined' ) s=document; | |
return s.getElementsByTagName(g); | |
} | |
function confirmclick(evt){ | |
if( confirm("Are you sure you tested this?") ) return true; | |
evt.preventDefault(); | |
} | |
if(window.location.host == 'userscripts.org'){ | |
var nd = document.createElement('div'); | |
nd.setAttribute('id','full-script-0'); | |
nd.setAttribute('style','display:none;'); | |
document.body.appendChild(nd); | |
origScript = _ge('script_src').value; | |
newScript = ''; | |
function setScript(scr){ | |
if( scr.substr(0,17) == '// ==UserScript==' ){ | |
newScript = scr; | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
function setupApply(){ | |
if( newScript != origScript ){ | |
origScript=origScript.replace("\r\n","\n"); | |
newScript=newScript.replace("\r\n","\n"); | |
ol=origScript.split("\n"); | |
nl=newScript.split("\n"); | |
//finally view the new script | |
document.getElementsByName('commit')[0].addEventListener('click',confirmclick,false); | |
_ge('script_src').value=newScript; | |
des=_ge('content'); | |
if(!des || _gcn('admin_section').length > 0){ | |
des=_gcn('admin_section')[0]; | |
_ge('script_src').style.height="100px"; | |
}else{ | |
return; | |
} | |
var pr = document.createElement('pre'); | |
lmax = nl.length + ol.length; | |
var lc=0; | |
var o = 0; | |
for( i=0,lin=nl.length; i<lmax; i++ ){ | |
var ns = document.createElement('div'); | |
var st='background-color:#AAA;'; | |
var tx = nl[i]; | |
var lno = i; | |
var cn = 'unchanged-line' | |
var idd = 'lino_'+lno; | |
if( nl[i] == ol[o] ){ | |
o++; | |
}else{ | |
var mli = false; | |
for( q=i+1,li=nl.length; q<li; q++ ){ | |
if( ol[o] == nl[q]){ | |
mli=q;//then we can keep going mli until i=q | |
break; | |
} | |
} | |
if( !mli ){ | |
st='background-color:#F99;'; | |
cn = 'removed-line' | |
tx=ol[o]; | |
lno = o; | |
idd='rem-lino_'+lno; | |
o++; | |
if( 0 <= ol.length ) i--; | |
}else{// if( nl[i] != ol[o] ){ | |
st='background-color:#9F9;'; | |
cn = 'new-line' | |
tx=nl[i]; | |
lno = i; | |
idd='new-lino_'+lno; | |
} | |
} | |
if( typeof(tx) != 'undefined' ){ | |
ns.setAttribute('style',st); | |
ns.setAttribute('class',cn); | |
ns.setAttribute('id',idd); | |
var no = document.createElement('div'); | |
no.setAttribute('class','line-no'); | |
no.appendChild(document.createTextNode(lno)); | |
ns.appendChild(no); | |
ns.appendChild(document.createTextNode(tx)); | |
pr.appendChild(ns); | |
}else{ | |
//break; probably | |
} | |
lc++; | |
if( lc > lmax ){ | |
break; | |
} | |
} | |
des.appendChild(pr); | |
//showNearLines(_gcn('removed-line')); | |
showNearLines(_gcn('new-line')); | |
}else{ | |
alert('The script appears to be up to date!'); | |
} | |
} | |
function showNearLines(lines){ | |
var shownLines = []; | |
for( i in lines ){ | |
var lno=lines[i].id.split('_')[1]-0; | |
for( var i = lno-5,en=lno+5; i<en; i++ ){ | |
if( !shownLines[i] && _ge('lino_'+i) ){ | |
_ge('lino_'+i).className = 'unchanged-line-preview' | |
shownLines[i] = true; | |
} | |
} | |
} | |
} | |
var uir = window.location.href.split('source=')[1]; | |
if( uir.length < 7 ) return; | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: uir, | |
onload: function(responseDetails) | |
{ | |
if( !setScript(responseDetails.responseText) ){ | |
nd.innerHTML=responseDetails.responseText; | |
rurl = false; | |
if( _ge('raw-url') ){ | |
//github | |
rurl = _ge('raw-url').href.replace('http://userscripts.org/','http://github.com/'); | |
}else if( _gcn('actions')[1] ){ | |
//gist.github | |
rurl = _gtn('a',_gcn('actions')[1])[1].href.replace('http://userscripts.org/','http://gist.github.com/'); | |
} | |
if( rurl ){ | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: rurl, | |
onload: function(responseDetails) | |
{ | |
if( !setScript(responseDetails.responseText) ){ | |
alert('sorry error fetching script :/ make sure is the correct url'); | |
}else{ | |
setupApply(); | |
} | |
} | |
}); | |
} | |
nd.innerHTML=''; | |
}else{ | |
setupApply(); | |
} | |
} | |
}); | |
return; | |
} | |
var nldest=document.body; | |
var fname = ''; | |
var link; | |
var scriptID = 555555; | |
//github code | |
if( _ge('readme') ){ | |
nldest=_ge('browser'); | |
scriptID =_ge('readme').innerHTML.split('userscripts.org/scripts/show/')[1]; | |
scriptID = scriptID.substr(0,scriptID.indexOf('"')); | |
var ctns = _gcn('content'); | |
for( i in ctns ){ | |
if(i!='length'){ | |
link=_gtn('a',ctns[i])[0]; | |
fname = link.innerHTML; | |
if( fname.indexOf('.user.js') > 0 ){ | |
ctns[i].style.border='1px solid blue' | |
link.href = link.href + '?'; | |
} | |
}else{ | |
break; | |
} | |
} | |
link = link.href | |
}else if(_ge('raw-url') && document.title.indexOf('.user.js') > 0 ){ | |
nldest=_ge('commit'); | |
scriptID =document.body.innerHTML.split('userscripts.org/scripts/show/')[1]; | |
if(scriptID){ | |
scriptID = scriptID.substr(0,scriptID.indexOf(' ')); | |
scriptID = scriptID.substr(0,scriptID.indexOf('"')); | |
scriptID = parseInt(scriptID); | |
}else scriptID = 555555 | |
link = _ge('raw-url').href | |
link = link + '?'; | |
if( link.length > 0) fname='.usr.js'; | |
}else{ | |
//gist.github | |
nldest=_ge('repos'); | |
scriptID =document.body.innerHTML.split('userscripts.org/scripts/show/')[1]; | |
scriptID = scriptID.substr(0,scriptID.indexOf(' ')); | |
scriptID = parseInt(scriptID); | |
link = _gtn('a',_gcn('actions')[1])[1].href; | |
if( link.length > 0) fname='.usr.js'; | |
} | |
if( fname.length > 0 ){ | |
var newl=document.createElement('a'); | |
newl.setAttribute('target','_blank'); | |
newl.setAttribute('title','Selected Script Id: '+scriptID); | |
newl.setAttribute('href','http://userscripts.org/scripts/edit_src/'+scriptID+'?source='+link) | |
newl.appendChild(document.createTextNode('Click to upgrade this version at Userscripts.org')); | |
nldest.appendChild(newl); | |
newl=document.createElement('span'); | |
newl.appendChild(document.createTextNode(' | ')); | |
nldest.appendChild(newl); | |
newl=document.createElement('a'); | |
newl.setAttribute('target','_blank'); | |
newl.setAttribute('href','http://userscripts.org/scripts/new?form=true&source='+link) | |
newl.appendChild(document.createTextNode('New Script')); | |
nldest.appendChild(newl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment