root/lang/javascript/vimperator-plugins/branches/2.1/account_switcher.js

Revision 34082, 6.5 kB (checked in by drry, 15 months ago)
  • HTTP URL の判定を修正しました。
Line 
1/**
2 * ==VimperatorPlugin==
3 * @name           account_switcher.js
4 * @description    switch account easily
5 * @description-ja 複数のアカウントを切り替える
6 * @minVersion     2.1a1pre
7 * @author         masa138
8 * @version        0.0.5
9 * ==/VimperatorPlugin==
10 *
11 * Usage:
12 * :account {username}@{servicename}
13 *
14 */
15(function(){
16    var services = [];
17    var accounts = [];
18    var nowLogin = [];
19    var isFirst  = true;
20
21    var manager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
22
23    var ns         = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
24    var statusBar  = document.getElementById('status-bar');
25    var targetElem = document.getElementById('page-report-button');
26    var afterSLine = targetElem.nextSibling;
27    var sbPannel   = document.createElementNS(ns, 'statusbarpannel');
28    var img        = sbPannel.appendChild(document.createElementNS(ns, 'image'));
29    sbPannel.id    = 'account-switcher-pannel';
30
31    var _services = {
32        google: {
33            host   : 'https://www.google.com',
34            login  : '/accounts/LoginAuth',
35            id     : 'Email',
36            pw     : 'Passwd',
37            logout : '/accounts/Logout',
38            jump   : '/accounts/ManageAccount'
39        },
40        hatena: {
41            host   : 'https://www.hatena.ne.jp',
42            login  : '/login',
43            id     : 'name',
44            pw     : 'password',
45            logout : '/logout'
46        },
47        hatelabo: {
48            host   : 'https://www.hatelabo.jp',
49            login  : '/login',
50            id     : 'mode=enter&key',
51            pw     : 'password',
52            logout : '/logout',
53            jump   : 'http://hatelabo.jp/'
54        }
55    };
56
57    function init() {
58        var rcServices = liberator.globalVariables.accountSwitcherServices;
59        rcServices = !rcServices ? [] : rcServices;
60
61        for (var key in _services)  if (_services.hasOwnProperty(key)) services[key] = _services[key];
62        for (var key in rcServices) if (rcServices.hasOwnProperty(key)) {
63            var s = rcServices[key];
64            if (services[key] == null) services[key] = s;
65            else {
66                for (var k in s) if (s.hasOwnProperty(k)) {
67                    services[key][k] = s[k];
68                }
69            }
70        }
71
72        var hosts = [key for (key in services)];
73        for (var i in hosts) {
74            var host = hosts[i];
75            if (isFirst) nowLogin[host] = '';
76            var logins = manager.findLogins({}, services[host].host, "", null);
77            for (var i = 0; i < logins.length; i++) {
78                var login = logins[i];
79                accounts[[login.username, host].join('@')] = {};
80                var a = accounts[[login.username, host].join('@')];
81                a.username = login.username,
82                a.password=login.password,
83                a.host = host;
84            }
85        }
86        isFirst = false;
87    }
88
89    function changeAccount(user) {
90        var service = services[accounts[user].host];
91        if (service.host == null || service.logout == null) return;
92
93        var username = accounts[user].username;
94        var password = accounts[user].password;
95        var req = new XMLHttpRequest();
96        var url = (service.logout.indexOf('http') != 0) ? service.host + service.logout : service.logout;
97        req.open("POST", url, true);
98        req.onload = function(e) {
99            if (service.login == null || service.id == null || service.pw == null) return;
100            var req = new XMLHttpRequest();
101            var url = (service.login.indexOf('http') != 0) ? service.host + service.login : service.login;
102            req.open("POST", url, true);
103            req.onload = function(e) {
104                if (service.jump != null) {
105                    var url = (service.jump.indexOf('http') == -1) ? service.host + service.jump : service.jump;
106                    content.location = url;
107                } else if(content.location != 'about:blank') {
108                    content.location.reload();
109                }
110                var needle = '.hatena.ne.jp';
111                if (service.host.toLowerCase().lastIndexOf(needle) == service.host.length - needle.length) {
112                    img.setAttribute('src', 'http://www.hatena.ne.jp/users/' + username.substr(0, 2) + '/' + username + '/profile_s.gif');
113                    if (!document.getElementById('account_switcher_pannel')) {
114                        if (afterSLine != null) {
115                            statusBar.insertBefore(sbPannel, afterSLine);
116                        } else {
117                            statusBar.appendChild(sbPannel);
118                        }
119                    }
120                }
121            };
122            req.onerror = function(e) { liberator.echoerr('Login error in account_switcher.js'); };
123            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
124            req.send(
125                service.id + '=' + encodeURIComponent(username) + '&' +
126                service.pw + '=' + encodeURIComponent(password)
127            );
128            nowLogin[user.substr(user.lastIndexOf('@') + 1)] = user;
129        };
130        req.onerror = function(e) { liberator.echoerr('Logout error in account_switcher.js'); };
131        req.send(null);
132    }
133
134    commands.addUserCommand(["account"], "Change Account",
135        function(args) {
136            if (!args) {
137                liberator.echo("Usage: account {username}@{servicename}");
138            } else {
139                var user = args[args.length - 1];
140                if (!user) return;
141                changeAccount(user);
142            }
143        }, {
144            completer: function(context, args) {
145                init();
146                context.title = ["Account", "Service"];
147                for (var service in nowLogin) if (nowLogin.hasOwnProperty(service)) {
148                    var username = nowLogin[service];
149                    if (username != '') delete(accounts[username]);
150                }
151                var compls = [[key, accounts[key].host] for (key in accounts) if (accounts.hasOwnProperty(key))];
152                if (args.length > 0) {
153                    for (var i = 0; i < args.length; i++) {
154                        var user = args[i];
155                        if (user != '') {
156                            compls = compls.filter(function(c) c[0].indexOf(user) != -1);
157                        }
158                    }
159                }
160                return [0, compls];
161            }
162        }
163    );
164})();
165// vim:sw=4 ts=4 et:
Note: See TracBrowser for help on using the browser.