Greasemonkey: tinyshell.user.js - mayokara note
仕様変更しまくりました。
- 標準入力をコマンド関数の引数に
- 変数名もそれっぽく変更
- STDINは文字列
- ARGVは引数の配列
\s+で区切られるので無視したいときはjoinするクオート考慮してない……考慮するように修正済
- STDOUTは返り値、自動的に文字列にされる
throw "error message"で文字列をSTDERRに出力して終了
- window.TinyShell.printlnを追加
- 遅延して文字列を出力できるように
- その他cosmetic changes
hitori-gotoコマンド
こんな感じで拡張できます、という例。
// ==UserScript==
// @name TinyShell extends
// @namespace http://mayokara.info/
// @description add some commands to TinyShell
// @include http://*
// ==/UserScript==
(function(){
var Shell = window.TinyShell;
if (!Shell) return;
Shell.addCommand("hitori-goto", function(STDIN, ARGV, $0){
var showStatus = function(response){
Shell.println($0 + ": " + response.status + " " + response.statusText);
};
if (ARGV.length > 0) {
GM_xmlhttpRequest({
method: "POST",
url: "http://hitori-goto.appspot.com/say",
data: "comment=" + encodeURIComponent(ARGV.join(" ")),
headers: { "Content-type": "application/x-www-form-urlencoded" },
onload: showStatus,
onerror: showStatus,
});
} else {
GM_xmlhttpRequest({
method: "GET",
url: "http://hitori-goto.appspot.com/rss2",
onload: function(response){
showStatus(response);
var rss = new XML(response.responseText.replace(/^<\?xml\s.*?\?>/, "")), ary = [];
for each (var title in rss.channel.item.title) {
ary.push(title);
}
Shell.println(ary.slice(0,15).join("\n"));
},
onerror: showStatus,
});
}
Shell.println($0 + ": waiting for response...");
});
})();
$ hitori-goto test hitori-goto: waiting for response... hitori-goto: 200 OK
