// Vimperator plugin: "Update Wassr" // Last Change: 09-May-2008. Jan 2008 // License: Creative Commons // Maintainer: mattn - http://mattn.kaoriya.net/ // Based On: twitter.js by Trapezoid // // The script allows you to update Wassr status from Vimperator 0.6.*. (function(){ var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); function sayWassr(username, password, stat){ var xhr = new XMLHttpRequest(); xhr.open("POST", "http://api.wassr.jp/statuses/update.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("status=" + encodeURIComponent(stat)); } function sprintf(format){ var i = 1, re = /%s/, result = "" + format; while (re.test(result) && i < arguments.length) result = result.replace(re, arguments[i++]); return result; } function showFollowersStatus(username, password){ var xhr = new XMLHttpRequest(); xhr.open("GET", "http://api.wassr.jp/statuses/friends_timeline.json", false, username, password); // for debug //xhr.open("GET", "http://api.wassr.jp/statuses/user_timeline/otsune.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(null); var statuses = window.eval(xhr.responseText); var html = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + statuses.map(function(status) <> {status.user.screen_name} {status.user_login_id}‬ .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + sprintf(': %s‬', status.text)) .join("
"); liberator.echo(html, true); } function todoAction(username, password, arg){ var xhr = new XMLHttpRequest(); if (arg.match(/\+ (.*)/)) { xhr.open("POST", "http://api.wassr.jp/todo/add.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("body=" + encodeURIComponent(RegExp.$1)); } else if (arg.match(/- (.*)/)) { xhr.open("POST", "http://api.wassr.jp/todo/delete.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("todo_rid=" + encodeURIComponent(RegExp.$1)); } else if (arg.match(/\* (.*)/)) { xhr.open("POST", "http://api.wassr.jp/todo/start.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("todo_rid=" + encodeURIComponent(RegExp.$1)); } else if (arg.match(/\/ (.*)/)) { xhr.open("POST", "http://api.wassr.jp/todo/stop.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("todo_rid=" + encodeURIComponent(RegExp.$1)); } else if (arg.match(/! (.*)/)) { xhr.open("POST", "http://api.wassr.jp/todo/done.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("todo_rid=" + encodeURIComponent(RegExp.$1)); } xhr.open("GET", "http://api.wassr.jp/todo/list.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(null); var todos = window.eval(xhr.responseText); var html = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + todos.map(function(todo) <> todo {todo.todo_rid} .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + sprintf(': %s', todo.body)) .join("
"); liberator.echo(html, true); } function footmarkAction(username, password){ var xhr = new XMLHttpRequest(); xhr.open("GET", "http://api.wassr.jp/footmark/recent.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(null); var footmarks = window.eval(xhr.responseText); var html = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + footmarks.map(function(footmark) <> {footmark.nick} {footmark.login_id}‬ .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ")).join("
"); liberator.echo(html, true); } function footmarkAction(username, password){ var xhr = new XMLHttpRequest(); xhr.open("GET", "http://api.wassr.jp/footmark/recent.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(null); var footmarks = window.eval(xhr.responseText); var html = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + footmarks.map(function(footmark) <> {footmark.nick} {footmark.login_id}‬ .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ")).join("
"); liberator.echo(html, true); } liberator.commands.addUserCommand(["wassr"], "Change wassr status", function(arg, special){ var password; var username; try { var logins = passwordManager.findLogins({}, "http://wassr.jp", "http://wassr.jp", null); if (logins.length) [username, password] = [logins[0].username, logins[0].password]; else throw "Wassr: account not found"; } catch (ex){ liberator.echoerr(ex); } if (special){ arg = arg.replace(/%URL%/g, liberator.buffer.URL) .replace(/%TITLE%/g, liberator.buffer.title); } if (!arg || arg.length == 0) showFollowersStatus(username, password); else if (arg.match(/^-todo(.*)/)) todoAction(username, password, RegExp.$1); else if (arg.match(/^-footmark$/)) footmarkAction(username, password); else sayWassr(username, password, arg); }, { args: [ [['-todo'], commands.OPTION_STRING], [['-footmark'], null] ], completer: function(filter) { candidates = []; if (filter.match(/-todo$/)) { candidates = [ ['-todo+', 'add todo'], ['-todo-', 'delete todo'], ['-todo*', 'start todo'], ['-todo/', 'stop todo'], ['-todo!', 'done todo'], ]; } else if (filter.match(/-todo[^\+].*/)) { var password; var username; try { var logins = passwordManager.findLogins({}, "http://wassr.jp", "http://wassr.jp", null); if (logins.length) [username, password] = [logins[0].username, logins[0].password]; else throw "Wassr: account not found"; var xhr = new XMLHttpRequest(); xhr.open("GET", "http://api.wassr.jp/todo/list.json", false, username, password); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(null); var todos = window.eval(xhr.responseText); for(let i in todos) candidates.push([filter + ' ' + todos[i].todo_rid, todos[i].body]); } catch (ex){ liberator.echoerr(ex); } } else { candidates = [ ['-todo', 'todo'], ['-footmark', 'footmark'], ]; } return [0,candidates]; } }); })(); // vim:sw=4 ts=4 et: