Advertisement
overloop

vk_performer.js

Feb 6th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sortn(es) {
  2.    
  3.     if (es.length<1)
  4.         return [];
  5.    
  6.     es.sort();
  7.    
  8.     var name = es[0];
  9.     var count = 1;
  10.     var res = [];
  11.    
  12.     for (var i=1;i<es.length;i++) {
  13.         if (es[i] != name) {
  14.             res.push({name:name,count:count});
  15.             name = es[i];
  16.             count = 1;
  17.         } else {
  18.             count = count + 1;
  19.         }
  20.     }
  21.     res.push({name:name,count:count});
  22.    
  23.     res.sort(function(a,b){
  24.         if (a.count<b.count) {
  25.             return 1;
  26.         } else if (a.count>b.count) {
  27.             return -1;
  28.         }
  29.         return 0;
  30.     });
  31.    
  32.     return res;
  33. }
  34.  
  35. function query(qs,es,res) {
  36.     var q = qs.shift();
  37.     var tag = null;
  38.     var class_ = null;
  39.     var first = false;
  40.     if (q.hasOwnProperty('tag')) {
  41.         tag = q['tag'];
  42.     }
  43.     if (q.hasOwnProperty('class')) {
  44.         class_ = q['class'];
  45.     }
  46.     if (es == null) {
  47.         es = [document.body];
  48.     }
  49.     if (tag != null) {
  50.         var es_ = [];
  51.         es.forEach(function(m){
  52.             [].slice.call(m.getElementsByTagName(tag)).forEach(function(n){
  53.                 if (class_ != null) {
  54.                     if (n.classList.contains(class_)) {
  55.                         es_.push(n);
  56.                     }
  57.                 } else {
  58.                     es_.push(n);
  59.                 }
  60.             });
  61.         });
  62.         es = es_;
  63.     } else {
  64.         console.log('function query(q,f) error: tag not defined, q:');
  65.         console.log(q);
  66.     }
  67.     if (qs.length == 0) {
  68.         if (res.hasOwnProperty('all')) {
  69.             res.all(es);
  70.         }
  71.         if (res.hasOwnProperty('each')) {
  72.             es.forEach(res.each);
  73.         }
  74.     } else {
  75.         query(qs,es,res);
  76.     }
  77. }
  78.  
  79. function objectArrayAsTable(oa,props) {
  80.     var trs = [];
  81.     for (var i=0;i<oa.length;i++) {
  82.         var o = oa[i];
  83.         var row = [];
  84.         for (var j=0;j<props.length;j++) {
  85.             if (o.hasOwnProperty(props[j])) {
  86.                 row.push(o[props[j]]);
  87.             } else {
  88.                 row.push('');
  89.             }
  90.         }
  91.         trs.push('<td>' + row.join('</td><td>') + '</td>');
  92.     }
  93.     return '<table>' + '<tr>' + trs.join('</tr><tr>') + '</tr>' + '</table>';
  94. }
  95.  
  96. var perf = [];
  97. query([{tag:'div','class':'title_wrap'},{tag:'b'},{tag:'a'}],null,{each:function(e){
  98.         e.textContent.split(/[,&]/).forEach(function(n){
  99.             var m = n.match(/\(C[.]?[Vv][:.] (.*)\)/);
  100.             if (m) {
  101.                 perf.push(m[1].trim());
  102.                 //console.log('cv: ' + n.trim() + ' -> ' + m[1].trim());
  103.             } else {
  104.                 perf.push(n.trim());
  105.                 //console.log('no cv: ' + n.trim());
  106.             }
  107.         });
  108.     }
  109. });
  110. var perf_ = sortn(perf);
  111. document.body.innerHTML = objectArrayAsTable(perf_,['name','count']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement