Changeset 34667
- Timestamp:
- 07/29/09 02:53:46 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/javascript/vimperator-plugins/trunk/hateDAopener.js
r34653 r34667 86 86 // }}} 87 87 plugins.hateDAopener = (function(){ 88 // UTILITY //////////////////////////////////////////////////////////////{{{ 89 let log = liberator.log; 90 let dump = function(title, obj) liberator.dump(title + "\n" + util.objectToString(obj)); 91 // \r\n separated string to array 92 let rns = function(source) source.split(/\r?\n/); 93 // comma separated string to array 94 let csv = function(source) source.split(","); 88 95 89 let libly = liberator.plugins.libly; 96 // }}}97 90 // PUBLIC ///////////////////////////////////////////////////////////////{{{ 98 91 let self = { 99 92 extractTitleAndTags: extractTitleAndTags, 100 93 generateCandidates: generateCandidates, 101 getDiaryEntries: getDiaryEntries,102 getFaviconURI: getFaviconURI,94 getDiaryEntries: getDiaryEntries, 95 getFaviconURI: getFaviconURI, 103 96 }; 104 97 // }}} … … 119 112 context.filterFunc = null; 120 113 context.regenerate = true; 121 context.generate = function() inputFilter(args);114 context.generate = function() filteredCandidates(args); 122 115 }, 123 116 argCount: "*", … … 129 122 // }}} 130 123 // PRIVATE //////////////////////////////////////////////////////////////{{{ 131 let cache = { };132 124 133 125 /** … … 138 130 * ['snaka72', 'vimperator.g']] 139 131 */ 140 function accounts() { 141 return liberator.globalVariables.hateDAopener_accounts || []; 142 } 132 function accounts() 133 liberator.globalVariables.hateDAopener_accounts || []; 143 134 144 135 /** 145 136 * filter candidates by words 146 137 */ 147 function inputFilter(words) { 148 var start = new Date; 149 dump(words.join(',') + "> start:" + start + "\n"); 150 words = words.map(function(i) i.toLowerCase()); 151 let filtered = generateCandidates().filter( 152 function(i) { 153 let targetString = [i.tags, i.name].join(",").toLowerCase(); 154 return words.every(function(word) targetString.indexOf(word) > -1); 155 } 156 ); 157 var end = new Date; 158 dump(words.join(',') + "> end :" + end + "[" + (end - start) + "]\n"); 159 return filtered; 160 } 138 function filteredCandidates(words) { 139 return generateCandidates() 140 .filter(function(i) 141 let (targetString = '' + i.tags + ' ' + i.name) 142 words.every(function(word) targetString.match(word, 'i')) 143 ); 144 } 145 146 function hatenaDiaryUrl(diary, userId) 147 'http://' + diary + '.hatena.ne.jp/' + userId; 161 148 162 149 /** … … 165 152 */ 166 153 function generateCandidates() { 167 dump("generateCandidates");168 154 let allEntries = []; 155 let notEmpty = function(i) i && i != ""; 156 169 157 accounts().forEach(function([userId, diary]) { 170 let entries = getDiaryEntries(userId, diary); 171 entries = entries.filter(function(i) i && i != ""); 172 entries = entries.map(function([dateTime, path, titleAndTag]) { 173 let url = 'http://' + diary + '.hatena.ne.jp/' + userId + path; 174 let title, tags; 175 [title, tags] = extractTitleAndTags(titleAndTag); 176 return { 177 "url" : url, 178 "baseUrl" : 'http://' + diary + '.hatena.ne.jp/' + userId, 179 "path" : path, 180 "name" : title, 181 "tags" : tags 182 }; 183 }); 158 let entries = getDiaryEntries(userId, diary) 159 .filter(notEmpty) 160 .map(function([dateTime, path, titleAndTag]) 161 let ([title, tags] = extractTitleAndTags(titleAndTag)) { 162 "url" : hatenaDiaryUrl(diary, userId) + path, 163 "baseUrl" : hatenaDiaryUrl(diary, userId), 164 "path" : path, 165 "name" : title, 166 "tags" : tags 167 } 168 ); 184 169 allEntries = allEntries.concat(entries); 185 170 }); … … 192 177 * @return [String dateTime, String path, String titleAndTag] 193 178 */ 194 function getDiaryEntries(userId, diary) { 195 dump("getDalyEntries"); 196 if (cache[diary + userId]) 197 return cache[diary + userId]; 198 199 let req = new libly.Request( 200 "http://"+ diary + ".hatena.ne.jp/" + userId + "/archive/plaintext", 201 null, 202 { asynchronous: false } 203 ); 204 let result; 205 req.addEventListener("onSuccess", function(data) { 206 dump("request succeeded", data); 207 result = data; 208 }); 209 req.addEventListener("onFailure", function(data) { 210 dump("request failed", data); 211 return; 212 }); 213 req.get(); 214 215 let entries = rns(result.responseText); 216 entries = entries.map(function(i) i.split(',')); 217 return cache[diary + userId] = entries; 218 } 179 let getDiaryEntries = (function() { 180 let cache = {}; 181 return function(userId, diary) { 182 if (cache[diary + userId]) 183 return cache[diary + userId]; 184 let res = util.httpGet(hatenaDiaryUrl(diary, userId) + "/archive/plaintext"); 185 return cache[diary + userId] = res.responseText 186 .split(/\r?\n/) 187 .map(function(i) i.split(',')); 188 }; 189 })(); 219 190 220 191 /** … … 223 194 */ 224 195 function extractTitleAndTags(titleAndTag) { 225 let patternTagAndTitle = /(\[.*\])?(.*)/; 226 if (!patternTagAndTitle.test(titleAndTag)) 227 return []; 228 229 let tags, title; 230 [,tags, title] = titleAndTag.match(patternTagAndTitle); 231 232 let patternTags = /\[([^\]]+)\]/g; 233 if (!patternTags.test(tags)) 234 return [title, []]; 235 236 tags = tags.match(patternTags); 196 let patternTags = /\[[^\]]+\]/g; 237 197 return [ 238 title ,239 tags198 titleAndTag.replace(patternTags, ''), 199 (titleAndTag.match(patternTags) || []) 240 200 ]; 241 }242 243 function templateTags(item){244 return item.tags && item.tags.length > 0 ? item.tags.join("") : "";245 201 } 246 202 … … 257 213 } 258 214 259 let faviconCache = {}; 260 function getFaviconURI(pageURI) { 261 if (faviconCache[pageURI]) 262 return faviconCache[pageURI]; 263 try { 215 function templateTags(item){ 216 return item.tags && item.tags.length > 0 ? item.tags.join("") : ""; 217 } 218 219 let getFaviconURI = (function() { 220 let faviconCache = {}; 221 222 return function (pageURI) { 223 if (faviconCache[pageURI]) 224 return faviconCache[pageURI]; 225 264 226 let uri = Cc["@mozilla.org/network/io-service;1"] 265 227 .getService(Ci.nsIIOService) … … 269 231 .getFaviconImageForPage(uri); 270 232 return faviconCache[pageURI] = faviconURI.spec; 271 } catch(e) {272 alert(pageURI);273 return "";274 233 } 275 } 276 277 // }}} 278 234 })(); 235 236 // }}} 237 // UTILITY //////////////////////////////////////////////////////////////{{{ 238 function dump(title, obj) 239 liberator.dump(title + "\n" + util.objectToString(obj)); 240 241 function rns(source) 242 source.split(/\r?\n/); 243 244 function csv(source) 245 source.split(","); 246 247 // }}} 279 248 return self; 280 249 })();
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)