Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyr/82794 to your computer and use it in GitHub Desktop.
Save satyr/82794 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Appjet Gist Sync
// @description Syncs Appjet publishing to Gist posting/updating
// @namespace http://d.hatena.ne.jp/murky-satyr
// @name$pace http://d.hatena.ne.jp/youpy/
// @include http://appjet.com/app/*/ide
// ==/UserScript==
if(top !== self) return;
const {URL} = document, [AppID] = /\d+/(URL);
var gistID = GM_getValue(AppID);
with(document.getElementById('publishbutton')){
textContent += ' and '+ (gistID ? 'Update Gist '+ gistID : 'Post to Gist');
addEventListener('click', sync, true);
}
function sync(e){
const AppName = document.getElementById('publishedappname').value;
var me = this;
GM_xmlhttpRequest({
method: 'get',
url: URL.replace(/[^\/]+$/, 'rawcode'),
onload: function(res){
var span = me.parentNode.insertBefore(document.createElement('span'),
me.nextSibling);
span.appendChild(new Image).src = '/img/status-ball.gif';
gist({
id: gistID,
name: AppName +'.app.js',
code: res.responseText,
load: function(res){
if(res.status === 200){
var id = gistID = (/\d+$/(res.finalUrl) || [''])[0];
GM_setValue(AppID, id);
GM_log(id);
span.innerHTML =
' '+ /<a [^>]*?class="id"[^>]*>\w+<\/a>/(res.responseText);
} else {
GM_log('Error: '+ res.status +' '+ res.statusText +
' ('+ res.finalUrl +')');
span.innerHTML = 'error'.italics();
}
}})}});
}
function gist({id, name, code, load, index}){
index || (index = 1);
GM_xmlhttpRequest({
method: 'post',
url: 'http://gist.github.com/gists/'+ (id || ''),
data: ['file_'+ key +'[gistfile'+ index +']='+ encodeURIComponent(val)
for each([key, val] in
Iterator({ext: '', name: name, contents: code}))
].concat(id && '_method=put').join('&'),
headers: {'Content-type': 'application/x-www-form-urlencoded'},
onload: load });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment