Hi, 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 so favorite site. I participate here on one portal in the competition, one of the proposals to be among the winners to be the most active voting user.
Decision
It would be possible to write a small desktop application (there is a good experience in C #), but this is laborious, and the task should be solved in the shortest possible time. I came to the aid of Userscript, allowing you to inject your function into a page and run it.
To avoid problems with regular expressions (the standard RegEx function) used the jQuery library and the basic Javascript functions introduced it into the page:
var head = document.getElementsByTagName('head')[0]; var script= document.createElement('script'); script.type= 'text/javascript'; script.src= 'http:
The next step was to determine the voting button:
<tr> <td> <a href="#"><span></span></a> </td> <td> <span> :</span> </td> <td> <div id="thumbs_up"> <img src="thumbs_up.png" onclick="vote('UnicID', '1');" alt=""> </div> </td> <td align=center width=20px> <div id="mess_ratio_UnicID"> <span><b></b></span> </div> </td> <td> <div id="thumbs_down"> <img src="thumbs_down.png" onclick="vote('UnicID', '-1');" alt=" "> </div> </td> <td> <div id="vote_status_UnicID"> <span></span> </div> </td> <td> <span> </span><span></span> </td> </tr>
The voting button was a picture with the image "thumbs_up.png" or "thumbs_down.png", deciding to be a bad boy completely, chose to minus all publications. Thereby he lowered high ratings and “killed” publications without votes, introducing a rating in the negative.
')
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 ran through the entire site and checked every vote.
Problem
The next day in the forum someone complained to the administration about the malicious evaluator. It was impossible to continue participation in the same pace, they would suddenly block. Having overestimated the situation, I decided to remake the automatic voting and determine the current rating of each publication. The condition is as follows:
1) if the rating is less than zero or zero, then we vote +1;
2) if the rating is greater than zero, then vote -1;
The task was still the same,
do not use regular expressions. Inquired about the jQuery documentation, revealed new functions for myself ".not ()", ": contains ()", ".has () /: has ()".
Revising the cut HTML code (see above) and correcting the expression, 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:
JQuery first query
- find voting pictures [jQuery ('img [src * = thumbs_down]')]
- Then we do 3 jumps upwards by the parents and get to the nearest common parent of the rating and the vote button, this is the TR branch [.parent (). parent (). parent ()]
- we exclude from the results found those that:
a) or do not contain a “span” tag with my nickname;
b) or do not contain a “b” tag with a minus rating;
c) or do not contain a “b” tag with a rating of zero;
[.not ("tr: has (span: contains ('MyNick'), b: contains ('-'), b: contains ('0'))")] - find the negative vote button and press it [.find ('img [src * = thumbs_down]'). click ()];
JQuery second query
- find voting pictures [jQuery ('img [src * = thumbs_down]')]
- Then we do 3 jumps upwards by the parents and get to the nearest common parent of the rating and the vote button, this is the TR branch [.parent (). parent (). parent ()]
- we exclude from the results found those that:
a) or contain a “span” tag with my nickname;
b) or contain a “b” tag with a minus rating;
c) or contain a “b” tag with a rating of zero;
[.has ("span: contains ('MyNick'), b: contains ('-'), b: contains ('0')")] - find the positive vote button and press it [.find ('img [src * = thumbs_up]'). click ()];
Total
Once again confirming that laziness is the engine of progress, I made an automated and minimalized automatic voting with a rating and learned new features of jQuery. With such simple and easy-to-read functions you can avoid many lines of if-else conditions.