Automation through Userscript

Hello, Habr!

Introduction

I want to share with the personal discovery of using a bunch of Userscript and jQuery to automate actions on your favorite or not very favorite site. I’m participating here on one portal in the contest, one of the proposals is to be among the winners to be the most actively voting user.

Decision

One could write a small desktop application (there is good experience in C #), but this is time-consuming, and the problem must be solved as soon as possible. Userscript came to my aid, allowing you to embed your function in the page and run it.
To avoid problems with regular expressions (the standard RegEx function) I used the jQuery library and Javascript embedded the basic functions into the page:
   var head = document.getElementsByTagName('head')[0];
   var script= document.createElement('script');
   script.type= 'text/javascript';
   script.src= 'http://code.jquery.com/jquery-latest.js';
   head.appendChild(script);

The next step was to define the vote button: The vote button was a picture with the image “thumbs_up.png” or “thumbs_down.png”, deciding to be finally a bad boy, he chose to minus all publications. Thus, he lowered high ratings and “killed” publications without votes, introducing a rating of minus.
АВТОРРейтинг записи:
Нравится
РЕЙТИНГ
Не нравится
Сообщение оставлено ВРЕМЯ



By calling the following code, there was a vote for (more precisely, against) all publications on the page:
   jQuery('img[src*=thumbs_down]').click();

Then I went over the entire site and monitored each vote.

Problem

The next day, someone in the forum complained to the administration about a malicious appraiser. It was impossible to continue participating at the same pace, they would suddenly block. Having overestimated the situation, I decided to redo the automatic vote and determine the current rating of each publication. The condition was as follows:
1) if the rating is less than zero or zero, then vote +1;
2) if the rating is greater than zero, then vote -1;

The task was the same, not to use regular expressions. He took an interest in jQuery documentation, revealed new functions ".not ()", ": contains ()", ".has () /: has ()".
After reviewing the cut HTML code (see above) and correcting the expression, I received the following code:
  jQuery('img[src*=thumbs_down]').parent().parent().parent().not("
        tr:has(span:contains('MyNick'), 
                   b:contains('-'),
                   b:contains('0')
        )").find('img[src*=thumbs_down]').click();
  jQuery('img[src*=thumbs_down]').parent().parent().parent().has("
                   span:contains('MyNick'),
                   b:contains('-'),
                   b:contains('0')
            ").find('img[src*=thumbs_up]').click(); 

So, in order:
The first jQuery query
  1. find voting pictures [jQuery ('img [src * = thumbs_down]')]
  2. then we make 3 jumps up the parents and get to the nearest common parent of the rating and voting button, this is the TR branch [.parent (). parent (). parent ()]
  3. weed out from the results found those that:
    a) or do not contain the span tag with my nickname;
    b) or do not contain a tag “b” with a rating of minus;
    c) or do not contain a tag “b” with a rating of zero;
    [.not (“tr: has (span: contains ('MyNick'), b: contains ('-'), b: contains ('0'))”]
  4. find the negative vote button and click it [.find ('img [src * = thumbs_down]'). click ()];

Second jQuery query
  1. find voting pictures [jQuery ('img [src * = thumbs_down]')]
  2. then we make 3 jumps up the parents and get to the nearest common parent of the rating and voting button, this is the TR branch [.parent (). parent (). parent ()]
  3. weed out from the results found those that:
    a) or contain a span tag with my nickname;
    b) or contain a tag “b” with a rating of minus;
    c) or contain a tag “b” with a rating of zero;
    [.has (“span: contains ('MyNick'), b: contains ('-'), b: contains ('0')„)]
  4. find the positive vote button and click it [.find ('img [src * = thumbs_up]'). click ()];

Total

Confirming once again that laziness is the engine of progress, made automated and minimized automatic voting with rating and learned new features of jQuery. With such simple and readable functions, you can avoid many lines of if-else conditions.

Also popular now: