Created
March 20, 2009 05:15
-
-
Save youpy/82228 to your computer and use it in GitHub Desktop.
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 appjet.com: post changes to gist | |
// @namespace http://d.hatena.ne.jp/youpy/ | |
// @include http://appjet.com/app/*/ide | |
// ==/UserScript== | |
(function() { | |
if(window != window.parent) return; | |
var urlbase = location.href.replace('/ide', ''); | |
var appjetId = urlbase.match(/\d+$/)[0]; | |
var button = document.getElementById('publishbutton'); | |
window.addEventListener('load', function(e) { | |
var id; | |
if(id = Gist.id) { | |
gistLink(id); | |
} | |
}, false); | |
button.addEventListener('click', function(e) { | |
GM_xmlhttpRequest({ | |
method: "get", | |
url: urlbase + '/rawcode', | |
onload: function(res) { | |
var ext = '.js'; | |
gist(ext, 'appjet_' + appjetId + ext, res.responseText); | |
} | |
}); | |
}, false); | |
var Gist = { | |
get id() { | |
return GM_getValue(appjetId); | |
}, | |
set id(id) { | |
GM_setValue(appjetId , id); | |
} | |
}; | |
Gist.create = function(ext, filename, content) { | |
var self = this; | |
var data = { | |
'file_ext[gistfile1]': ext, | |
'file_name[gistfile1]': filename, | |
'file_contents[gistfile1]': content | |
}; | |
GM_xmlhttpRequest({ | |
method: "post", | |
url: 'http://gist.github.com/gists', | |
data: queryString(data), | |
headers: { | |
'Content-type': 'application/x-www-form-urlencoded' | |
}, | |
onload: function(res) { | |
var id = res.finalUrl.match(/\d+$/)[0]; | |
self.id = id; | |
gistLink(id); | |
} | |
}); | |
}; | |
Gist.update = function(id, ext, filename, content) { | |
var data = { | |
'_method': 'put' | |
} | |
data['file_ext[' + filename + ']'] = ext; | |
data['file_name[' + filename + ']'] = filename; | |
data['file_contents[' + filename + ']'] = content; | |
GM_xmlhttpRequest({ | |
method: "post", | |
url: 'http://gist.github.com/gists/' + id, | |
data: queryString(data), | |
headers: { | |
'Content-type': 'application/x-www-form-urlencoded' | |
} | |
}); | |
}; | |
function gist(ext, filename, content) { | |
var id; | |
if(id = Gist.id) { | |
Gist.update(id, ext, filename, content); | |
} else { | |
Gist.create(ext, filename, content); | |
} | |
} | |
function gistLink(id) { | |
var a = document.createElement('a'); | |
a.href = 'http://gist.github.com/' + id; | |
a.appendChild(document.createTextNode('gist')); | |
button.parentNode.appendChild(a); | |
} | |
function queryString(params, question){ | |
if(typeof(params)=='string') | |
return params; | |
var qeries = []; | |
for(var key in params){ | |
var value = params[key]; | |
if(value==null) continue; | |
qeries.push(encodeURIComponent(key) + '='+ encodeURIComponent(value)); | |
} | |
return (question? '?' : '') + qeries.join('&'); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment