Show
Ignore:
Timestamp:
06/21/08 16:41:07 (5 months ago)
Author:
kga
Message:

refactoring

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/userscripts/ldr_show_sbm_comments/ldr_show_sbm_comments.user.js

    r14329 r14336  
    2424        var url       = encodeURIComponent(item_link); 
    2525        var elem      = w.$('sbm_' + item_id); 
    26         var stat      = { 
    27             H : false, 
    28             L : false 
    29         }; 
    30         function updateStat () { 
    31             var mes = []; 
    32             for (var i in stat) { 
    33                 mes.push(i + ' : ' + (stat[i] ? 'Done' : 'loading...')); 
    34             } 
    35             w.message(mes.join(' ')); 
    36         } 
    37         var services = { 
    38             'Hatena' : xhttp.get('http://b.hatena.ne.jp/entry/json/?url=' + url).next(function (req) { 
    39                 stat['H'] = true; 
    40                 updateStat(); 
     26 
     27        var hatena = { 
     28            api : 'http://b.hatena.ne.jp/entry/json/?url=', 
     29            parseResponse : function (req) { 
    4130                var o = new Function('return ' + req.responseText)(); 
    4231                if (!o || o.count < 1) return []; 
    43                 return o.bookmarks.map(function (b) { 
    44                     var id = b.user; 
    45                     var dt = new Date(b.timestamp); 
    46                     var com = (b.comment.length > 0) ? '' : 'out'; 
    47                     var tags = b.tags.map(function (tag) { 
    48                         tag = escapeHTML(tag); 
    49                         return [ 
    50                             '<span class="sbm_tag">', 
    51                             '<a href="http://b.hatena.ne.jp/', id, '/', tag, '/">', tag, '</a>', 
    52                             '</span>' 
    53                         ].join(''); 
    54                     }).join(','); 
    55                     return { 
    56                         sort_key : dt.getTime(), 
    57                         comment : [ 
    58                             '<div class="flat_menu sbm_comment hatebu with', com, '_comment">', 
    59                                 '<a href="http://b.hatena.ne.jp/', id, '/">', 
    60                                 '<img src="http://www.hatena.ne.jp/users/', id.substr(0, 2), '/', id, '/profile_s.gif" width="16" height="16">', id, '</a>', 
    61                                 '<span class="date">', dt.toLocaleString(), '</span>', tags, b.comment, 
    62                             '</div>' 
    63                         ].join('') 
    64                     } 
    65                 }); 
    66             }), 
    67             'livedoor' : xhttp.get('http://api.clip.livedoor.com/json/comments?all=1&link=' + url).next(function (req) { 
    68                 stat['L'] = true; 
    69                 updateStat(); 
     32                return o.bookmarks.map(hatena.makeComment); 
     33            }, 
     34            makeTag : function (tags, id) { 
     35                return tags.map(escapeHTML).map(function (tag) { 
     36                    return [ 
     37                        '<span class="sbm_tag">', 
     38                        '<a href="http://b.hatena.ne.jp/', id, '/', tag, '/">', tag, '</a>', 
     39                        '</span>' 
     40                    ].join(''); 
     41                }).join(','); 
     42            }, 
     43            makeComment : function (o) { 
     44                var id   = o.user; 
     45                var dt   = new Date(o.timestamp); 
     46                var com  = (o.comment.length > 0) ? '' : 'out'; 
     47                var tags = hatena.makeTag(o.tags, id); 
     48                return { 
     49                    sort_key : dt.getTime(), 
     50                    comment : [ 
     51                        '<div class="flat_menu sbm_comment hatebu with', com, '_comment">', 
     52                            '<a href="http://b.hatena.ne.jp/', id, '/">', 
     53                            '<img src="http://www.hatena.ne.jp/users/', id.substr(0, 2), '/', id, '/profile_s.gif" width="16" height="16">', id, '</a>', 
     54                            '<span class="date">', dt.toLocaleString(), '</span>', tags, o.comment, 
     55                        '</div>' 
     56                    ].join('') 
     57                }; 
     58            } 
     59        }; 
     60 
     61        var livedoor = { 
     62            api : 'http://api.clip.livedoor.com/json/comments?all=1&link=', 
     63            parseResponse: function (req) { 
    7064                var o = new Function('return ' + req.responseText)(); 
    7165                if (!o.isSuccess) return []; 
    72                 return o.Comments.map(function (c) { 
    73                     var id = c.livedoor_id; 
    74                     var dt = new Date(c.created_on * 1000); 
    75                     var com = (c.notes.length > 0) ? '' : 'out'; 
    76                     var tags = c.tags.map(function (tag) { 
    77                         tag = escapeHTML(tag); 
    78                         return [ 
    79                             '<span class="sbm_tag">', 
    80                             '<a href="http://clip.livedoor.com/clips/', id, '/tag/', tag, '">', tag, '</a>', 
    81                             '</span>' 
    82                         ].join(''); 
    83                     }).join(','); 
    84                     return { 
    85                         sort_key : dt.getTime(), 
    86                         comment : [ 
    87                             '<div class="flat_menu sbm_comment ldc with', com, '_comment">', 
    88                                 '<a href="http://clip.livedoor.com/clips/', id, '">', 
    89                                 '<img src="http://image.profile.livedoor.jp/icon/', id, '_16.gif" width="16" height="16">', id, '</a>', 
    90                                 '<span class="date">', dt.toLocaleString(), '</span>', tags, escapeHTML(c.notes), 
    91                             '</div>' 
    92                         ].join('') 
    93                     } 
    94                 }); 
    95             }) 
     66                return o.Comments.map(livedoor.makeComment); 
     67            }, 
     68            makeTag : function (tags, id) { 
     69                return tags.map(escapeHTML).map(function (tag) { 
     70                    return [ 
     71                        '<span class="sbm_tag">', 
     72                        '<a href="http://clip.livedoor.com/clips/', id, '/tag/', tag, '">', tag, '</a>', 
     73                        '</span>' 
     74                    ].join(''); 
     75                }).join(','); 
     76            }, 
     77            makeComment : function (o) { 
     78                var id   = o.livedoor_id; 
     79                var dt   = new Date(o.created_on * 1000); 
     80                var com  = (o.notes.length > 0) ? '' : 'out'; 
     81                var tags = livedoor.makeTag(o.tags, id); 
     82                return { 
     83                    sort_key : dt.getTime(), 
     84                    comment : [ 
     85                        '<div class="flat_menu sbm_comment ldc with', com, '_comment">', 
     86                            '<a href="http://clip.livedoor.com/clips/', id, '">', 
     87                            '<img src="http://image.profile.livedoor.jp/icon/', id, '_16.gif" width="16" height="16">', id, '</a>', 
     88                            '<span class="date">', dt.toLocaleString(), '</span>', tags, escapeHTML(o.notes), 
     89                        '</div>' 
     90                    ].join('') 
     91                }; 
     92            } 
    9693        }; 
    9794 
    98         updateStat(); 
    9995        w.LoadEffect.Start(); 
    100         parallel(services).next(function (o) { 
    10196 
    102             updateStat(); 
     97        parallel([ 
     98            xhttp.get(hatena.api + url).next(hatena.parseResponse), 
     99            xhttp.get(livedoor.api + url).next(livedoor.parseResponse) 
     100        ]).next(function (o) { 
    103101            w.LoadEffect.Stop(); 
    104102