Advertisement
overloop

govnokod_poetry.js

Aug 31st, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        govnokod_poetry
  3. // @namespace   mugiseyebrows.ru
  4. // @include     http://govnokod.ru/
  5. // @include     http://govnokod.ru/?page=*
  6. // @version     1
  7. // @grant       none
  8. // ==/UserScript==
  9.  
  10. var timeout = 1000;
  11.  
  12. function loadComments() {
  13.   var entries = $('a.entry-comments-load');
  14.   entries.each(
  15.     function(i,item){
  16.       setTimeout(function(){$(item).click()},i*timeout);
  17.     }
  18.   );
  19.   return entries.length;
  20. }
  21.  
  22. function beginsWithCapital(line) {
  23.   return line.length > 0 && ((line[0] >= 'А' && line[0] <= 'Я') /*|| (line[0] >= 'A' && line[0] <= 'Z')*/);
  24. }
  25.  
  26. function findPoetry() {
  27.   var poetry = [];
  28.   $('div.entry-comment').each(function(i,item){
  29.     var lines = $(item).text().split(/\n/);
  30.     //console.log(lines);
  31.     var maxLength = 70;
  32.     for (var i=0;i<lines.length-3;i++) {
  33.       if (
  34.         beginsWithCapital(lines[i+0]) && lines[i+0].length < maxLength &&
  35.         beginsWithCapital(lines[i+1]) && lines[i+1].length < maxLength &&
  36.         beginsWithCapital(lines[i+2]) && lines[i+2].length < maxLength &&
  37.         beginsWithCapital(lines[i+3]) && lines[i+3].length < maxLength) {
  38.  
  39.         var href = $(item).parent().find('a.comment-link').attr('href');
  40.         var author = $(item).parent().find('.entry-author').html();
  41.        
  42.         var link1 = '<a href="' + href.match(/#.*/) + '">' + 'сюда' + '</a> ';
  43.         var link2 = '<a href="' + href + '">' + 'туда' + '</a> '
  44.        
  45.         lines.push('<br>' + author + ' ' + link1 + link2);
  46.         poetry.push(lines.join("<br>"));
  47.         //console.log('true');
  48.  
  49.         break;
  50.       }
  51.     }
  52.     //console.log('false');
  53.   });
  54.   return poetry;
  55. }
  56.  
  57. function findAndShowPoetry() {
  58.   var poetry = findPoetry();
  59.   $('#mydiv').html('<p>' + poetry.join('</p><hr><p>') + '</p>');
  60.   //set target="_blank"
  61.   $('#mydiv a').each(function(i,item) {
  62.     if ($(item).attr('href').match(/^#/) == null) {
  63.       $(item).attr('target','_blank');
  64.     }
  65.   });
  66.   $('#mydiv').css('height','100%');
  67.   $('#mydiv').css('overflow','scroll');
  68. }
  69.  
  70. function toggleLeftRight(id) {
  71.   var el = $(id);
  72.   var pl = el.css('padding-left');
  73.   var pr = el.css('padding-right');
  74.   el.css('padding-left',pr);
  75.   el.css('padding-right',pl);
  76.   if (el.css('left') == '0px') {
  77.     el.css('left',null);
  78.     el.css('right','0px');
  79.   } else {
  80.     el.css('left','0px');
  81.     el.css('right',null);
  82.   }
  83. }
  84.  
  85. window.onload = function() {
  86.   var button1 = '<input type="button" id="mybutton1" value="_[]" />';
  87.   var button2 = '<input type="button" id="mybutton2" value="<->" />';
  88.   var buttons = '<div id="mybuttons" style="position:fixed; top:0px; left:0px;">' + button1 + button2 + '</div>';
  89.  
  90.   var divStyle = 'position: fixed; top: 0; left: 0; padding: 10px 10px 10px 70px; background-color: rgba(255,255,255,0.8);';
  91.   var div = '<div id="mydiv" style="' + divStyle + '"></div>'
  92.   $('body').append(div + buttons);
  93.  
  94.   $('#mybutton1').click(function(){$('#mydiv').toggle();});
  95.   $('#mybutton2').click(function(){toggleLeftRight('#mydiv');toggleLeftRight('#mybuttons');});
  96.  
  97.   $('#mydiv').html('loading...');
  98.   var length = loadComments();
  99.   setTimeout(findAndShowPoetry,(length)*timeout+3000);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement