From 4e2d53d0898a5a4269c7d7f4b2e5b9d7bae3461c Mon Sep 17 00:00:00 2001 From: snaka Date: Mon, 27 Jul 2009 22:25:57 +0000 Subject: Add favicon git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@34653 d0d07461-0603-4401-acd4-de1884942a52 --- hateDAopener.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'hateDAopener.js') diff --git a/hateDAopener.js b/hateDAopener.js index 36305ab..0e80c3b 100644 --- a/hateDAopener.js +++ b/hateDAopener.js @@ -89,7 +89,7 @@ plugins.hateDAopener = (function(){ let log = liberator.log; let dump = function(title, obj) liberator.dump(title + "\n" + util.objectToString(obj)); // \r\n separated string to array - let rns = function(source) source.split("\r\n"); + let rns = function(source) source.split(/\r?\n/); // comma separated string to array let csv = function(source) source.split(","); let libly = liberator.plugins.libly; @@ -99,6 +99,7 @@ plugins.hateDAopener = (function(){ extractTitleAndTags: extractTitleAndTags, generateCandidates: generateCandidates, getDiaryEntries: getDiaryEntries, + getFaviconURI: getFaviconURI, }; // }}} // COMMAND //////////////////////////////////////////////////////////////{{{ @@ -112,7 +113,7 @@ plugins.hateDAopener = (function(){ context.format = { anchored: false, title: ["Title and URL", "Tags"], - keys: { text: "url", name: "name", tags: "tags"}, + keys: { text: "url", baseUrl: "baseUrl", path: "path", name: "name", tags: "tags"}, process: [templateTitleAndUrl, templateTags] }; context.filterFunc = null; @@ -127,10 +128,7 @@ plugins.hateDAopener = (function(){ // }}} // PRIVATE //////////////////////////////////////////////////////////////{{{ - let cache = { - data: null, - userId: null - }; + let cache = { }; /** * @return accounts info @@ -166,6 +164,7 @@ plugins.hateDAopener = (function(){ * @return [{"url": "(url)", "name": "hogehoge", "tags": "[hoge]"}, ... ] */ function generateCandidates() { + dump("generateCandidates"); let allEntries = []; accounts().forEach(function([userId, diary]) { let entries = getDiaryEntries(userId, diary); @@ -176,6 +175,8 @@ plugins.hateDAopener = (function(){ [title, tags] = extractTitleAndTags(titleAndTag); return { "url" : url, + "baseUrl" : 'http://' + diary + '.hatena.ne.jp/' + userId, + "path" : path, "name" : title, "tags" : tags }; @@ -191,6 +192,7 @@ plugins.hateDAopener = (function(){ * @return [String dateTime, String path, String titleAndTag] */ function getDiaryEntries(userId, diary) { + dump("getDalyEntries"); if (cache[diary + userId]) return cache[diary + userId]; @@ -244,7 +246,9 @@ plugins.hateDAopener = (function(){ function templateTitleAndUrl(item){ let simpleURL = item.text.replace(/^https?:\/\//, ''); + let favicon = getFaviconURI(item.baseUrl + '/'); return <> + {item.name} {simpleURL} @@ -252,6 +256,24 @@ plugins.hateDAopener = (function(){ ; } + let faviconCache = {}; + function getFaviconURI(pageURI) { + if (faviconCache[pageURI]) + return faviconCache[pageURI]; + try { + let uri = Cc["@mozilla.org/network/io-service;1"] + .getService(Ci.nsIIOService) + .newURI(pageURI, null, null); + let faviconURI = Cc["@mozilla.org/browser/favicon-service;1"] + .getService(Ci.nsIFaviconService) + .getFaviconImageForPage(uri); + return faviconCache[pageURI] = faviconURI.spec; + } catch(e) { + alert(pageURI); + return ""; + } + } + // }}} return self; -- cgit v1.2.3