| 1 | /** |
|---|
| 2 | * ==VimperatorPlugin== |
|---|
| 3 | * @name access_hatena.js |
|---|
| 4 | * @description Access to Hatena Service quickly |
|---|
| 5 | * @description-ja はてなのサービスに簡単にアクセス |
|---|
| 6 | * @minVersion 2.1a1pre |
|---|
| 7 | * @author id:masa138, id:hitode909 |
|---|
| 8 | * @version 0.5.8 |
|---|
| 9 | * ==/VimperatorPlugin== |
|---|
| 10 | * |
|---|
| 11 | * Usage: |
|---|
| 12 | * :accesshatena -> access to http://www.hatena.ne.jp/ |
|---|
| 13 | * |
|---|
| 14 | * :accesshatena {host} -> access to http://{host}.hatena.ne.jp/ |
|---|
| 15 | * |
|---|
| 16 | * :accesshatena {host} {user_id} -> access to http://{host}.hatena.ne.jp/{user_id}/ |
|---|
| 17 | * |
|---|
| 18 | * :accesshatena {group_name}.g {user_id} -> access to http://{group_name}.g.hatena.ne.jp/{user_id}/ |
|---|
| 19 | * |
|---|
| 20 | */ |
|---|
| 21 | (function(){ |
|---|
| 22 | var alwaysCollect; |
|---|
| 23 | var useWedata; |
|---|
| 24 | |
|---|
| 25 | var wedataCache = []; |
|---|
| 26 | var ignoreIds; |
|---|
| 27 | var ids; |
|---|
| 28 | var pageFor; |
|---|
| 29 | var title; |
|---|
| 30 | |
|---|
| 31 | function Title() { |
|---|
| 32 | this.initialize.apply(this, arguments); |
|---|
| 33 | } |
|---|
| 34 | Title.prototype = { |
|---|
| 35 | initialize: function() { |
|---|
| 36 | this.title = []; |
|---|
| 37 | }, |
|---|
| 38 | key: function(host, id) { |
|---|
| 39 | return [host, id.replace('/', '')].join(':'); |
|---|
| 40 | }, |
|---|
| 41 | set: function(host, id, title) { |
|---|
| 42 | this.title[this.key(host, id)] = title; |
|---|
| 43 | }, |
|---|
| 44 | get: function(host, id, title) { |
|---|
| 45 | if (!this.title[this.key(host, id)]) return host + '.hatena.ne.jp/' + id; |
|---|
| 46 | return this.title[this.key(host, id)]; |
|---|
| 47 | } |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | function init(flag) { |
|---|
| 51 | ids = []; |
|---|
| 52 | pageFor = []; |
|---|
| 53 | title = new Title(); |
|---|
| 54 | |
|---|
| 55 | alwaysCollect = liberator.globalVariables.accessHatenaAlwaysCollect; |
|---|
| 56 | useWedata = liberator.globalVariables.accessHatenaUseWedata; |
|---|
| 57 | ignoreIds = liberator.globalVariables.accessHatenaIgnoreIds; |
|---|
| 58 | alwaysCollect = (alwaysCollect != null) ? alwaysCollect : true; |
|---|
| 59 | useWedata = (useWedata != null) ? useWedata : true; |
|---|
| 60 | ignoreIds = (ignoreIds != null) ? ignoreIds : ['login']; |
|---|
| 61 | |
|---|
| 62 | if (flag && useWedata) loadWedata(); |
|---|
| 63 | |
|---|
| 64 | if (useWedata) { |
|---|
| 65 | for (i in wedataCache) if (wedataCache.hasOwnProperty(i)) { |
|---|
| 66 | var id = wedataCache[i]; |
|---|
| 67 | if (ignoreIds.indexOf(id) == -1) { |
|---|
| 68 | ignoreIds.push(id) |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | init(true); |
|---|
| 74 | |
|---|
| 75 | function collectLog() { |
|---|
| 76 | init(false); |
|---|
| 77 | var visited = history.get('.hatena.ne.jp'); |
|---|
| 78 | for (i in visited) if (visited.hasOwnProperty(i)) { |
|---|
| 79 | var page = visited[i]; |
|---|
| 80 | |
|---|
| 81 | page.url.match('^https?://([a-zA-Z0-9.-]+)\\.hatena\\.ne\\.jp/([a-zA-Z][a-zA-Z0-9_-]{1,30}[a-zA-Z0-9]/?)?'); |
|---|
| 82 | var host = RegExp.$1; |
|---|
| 83 | var id = RegExp.$2; |
|---|
| 84 | if (host != '') { |
|---|
| 85 | if (!pageFor[host]) { |
|---|
| 86 | pageFor[host] = page; |
|---|
| 87 | } |
|---|
| 88 | if (pageFor[host].url.length > page.url.length) { |
|---|
| 89 | pageFor[host] = page; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | if (id != '' && !id.match('^(?:' + ignoreIds.join('|') + ')$') && ids.indexOf(id) == -1) { |
|---|
| 93 | ids.push(id); |
|---|
| 94 | if (id.indexOf('/') != -1) { |
|---|
| 95 | if (page.url.match('^https?://' + host + '\\.hatena\\.ne\\.jp/' + id + '$') && title.get(host, id) != page.title) { |
|---|
| 96 | title.set(host, id, page.title); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | collectLog(); |
|---|
| 103 | |
|---|
| 104 | function loadWedata() { |
|---|
| 105 | var url = 'http://wedata.net/databases/access_hatena_ignore_id/items.json'; |
|---|
| 106 | var req = XMLHttpRequest(); |
|---|
| 107 | |
|---|
| 108 | req.open('GET', url, true); |
|---|
| 109 | req.onload = registerIgnoreIds; |
|---|
| 110 | req.onerror = function(e) { liberator.echoerr('Error in access_hatena.js: loadWedata'); }; |
|---|
| 111 | req.send(null); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | function registerIgnoreIds(e) { |
|---|
| 115 | var req = this; |
|---|
| 116 | var json = eval(req.responseText); |
|---|
| 117 | for (var i in json) if (json.hasOwnProperty(i)) { |
|---|
| 118 | var id = json[i].data.id; |
|---|
| 119 | if (wedataCache.indexOf(id) == -1 && id != '') { |
|---|
| 120 | wedataCache.push(id); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | commands.addUserCommand(["accesshatena"], "Access to Hatene Service Quickly", |
|---|
| 126 | function(args) { |
|---|
| 127 | var host = args[0] ? encodeURIComponent(args[0].toString()) : 'www'; |
|---|
| 128 | var id = args[1] ? encodeURIComponent(args[1].toString()).replace('%2F', '/') : ''; |
|---|
| 129 | var uri = 'http://' + host + '.hatena.ne.jp/' + id; |
|---|
| 130 | liberator.open(uri, liberator.CURRENT_TAB); |
|---|
| 131 | }, { |
|---|
| 132 | completer: function (context, args) { |
|---|
| 133 | if (args.length == 1) { |
|---|
| 134 | if (alwaysCollect) collectLog(); |
|---|
| 135 | context.title = ["Host", "Service"]; |
|---|
| 136 | context.completions = [[host, pageFor[host].title] for (host in pageFor) if (pageFor.hasOwnProperty(host))]; |
|---|
| 137 | } else if (args.length == 2) { |
|---|
| 138 | var host = args[0].toString(); |
|---|
| 139 | context.title = ["ID", "Page"]; |
|---|
| 140 | context.completions = [[ids[i], title.get(host, ids[i])] for (i in ids) if (ids.hasOwnProperty(i))]; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | ); |
|---|
| 145 | |
|---|
| 146 | commands.addUserCommand(["accesshatenainit"], "Initialize Access Hatena", |
|---|
| 147 | function() { |
|---|
| 148 | init(true); |
|---|
| 149 | liberator.echo('Finish initializing.'); |
|---|
| 150 | } |
|---|
| 151 | ); |
|---|
| 152 | })(); |
|---|
| 153 | // vim:sw=4 ts=4 et: |
|---|