Created
September 18, 2009 08:35
-
-
Save youpy/188945 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 save wonderfl changes to gist | |
| // @namespace http://d.hatena.ne.jp/youpy/ | |
| // @include http://wonderfl.net/code/*/edit | |
| // ==/UserScript== | |
| (function() { | |
| if(window != window.parent) return; | |
| var wonderflId = location.href.match(/([^\/]+)\/edit$/)[1]; | |
| var button = document.getElementById('btn_save'); | |
| var textArea = document.getElementById('as3'); | |
| window.addEventListener('load', function(e) { | |
| var id; | |
| if(id = Gist.id) { | |
| gistLink(id); | |
| } | |
| }, false); | |
| button.addEventListener('click', function(e) { | |
| var ext = '.as'; | |
| gist(ext, 'wonderfl_' + wonderflId + ext, textArea.value); | |
| }, false); | |
| var Gist = { | |
| get id() { | |
| return GM_getValue(wonderflId); | |
| }, | |
| set id(id) { | |
| GM_setValue(wonderflId , 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')); | |
| a.style.fontSize = '150%'; | |
| a.style.verticalAlign = 'top'; | |
| 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