Changeset 17865

Show
Ignore:
Timestamp:
08/19/08 00:35:03 (5 months ago)
Author:
drry
Message:
  • 正規表現を修正しました。
  • ほか。
Files:
1 modified

Legend:

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

    r17850 r17865  
    4343    null), 
    4444  r = []; 
    45   for (var i = 0;i < xp.snapshotLength;i++) 
    46     r.push(xp.snapshotItem(i)); 
     45  for (var i = 0, l = xp.snapshotLength; i < l; r.push(xp.snapshotItem(i++))); 
    4746  return r; 
    4847} 
     
    6261      left: 0px; 
    6362      overflow: hidden; 
    64       Opacity: 0.85; 
    65       min-Height: 20px; 
    66       min-Width: 20px; 
    67       max-Height: 400px; 
     63      opacity: 0.85; 
     64      min-height: 20px; 
     65      min-width: 20px; 
     66      max-height: 400px; 
    6867      padding: 2px; 
    6968      -moz-border-radius: 0 8px 8px 0; 
     
    7170    } 
    7271    div#TopHatenar_view table.TopHatenar_view_table{ 
    73       color:#CCCCCC; 
    74       font-size : 12px; 
    75       font-weight : bold; 
    76       border: none;  
    77       padding: 5px 10px;  
    78       margin : 10px 10px 10px 10px; 
    79       background-color :#333333; 
    80       border-collapse:collapse; 
     72      color: #CCCCCC; 
     73      font-size: 12px; 
     74      font-weight: bold; 
     75      border: none; 
     76      padding: 5px 10px; 
     77      margin: 10px 10px 10px 10px; 
     78      background-color: #333333; 
     79      border-collapse: collapse; 
    8180    } 
    8281    div#TopHatenar_view table.TopHatenar_view_table td{ 
    83       padding : 0px 15px; 
    84       background-color :#333333; 
    85       white-space: nowrap;  
    86       border:1px solid #CCCCCC; 
     82      padding: 0px 15px; 
     83      background-color: #333333; 
     84      white-space: nowrap; 
     85      border: 1px solid #CCCCCC; 
    8786    } 
    8887    div#TopHatenar_view table.TopHatenar_view_table a{ 
    89       color:#CCCCCC; 
    90       text-decoration:none; 
    91     } 
    92     img:after { 
     88      color: #CCCCCC; 
     89      text-decoration: none; 
     90    } 
     91    img:after{ 
    9392      display: block; 
    94       content : "<" attr(title) ">"; 
    95       color:#CCCCCC; 
     93      content: "<" attr(title) ">"; 
     94      color: #CCCCCC; 
    9695    } 
    9796 
     
    9998 
    10099 
    101   function main(){ 
     100  function main() { 
    102101 
    103102    var element = document.createElement('div'); 
     
    109108    } 
    110109    var logo = document.createElement('div'); 
    111     with(logo){ 
    112       id= 'TopHatenar_view_logo'; 
     110    with (logo) { 
     111      id = 'TopHatenar_view_logo'; 
    113112      innerHTML = "T?"; 
    114113      with (style) { 
     
    121120      } 
    122121    } 
    123     element.appendChild( logo ); 
    124          
     122    element.appendChild(logo); 
     123 
    125124    var content = document.createElement('div'); 
    126     with(content){ 
    127       id= 'TopHatenar_view_content'; 
    128         init_style(style); 
    129         with (style) { 
    130           display = 'none'; 
    131           margin = '7px 4px 4px 4px'; 
    132           width = '540px'; 
    133  
    134         } 
    135     } 
    136     element.appendChild( content ); 
    137      
     125    with (content) { 
     126      id = 'TopHatenar_view_content'; 
     127      init_style(style); 
     128      with (style) { 
     129        display = 'none'; 
     130        margin = '7px 4px 4px 4px'; 
     131        width = '540px'; 
     132 
     133      } 
     134    } 
     135    element.appendChild(content); 
     136 
    138137    document.body.appendChild(element); 
    139138  } 
     
    141140  function show_TopHatenar_view() { 
    142141 
    143       if( !already_got_chart ) { 
     142      if (!already_got_chart) { 
    144143        get_TopHatenar_chart(); 
    145144      } 
     
    147146      var TopHatenar_view_logo = document.getElementById('TopHatenar_view_logo'); 
    148147      TopHatenar_view_logo.style.display = 'none'; 
    149        
     148 
    150149      var TopHatenar_view_elem = document.getElementById('TopHatenar_view_content'); 
    151150      TopHatenar_view_elem.style.display = 'block'; 
     
    162161 
    163162  function get_TopHatenar_chart() { 
    164      
     163 
    165164    var content = document.getElementById('TopHatenar_view_content'); 
    166      
     165 
    167166    // HatenaIDを切り出す 
    168     var m = top.location.href.match(/d.hatena.ne.jp\/(\w+)(\/.*)?/); 
     167    var m = /^http:\/\/d\.hatena\.ne\.jp\/([a-zA-Z][\w-]{1,30}[a-zA-Z\d])/.exec(top.location.href); 
    169168    var hatena_id = m[1]; 
    170169 
    171170    var detail_url = 'http://tophatenar.com/view/' + hatena_id;  // 詳細ページURL 
    172      
     171 
    173172    // ID Link 
    174173    var id_link = document.createElement('a'); 
    175     with(id_link){ 
     174    with (id_link) { 
    176175      innerHTML = 'TopHatenar@' + hatena_id; 
    177       setAttribute('href',detail_url); 
    178       setAttribute('title','クリックで詳細情報ページへ'); 
    179       with(style){ 
     176      setAttribute('href', detail_url); 
     177      setAttribute('title', 'クリックで詳細情報ページへ'); 
     178      with (style) { 
    180179        fontSize = '20px'; 
    181180        textAlign = 'center'; 
     
    187186    } 
    188187    content.appendChild(id_link); 
    189      
     188 
    190189    // Ranking情報表示用 
    191190    var id_info = document.createElement('div'); 
     
    194193    // グラフを取得 
    195194    var correlation_chart = document.createElement('img'); 
    196     with(correlation_chart){ 
     195    with (correlation_chart) { 
    197196      src = 'http://tophatenar.com/chart/correlation_small/' + hatena_id; 
    198197      title = "相関グラフ"; 
     
    204203    correlation_chart_title.appendChild(correlation_chart); 
    205204    content.appendChild(correlation_chart_title); 
    206     correlation_chart_title.innerHTML += "<p>&lt;相関グラフ&gt;</P>"; 
    207      
     205    correlation_chart_title.innerHTML += "<p>&lt;相関グラフ&gt;</p>"; 
     206 
    208207    var subscriber_chart = document.createElement('img'); 
    209     with(subscriber_chart){ 
     208    with (subscriber_chart) { 
    210209      src = 'http://tophatenar.com/chart/subscriber_small/' + hatena_id; 
    211210      title = "購読者数の推移"; 
    212       init_img_style(style);     
     211      init_img_style(style); 
    213212    } 
    214213    var subscriber_chart_title = document.createElement('div'); 
     
    216215    subscriber_chart_title.style.textAlign = 'center'; 
    217216    subscriber_chart_title.appendChild(subscriber_chart); 
    218     subscriber_chart_title.innerHTML += "<P>&lt;購読者数の推移&gt;</P>"; 
     217    subscriber_chart_title.innerHTML += "<p>&lt;購読者数の推移&gt;</p>"; 
    219218    content.appendChild(subscriber_chart_title); 
    220      
     219 
    221220    var bookmark_chart = document.createElement('img'); 
    222     with(bookmark_chart){ 
     221    with (bookmark_chart) { 
    223222      src = 'http://tophatenar.com/chart/bookmark_small/' + hatena_id; 
    224223      title = "ブックマーク数の推移"; 
    225       init_img_style(style);     
     224      init_img_style(style); 
    226225    } 
    227226    var bookmark_chart_title = document.createElement('div'); 
     
    230229    bookmark_chart_title.appendChild(bookmark_chart); 
    231230    content.appendChild(bookmark_chart_title); 
    232     bookmark_chart_title.innerHTML += "<P>&lt;ブックマーク数の推移&gt;</P>"; 
    233      
     231    bookmark_chart_title.innerHTML += "<p>&lt;ブックマーク数の推移&gt;</p>"; 
     232 
    234233    // 詳細ページから情報取得 
    235234    GM_xmlhttpRequest({ 
    236235        method : 'GET', 
    237236        url : detail_url, 
    238         onload : function (res) { 
    239              var html = res.responseText 
    240              var numbers = getTags( html , "div" , "number"); 
    241              var ranks = getTags( html , "div" , "rank"); 
    242              var percentiles = getTags( html , "div" , "percentile"); 
    243               
    244              for( n in numbers ) console.log( numbers[n] ); 
    245              for( n in ranks ) console.log( ranks[n] ); 
    246              for( n in percentiles ) console.log( percentiles[n] ); 
    247               
    248               
    249              subscriber_cnt        = numbers[1].match(/\d+/gm); 
    250              subscriber_rank       = ranks[0].match(/\d+/gm); 
    251              subscriber_percentile = percentiles[0].match(/(\d|\.)+/gm); 
    252               
    253              bookmark_cnt          = numbers[2].match(/\d+/gm); 
    254              bookmark_rank         = ranks[1].match(/\d+/gm); 
    255              bookmark_percentile   = percentiles[1].match(/(\d|\.)+/gm); 
    256               
    257  
    258              console.log(subscriber_cnt        ); 
    259              console.log(subscriber_rank       ); 
    260              console.log(subscriber_percentile ); 
    261              console.log(bookmark_cnt          ); 
    262              console.log(bookmark_rank         ); 
    263              console.log(bookmark_percentile   ); 
    264               
    265               
    266               
     237        onload : function(res) { 
     238             var html = res.responseText; 
     239             var numbers = getTags(html, "div", "number"); 
     240             var ranks = getTags(html, "div", "rank"); 
     241             var percentiles = getTags(html, "div", "percentile"); 
     242 
     243             [numbers, ranks, percentiles].forEach(function(n) n.forEach(console.log)); 
     244 
     245 
     246             var subscriber_cnt        = numbers[1].match(/\d+/gm); 
     247             var subscriber_rank       = ranks[0].match(/\d+/gm); 
     248             var subscriber_percentile = percentiles[0].match(/[\d.]+/gm); 
     249 
     250             var bookmark_cnt          = numbers[2].match(/\d+/gm); 
     251             var bookmark_rank         = ranks[1].match(/\d+/gm); 
     252             var bookmark_percentile   = percentiles[1].match(/[\d.]+/gm); 
     253 
     254 
     255             console.log(subscriber_cnt       ); 
     256             console.log(subscriber_rank      ); 
     257             console.log(subscriber_percentile); 
     258             console.log(bookmark_cnt         ); 
     259             console.log(bookmark_rank        ); 
     260             console.log(bookmark_percentile  ); 
     261 
     262 
     263 
    267264             var inner = "<table class='TopHatenar_view_table'><tbody><tr>"; 
    268265             inner += "<td>購読者数 : " + subscriber_cnt + "</td>"; 
     
    276273             inner += "<td>(上位" +bookmark_percentile + "%以内) </td>"; 
    277274             inner += "</tr></tbody></table>" 
    278               
     275 
    279276             id_info.innerHTML += inner; 
    280         }, 
     277        } 
    281278      }); 
    282      
    283      
     279 
     280 
    284281    already_got_chart = true; 
    285      
    286  
    287   } 
    288  
    289    
     282 
     283 
     284  } 
     285 
     286 
    290287  function init_style(style) { 
    291     with(style) { 
     288    with (style) { 
    292289      fontSize = '12px'; 
    293290      textAlign = 'left'; 
     
    300297    } 
    301298  } 
    302    
    303   function init_img_style(style){ 
    304     with(style){ 
     299 
     300  function init_img_style(style) { 
     301    with (style) { 
    305302      border = '1px solid #CCCCCC'; 
    306303      margin = '5px'; 
     
    309306    } 
    310307  } 
    311    
    312   function getTags(html, tagName, className){ 
     308 
     309  function getTags(html, tagName, className) { 
    313310    var cls = ""; 
    314     if(className){ 
    315       cls = "[^>]*?class=\"" + className + "\""; 
    316     } 
    317     var reg = new RegExp("<" + tagName + cls + "(\\s|.)*?>([^<]*)</" + tagName + ">", "ig"); 
     311    if (className) { 
     312      cls = "(?:\\s[^>]*?)?\\sclass\\s*=\\s*([\"'])(?:(?:\\\\\\1|(?!\\1).)+?\\s+)?" + className + "(?:\\s+(?:\\\\\\1|(?!\\1).)+?)?\\1"; 
     313    } 
     314    var reg = new RegExp("<" + tagName + cls + "(?:\\s[^>]*)?>(?:(?!<" + tagName + "\\b)[\\s\\S])*?</" + tagName + "\\s*>", "ig"); 
    318315    return html.match(reg); 
    319316  } 
    320    
     317 
    321318  main(); 
    322      
     319 
    323320})();