// Vimperator plugin: "Update Twitter" // Last Change: 11-Nov-2008. Jan 2008 // License: Creative Commons // Maintainer: Trapezoid - 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://twitter.1x1.jp". // :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 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 = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + statuses.map(function(status) <> {status.user.screen_name} {status.user.name}‬ .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + sprintf(': %s‬', status.text)) .join("
"); //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 = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ") + statuses.map(function(status) <> {status.user.screen_name} {status.user.name}‬ : {status.text} .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " ")) .join("
"); //liberator.log(html); liberator.echo(html, true); } function showTwitterSearchResult(word){ var xhr = new XMLHttpRequest(); xhr.open("GET", "http://twitter.1x1.jp/rss/search/?keyword=" + encodeURIComponent(word) + "&text=1", false); xhr.send(null); var items = xhr.responseXML.getElementsByTagName('item'); var html = .toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " "); for (var n = 0; n < items.length; n++) html += <> {items[n].getElementsByTagName('title')[0].textContent}‬ : {items[n].getElementsByTagName('description')[0].textContent}‬
.toSource() .replace(/(?:\r?\n|\r)[ \t]*/g, " "); 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: 139' href='#n139'>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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
var PLUGIN_INFO =
<VimperatorPlugin>
<name>{NAME}</name>
<description>Access to Hatena Sevices quickly.</description>
<description lang="ja">はてなのサーヴィスに簡単にアクセス出来ます</description>
<minVersion>2.1a1pre</minVersion>
<maxVersion>2.1a1pre</maxVersion>
<updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/access_hatena.js</updateURL>
<author mail="masa138@gmail.com" homepage="http://www.hatena.ne.jp/masa138/">Masayuki KIMURA and id:hitode909</author>
<version>0.63</version>
<detail><![CDATA[

== Commands ==
:accesshatena
    Access to http://www.hatena.ne.jp/

:accesshatena {host}
    Access to http://{host}.hatena.ne.jp/

:accesshatena {host} {user_id}
    Access to http://{host}.hatena.ne.jp/{user_id}/

:accesshatena {group_name}.g {user_id}
    Access to http://{group_name}.g.hatena.ne.jp/{user_id}/


== Global variables ==
maxRecentHostsSize
    直近何件の履歴を検索してホスト補完をするか

collectLogSpan
    どのくらいの期間の履歴を検索するか

accessHatenaUseWedata
    Wedata から拒否 ID リストを取ってくるか

accessHatenaIgnoreIds
    Wedata の拒否リストに含まれないけれど拒否したい ID の記入用


== .vimperatorrc ==
コマンドが長いので以下の様に短い物にマッピングすると便利です
>||
map ; :accesshatena 
||<
# 最後にスペースを入れておくと直ぐにホストの入力から始められます

]]></detail>
</VimperatorPlugin>;
(function(){
    var useWedata;
    var ignoreIds;
    var ids;
    var recentHosts;
    var maxRecentHostsSize;
    var historyCompletions;
    var collectLogSpan;
    var pageFor;
    var title;
    var isFirst;
    var isIncreased;
    var isUpdated;
    var lastLocation;

    function Title() {
        this.initialize.apply(this, arguments);
    }
    Title.prototype = {
        initialize: function() {
            this.title = [];
            this.n     = 0;
        },
        key: function(host, id) {
            return [host, id.replace('/', '')].join(':');
        },
        set: function(host, id, title) {
            var key = this.key(host, id);
            if (this.title[key] == null) {
                this.title[key] = title;
                this.n++;
            }
        },
        get: function(host, id, title) {
            if (!this.title[this.key(host, id)]) return host + '.hatena.ne.jp/' + id;
            return this.title[this.key(host, id)];
        }
    };

    function init() {
        ids                = [];
        recentHosts        = [];
        historyCompletions = [];
        pageFor            = [];
        title              = new Title();
        isFirst            = true;
        isIncreased        = false;
        isUpdated          = false;
        lastLocation       = window.content.location.href.replace(/^https?:\/\//, '');

        maxRecentHostsSize = liberator.globalVariables.maxRecentHostsSize || 10;
        collectLogSpan     = liberator.globalVariables.collectLogSpan     || 24 * 7 * 4 * 60 * 60 * 1000000; // 4 weeks
        useWedata          = liberator.globalVariables.accessHatenaUseWedata;
        ignoreIds          = liberator.globalVariables.accessHatenaIgnoreIds;
        useWedata          = (useWedata != null) ? useWedata : true;
        ignoreIds          = (ignoreIds != null) ? ignoreIds : ['login'];

        if (useWedata) {
            loadWedata();
        }
    }
    init();

    function prepareSearch() {
        var historyService       = Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsINavHistoryService);
        var options              = historyService.getNewQueryOptions();
        var query                = historyService.getNewQuery();
        query.beginTimeReference = query.TIME_RELATIVE_NOW;
        query.beginTime          = -1 * collectLogSpan;
        query.endTimeReference   = query.TIME_RELATIVE_NOW;
        query.endTime            = 0;
        query.domain             = "hatena.ne.jp";

        return historyService.executeQuery(query, options).root;
    }

    function collectLog() {
        var root             = prepareSearch();
        ids                  = [];
        recentHosts          = [];
        historyCompletions   = [];
        historyCompletions.h = [];

        root.containerOpen = true;
        for (var i = 0, length = root.childCount; i < length; i++) {
            var page = root.getChild(i);
            page.uri.match('^https?://([a-zA-Z0-9.]+)\\.hatena\\.ne\\.jp/([a-zA-Z][a-zA-Z0-9_-]{1,30}[a-zA-Z0-9](?:\\+[a-zA-Z0-9_-]{1,30})?/?)?');
            var host = RegExp.$1;
            var id   = RegExp.$2;
            var _recent_hosts_length = recentHosts.length;
            if (host != '') {
                if (!page.uri) continue;

                if (!pageFor[host]) {
                    pageFor[host] = page;
                    isIncreased = true;
                } else if (pageFor[host].uri.length > page.uri.length) { // より短いアドレスのタイトルが妥当
                    pageFor[host] = page;
                    isUpdated = true;
                }

                if (_recent_hosts_length < maxRecentHostsSize && recentHosts.indexOf(host) == -1) {
                    recentHosts.push(host);
                } else if (recentHosts.indexOf(host) == -1 && historyCompletions.h.indexOf(host) == -1) {
                    historyCompletions.push([host, pageFor[host].title]);
                    historyCompletions.h.push(host);
                }
            }
            if (id != '' && !id.replace('/', '').match(new RegExp('^(?:' + ignoreIds.join('|') + ')$'))) { // Wedata の拒否リストに入っていなかったら
                var index = ids.indexOf(id);
                if (index == -1) {
                    ids.unshift(id);
                } else {
                    ids.splice(index, 1);
                    ids.unshift(id);
                }
                if (isFirst || isIncreased || isUpdated) { // 初回か,pageFor に何か追加されたか,pageFor が更新されたら
                    isIncreased = false;
                    isUpdated   = false;
                    if (page.title != '' && title.get(host, id) != page.title) {
                        title.set(host, id, page.title);
                    }
                }
            }
        }
        root.containerOpen = false;
        isFirst = false;
    }
    collectLog();

    function loadWedata() {
        var url = 'http://wedata.net/databases/access_hatena_ignore_id/items.json';
        var req = XMLHttpRequest();

        req.open('GET', url, true);
        req.onload = registerIgnoreIds;
        req.onerror = function(e) { liberator.echoerr('Error in access_hatena.js: loadWedata'); };
        req.send(null);
    }

    function registerIgnoreIds(e) {
        var req = this;
        var json = eval(req.responseText);
        for (var i in json) if (json.hasOwnProperty(i)) {
            var id = json[i].data.id;
            if (ignoreIds.indexOf(id) == -1 && id != '') {
                ignoreIds.push(id)
            }
        }
    }

    commands.addUserCommand(["accesshatena"], "Access to Hatene Service Quickly",
        function(args) {
            var host = args[0] ? encodeURIComponent(args[0].toString()) : 'www';
            var id   = args[1] ? encodeURIComponent(args[1].toString()).replace('%2F', '/') : '';
            var uri  = 'http://' + host + '.hatena.ne.jp/' + id;
            var targetTab = args.bang ? liberator.CURRENT_TAB : liberator.NEW_TAB;
            liberator.open(uri, targetTab);
            lastLocation = '';
        }, {
            completer: function (context, args) {
                if (args.length == 1) {
                    context.title = ["Host", "Service"];
                    if (window.content.location.href.replace(/^https?:\/\//, '') != lastLocation) { // ページ遷移がない場合に何度も collectLog() しないように
                        collectLog();
                        lastLocation = window.content.location.href.replace(/https?:\/\//, '');
                    }
                    context.completions = [[recentHosts[i], pageFor[recentHosts[i]].title] for (i in recentHosts) if (recentHosts.hasOwnProperty(i))].concat(historyCompletions);
                } else if (args.length == 2) {
                    var host = args[0].toString();
                    context.title = ["ID", "Page"];
                    var _completions = [[ids[i], title.get(host, ids[i])] for (i in ids) if (ids.hasOwnProperty(i))];
                    context.completions = host != 'd' ? _completions.filter(function(i){ return !/\+/.test(i[0]) }) : _completions;
                }
            }
        }
    );

    commands.addUserCommand(["accesshatenainit"], "Initialize Access Hatena",
        function() {
            init();
            liberator.echo('Finish initializing.');
        }
    );
})();
// vim:sw=4 ts=4 et: