aboutsummaryrefslogtreecommitdiffstats
path: root/twitter.js
blob: 82e28d34b102915a246d8b7e4a2b9c13bde12834 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Vimperator plugin: "Update Twitter"
// Last Change: 21-Jan-2009. Jan 2008
// License: Creative Commons
// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid
//
// The script allows you to update Twitter status from Vimperator 0.6.*.
//
// Commands:
//  :twitter some thing text
//      post "some thing text" to twitter.
//  :twitter! someone
//      show someone's statuses.
//  :twitter!? someword
//      show search result of 'someword' from "http://search.twitter.com/".
//  :twitter!@
//      show replies.
//  :twitter!+ someone
//      fav someone's last status..
//  :twitter!- someone
//      un-fav someone's last status..

(function(){
    var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
    var evalFunc = window.eval;
    try {
        var sandbox = new Components.utils.Sandbox(window);
        if (Components.utils.evalInSandbox("true", sandbox) === true) {
            evalFunc = function(text) {
                return Components.utils.evalInSandbox(text, sandbox);
            }
        }
    } catch(e) { liberator.log('warning: twitter.js is working with unsafe sandbox.'); }

    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 getElementsByXPath(xpath, node){
        node = node || document;
        var nodesSnapshot = (node.ownerDocument || node).evaluate(xpath, node, null,
                XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        var data = [];
        for(var i = 0, l = nodesSnapshot.snapshotLength; i < l;
                data.push(nodesSnapshot.snapshotItem(i++)));
        return (data.length > 0) ? data : null;
    }
    function getFirstElementByXPath(xpath, node){
        node = node || document;
        var doc = node.ownerDocument;
        var result = (node.ownerDocument || node).evaluate(xpath, node, null,
                XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        return result.singleNodeValue ? result.singleNodeValue : null;
    }
    function sayTwitter(username, password, stat){
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://twitter.com/statuses/update.json", false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send("status=" + encodeURIComponent(stat) + "&source=Vimperator");
        liberator.echo("[Twitter] Your post " + '"' + stat + '" (' + stat.length + " characters) was sent. " );
    }
    function favTwitter(username, password, user){
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://twitter.com/statuses/user_timeline/" + user + ".json?count=1", false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
        xhr.open("POST", "http://twitter.com/favourings/create/" + window.eval(xhr.responseText)[0].id, false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
    }
    function unfavTwitter(username, password, user){
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://twitter.com/statuses/user_timeline/" + user + ".json?count=1", false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
        xhr.open("POST", "http://twitter.com/favourings/destroy/" + window.eval(xhr.responseText)[0].id, false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
    }
    function showTwitterReply(username, password){
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://twitter.com/statuses/replies.json", false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
        var statuses = evalFunc(xhr.responseText);

        var html = <style type="text/css"><![CDATA[
            span.twitter.entry-content a { text-decoration: none; }
            img.twitter.photo { border; 0px; width: 16px; height: 16px; vertical-align: baseline; }
        ]]></style>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
            statuses.map(function(status)
                <>
                    <img src={status.user.profile_image_url}
                         alt={status.user.screen_name}
                         title={status.user.screen_name}
                         class="twitter photo"/>
                    <strong>{status.user.name}&#x202C;</strong>
                </>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
                    sprintf(': <span class="twitter entry-content">%s&#x202C;</span>', status.text))
                        .join("<br/>");

        //liberator.log(html);
        liberator.echo(html, true);
    }
    function showFollowersStatus(username, password, target){
        var xhr = new XMLHttpRequest();
        var endPoint = target ? "http://twitter.com/statuses/user_timeline/" + target + ".json"
            : "http://twitter.com/statuses/friends_timeline.json";
        xhr.open("POST", endPoint, false, username, password);
        // for debug
        //xhr.open("POST", "http://twitter.com/statuses/user_timeline/otsune.json", false, username, password);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
        var statuses = evalFunc(xhr.responseText) || [];

        var html = <style type="text/css"><![CDATA[
            span.twitter.entry-content a { text-decoration: none; }
            img.twitter.photo { border; 0px; width: 16px; height: 16px; vertical-align: baseline; margin: 1px; }
        ]]></style>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
            statuses.map(function(status)
                <>
                    <img src={status.user.profile_image_url}
                         alt={status.user.screen_name}
                         title={status.user.screen_name}
                         class="twitter photo"/>
                    <strong>{status.user.name}&#x202C;</strong>
                    : <span class="twitter entry-content">{status.text}</span>
                </>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " "))
                        .join("<br/>");

        //liberator.log(html);
        liberator.echo(html, true);
    }
    function showTwitterSearchResult(word){
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://search.twitter.com/search.json?q=" + encodeURIComponent(word), false);
        xhr.send(null);
        var results = (evalFunc("("+xhr.responseText+")") || {'results':[]}).results;

        var html = <style type="text/css"><![CDATA[
            span.twitter.entry-content a { text-decoration: none; }
            img.twitter.photo { border; 0px; width: 16px; height: 16px; vertical-align: baseline; margin: 1px; }
        ]]></style>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " ") +
            results.map(function(result)
                <>
                    <img src={result.profile_image_url}
                         alt={result.from_user}
                         title={result.from_user}
                         class="twitter photo"/>
                    <strong>{result.from_user}&#x202C;</strong>
                    : <span class="twitter entry-content">{result.text}</span>
                </>.toSource()
                   .replace(/(?:\r?\n|\r)[ \t]*/g, " "))
                        .join("<br/>");

        //liberator.log(html);
        liberator.echo(html, true);
    }
    liberator.modules.commands.addUserCommand(["twitter"], "Change Twitter status",
        function(arg, special){
            var password;
            var username;
            try {
                var logins = passwordManager.findLogins({}, "http://twitter.com", "https://twitter.com", null);
                if (logins.length)
                    [username, password] = [logins[0].username, logins[0].password];
                else
                    throw "Twitter: account not found";
            }
            catch (ex){
                liberator.echoerr(ex);
            }

            arg = arg.string.replace(/%URL%/g, liberator.modules.buffer.URL)
                .replace(/%TITLE%/g, liberator.modules.buffer.title);

            if (special && arg.match(/^\?\s*(.*)/))
                showTwitterSearchResult(RegExp.$1)
            else
            if (special && arg.match(/^\+\s*(.*)/))
                favTwitter(username, password, RegExp.$1)
            else
            if (special && arg.match(/^\-\s*(.*)/))
                unfavTwitter(username, password, RegExp.$1)
            else
            if (special && arg.match(/^@/))
                showTwitterReply(username, password)
            else
            if (special || arg.length == 0)
                showFollowersStatus(username, password, arg)
            else
                sayTwitter(username, password, arg);
        },{
            bang: true
        }
    );
})();
// vim:sw=4 ts=4 et:
="p">().mTabContainer.addEventListener('TabClose',updateGmContainerList,false); getBrowser().mTabBox.addEventListener('TabSelect',dispatchGMTabSelect,false); config.autocommands.push(['GMInjectedScript','Triggered when UserScript is injected']); config.autocommands.push(['GMActiveScript','Triggered when location is changed and injected UserScripts are exist']); config.dialogs.push(['userscriptmanager','Greasemonkey Manager',function(){GM_openUserScriptManager();}]); // }}} return manager; })(); //}}} // --------------------------- // User Command // --------------------------- commands.addUserCommand(['gmli[st]','lsgm'],'list Greasemonkey scripts', //{{{ function(args){ var xml = <></>; var scripts = GM_getConfig().scripts; var reg; if (args.bang || args.string == 'full'){ reg = new RegExp(); } else if (args.string){ reg = new RegExp(args.string,'i'); } if (reg){ for each (let s in scripts){ if (reg.test(s.name) || reg.test(s._filename)){ xml += scriptToString(s); } } } else { let table = <table/>; let tr; for each (let script in scripts){ tr = <tr/>; if (script.enabled){ tr.* += <td style="font-weight:bold;">{script.name}</td>; } else { tr.* += <td>{script.name}</td>; } tr.* += <td>({script._filename})</td>; table.* += tr; } xml += table; } liberator.echo(xml,true); function scriptToString(script){ var table = <table> <caption class="hl-Title" style="text-align:left">{script.name}</caption> </table>; [['FileName','_filename'],['NameSpace','namespace'],['Description','description'], ['Includes','includes'],['Excludes','excludes'],['Enabled','enabled']].forEach(function(prop){ let tr = <tr> <th style="font-weight:bold;text-align:left;vertical-align:top">{prop[0]}</th> </tr>; let contents = script[prop[1]]; if (typeof contents == 'string' || typeof contents == 'boolean'){ tr.* += <td>{contents}</td>; } else { let td = <td/>; for (let i=0,l=contents.length; i<l; i++){ td.* += contents[i]; if (contents[i+1]) td.* += <br/>; } tr.* += td; } table.* += tr; }); return table; } },{ bang:true } ); //}}} commands.addUserCommand(['gmlo[ad]'],'load Greasemonkey scripts', //{{{ function(args){ if (!args.string){ liberator.echoerr('Usage: :gmlo[ad][!] {name|filename}'); return; } var scripts = GM_getConfig().scripts; var script; for (let i=0,l=scripts.length; i<l; i++){ if (scripts[i]._filename == args.string || scripts[i].name == args.string){ script = scripts[i]; break; } } if (!script){ liberator.echoerr('no such a user script'); return; } else if (liberator.plugins.gmperator.currentContainer.hasScript(script._filename) && !args.bang){ liberator.echoerr(script._filename + ' is already loaded!'); return; } else { liberator.echo('loading: ' +script._filename); } var href,unsafewin; try { href = buffer.URL; unsafewin = window.content.document.defaultView.wrappedJSObject; GM_BrowserUI.gmSvc.wrappedJSObject.injectScripts([script],href,unsafewin,window); } catch (e){ liberator.log(e); liberator.echoerr(e); } /* // do you have idea how to dispatch load event to only the script ? window.setTimeout(function(){ var loadEvent = document.createEvent('Event'); loadEvent.initEvent('load',true,true,window.content.document,1); window.content.document.dispatchEvent(loadEvent); },100); */ },{ completer: function(context) scriptsCompleter(context.filter,true) } ); //}}} commands.addUserCommand(['gmset'],'change settings for Greasemonkey scripts', //{{{ function(args){ if (args.length == 0){ if (args.bang) GM_setEnabled(!GM_getEnabled()); // toggle enable/disable Greasemonkey return; } var filename = args[0]; var config = GM_getConfig(); var script; for (let i=0,l=config.scripts.length; i<l; i++){ if (config.scripts[i]._filename == filename){ script = config.scripts[i]; break; } } if (!script) return; if (args.bang){ // toggle enable/disable the script if {filename} is exist script.enabled = !script.enabled; } if (args['-name']) script.name = args['-name']; if (args['-include']) script.include = args['-include']; if (args['-exclude']) script.exclude = args['-exclude']; config._save(); },{ completer: function(context) scriptsCompleter(context.filter,false), options: [ [['-name','-n'], commands.OPTION_STRING], [['-include','-i'], commands.OPTION_LIST], [['-exclude','-e'], commands.OPTION_LIST] ], bang:true } ); //}}} commands.addUserCommand(['gmcommand','gmcmd'],'run Greasemonkey Command', //{{{ function(args){ var commander = GM_BrowserUI.getCommander(content); var commandName = args[0]; for (let i=0,l=commander.menuItems.length; i<l; i++){ let menuItem = commander.menuItems[i]; if (menuItem.getAttribute('label') == commandName){ menuItem._commandFunc(); return; } } liberator.echoerr(commandName + ' is not defined userscript command.'); }, { completer: function(context){ var items = GM_BrowserUI.getCommander(content).menuItems; var completions = []; var exp = new RegExp(context.filter,'i'); context.title = ["UserScript's Commands"]; context.completions = [[items[i].getAttribute('label'),'-'] for (i in items)].filter(function(item){ return this.test(item[0]); },exp); }, argCount: "1" } ); //}}} // --------------------------- // Utils // --------------------------- /** Grasemonkey sandbox container {{{ * @param {String} uri * @param {Sandbox} sandbox * @param {Array} scripts */ function GmContainer(uri,sandbox){ this.uri = uri; this.sandbox = sandbox; this.scripts = []; } GmContainer.prototype = { addScript : function(script){ if (!this.hasScript(script)){ return this.scripts.push(script); } return false; }, hasScript : function(script){ var filename; switch (typeof script){ case 'object': filename = script._filename; break; case 'string': filename = script; break; default: return null; } return this.scripts.some(function(s) s._filename == filename); } }; // }}} function scriptsCompleter(filter,flag){ //{{{ var candidates = []; var scripts = GM_getConfig().scripts; var isAll = false; if (!filter) isAll=true; if (flag){ for each (let s in scripts){ if (isAll || s.name.toLowerCase().indexOf(filter) == 0 || s._filename.indexOf(filter) == 0) { candidates.push([s.name,s.description]); candidates.push([s._filename,s.description]); } } } else { for each (let s in scripts){ if (isAll || s._filename.indexOf(filter) == 0) { candidates.push([s._filename,s.description]); } } } return [0,candidates]; } //}}} })(); // vim: fdm=marker sw=4 ts=4 et: