aboutsummaryrefslogtreecommitdiffstats
path: root/wassr.js
blob: c76aaf73a3d2107ad786002ad57d237a5fe7d53e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Vimperator plugin: "Update Wassr"
// Last Change: 08-May-2008. Jan 2008
// License: Creative Commons
// Maintainer: mattn <mattn.jp@gmail.com> - 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 = <style type="text/css"><![CDATA[
            span.wassr.entry-content a { text-decoration: none; }
            img.wassr.photo { border; 0px; width: 16px; height: 16px; vertical-align: baseline; }
        ]]></style>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
            statuses.map(function(status)
                <>
                    <img src={"http://wassr.jp/user/" + status.user_login_id + "/profile_img.png.32"}
                         alt={status.user.screen_name}
                         title={status.user.screen_name}
                         class="wassr photo"/>
                    <strong>{status.user_login_id}&#x202C;</strong>
                </>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
                    sprintf(': <span class="wassr entry-content">%s&#x202C;</span>', status.text))
                        .join("<br/>");

        //liberator.log(html);
        liberator.echo(html, true);
    }
    function todoAction(username, password, arg){
        var xhr = new XMLHttpRequest();
        liberator.log(arg)
        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 = <style type="text/css"><![CDATA[
            span.wassr.entry-content a { text-decoration: none; }
            img.wassr.icon { border; 0px; width: 16px; height: 16px; vertical-align: baseline; }
        ]]></style>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
            todos.map(function(todo)
                <>
                    <img src="http://wassr.jp/img/icn-balloon.gif"
                         alt="todo"
                         title="todo"
                         class="wassr icon"/>
                    <strong>{todo.todo_rid}</strong>
                </>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
                    sprintf(': <span class="wassr entry-content">%s</span>', todo.body))
                        .join("<br/>");

        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
                sayWassr(username, password, arg);
        },
    {
        completer: function(filter) {
            liberator.log(filter)
            candidates = [];
            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]);
                    liberator.log(candidates)
                }
                catch (ex){
                    liberator.echoerr(ex);
                }
            }
            return [0,candidates];
        }
    });
})();
// vim:sw=4 ts=4 et: