Skip to content

Instantly share code, notes, and snippets.

@kon3ko
Last active July 12, 2019 20:45
Show Gist options
  • Save kon3ko/1906a4b942f94d7048c1a916a750d776 to your computer and use it in GitHub Desktop.
Save kon3ko/1906a4b942f94d7048c1a916a750d776 to your computer and use it in GitHub Desktop.
Tampermonkey-SiamBitAutoVote.js
// ==UserScript==
// @name [SiamBit] Auto Vote
// @namespace http://tampermonkey.net/
// @version 0.11
// @description Auto vote your torrent
// @author น้องแมว
// @match https://www.siambit.me/snatchdown.php*
// @require https://code.jquery.com/jquery-1.12.4.min.js
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_addStyle
// @updateURL https://gist.github.com/akoneko/1906a4b942f94d7048c1a916a750d776/raw/Tampermonkey-SiamBitAutoVote.js
// @downloadURL https://gist.github.com/akoneko/1906a4b942f94d7048c1a916a750d776/raw/Tampermonkey-SiamBitAutoVote.js
// ==/UserScript==
(function() {
'use strict';
let $ = window.jQuery;
let debug = false;
let queues = [];
let queueRound = 0;
let delay = [600,1200];
let point = GM_getValue('point',5);
let run = GM_getValue('run',null);
let page = GM_getValue('page',10);
let maxReCheck = 10;
let worked = false;
let delayRun = 0;
let reCheck = 0;
let rows = $(".mainouter form table[border=1] tr td:nth-child(4)");
//Queue
$.queue = function(queue) {
var q = queue.sort(function(a, b) {
return a.delay - b.delay;
}),
next, pre = {
delay: 0
};
var run = function() {
next = q.shift();
if (!next) return;
setTimeout(function() {
next.fn();
pre = next;
return run();
}, next.delay - pre.delay);
};
return {
run: run
};
}
function random(min, max) {
return Math.ceil(Math.random() * (max - min) + min);
}
function excuteQueues(q){
$.queue(q).run();
}
function gotoPage(page) {
var params = new URLSearchParams(window.location.search);
params.set('page', page);
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + params.toString();
window.location.href = newUrl;
}
function nextPage(){
return gotoPage(getPage() + 1);
}
function getPage(){
var result = null,
parameterName = 'page',
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return parseInt(result ? result : 0);
}
function check(){
return $("div > a>img[src='pic/v1.1.png']",rows).length;
}
function loadSetting(){
//point
point = $("#auto_vote select[name='point']").val();
GM_setValue('point',point)
//page
page = $("#auto_vote #page").val();
GM_setValue('page',page)
}
function setSetting(){
$("#auto_vote select[name='point']").val(point)
$("#auto_vote #page").val(isNaN(page) ? 0 : page);
$("#auto_vote #page_current").val(getPage());
}
function vote(){
let length = check();
if(length == '0'){
if(debug) console.log('done')
if(page > getPage()){
nextPage();
}else{
alert("Auto Vote : ทำงานเสร็จเรียบร้อยแล้ว :)")
stop();
}
return;
}
queues = [];
delayRun = 0;
if(debug) console.log(queues)
$(".mainouter form table[border=1] tr td:nth-child(4)").each(function(index){
let voteUrl = $("div > a",$(this)).attr('onclick');
if(voteUrl){
voteUrl = voteUrl.replace('vpoint=1','vpoint=' + point)
//eval(voteUrl)
let _delay = random(delay[0], delay[1]);
let _round = queueRound;
delayRun = delayRun + _delay;
if(debug) console.log('loop' + index)
queues.push({
delay: delayRun,
fn: function() {
if(run && _round === queueRound){
if(debug) console.log('Exe :' + voteUrl)
eval(voteUrl)
}else{
return false;
}
if(debug) console.log('push round : ' + _round,queueRound,index)
}
});
if(length === queues.length){
queues.push({
delay: delayRun,
fn: function() {
if(run && _round === queueRound){
if(debug) console.log('re-check : ' + reCheck)
if(reCheck >= maxReCheck) {
location.reload();
return;
}
vote();
reCheck++;
}
}
});
if(run) excuteQueues(queues)
worked = true;
}
}
});
}
//Start
function start(){
console.log(1)
$('#auto_vote_start').prop('disabled', true)
$('#auto_vote_stop').prop('disabled', false)
loadSetting()
if(worked){
excuteQueues(queues);
}
GM_setValue('run',true)
run = true;
reCheck = 0;
vote();
}
//Stop
function stop(){
$('#auto_vote_stop').prop('disabled', true)
$('#auto_vote_start').prop('disabled', false)
GM_setValue('run',false)
run = false;
queueRound++;
}
//Display
GM_addStyle(`
.form-inline {
display: flex;
flex-flow: row wrap;
align-items: center;
}
.form-inline label {
margin: 5px 10px 5px 20px;
}
.form-inline input {
vertical-align: middle;
margin: 5px 10px 5px 0;
padding: 9px;
background-color: #fff;
border: 1px solid #ddd;
width:50px;
}
.form-inline input:disabled,
.form-inline input:[disabled]{
background-color:#ccc !important;;
}
.form-inline select {
margin: 5px 10px 5px 0;
padding: 9px;
background-color: #fff;
border: 1px solid #ddd;
}
.form-inline button {
margin: 0 0 0 10px;
padding: 10px 20px;
background-color: dodgerblue;
border: 1px solid #ddd;
color: white;
cursor: pointer;
}
.form-inline button.red {
background-color:red;
}
.form-inline button:hover{
background-color: #0556da;;
}
.form-inline button.red:hover{
background-color: #771111;;
color:#fff;
}
.form-inline button:disabled,
.form-inline button[disabled]{
background-color: #cccccc;
color: #666666;
}
`);
let html = `
<div style="position:fixed;width:100%;box-shadow: 0 1px 0 rgba(12,13,14,0.1), 0 1px 6px rgba(59,64,69,0.1);height:50px;background-color: #fafafb;z-index: 999;">
<form class="form-inline" id="auto_vote">
<label for="point">โหวต</label>
<select name="point" id="point" required="required" aria-required="true">
<option value="1" selected="true" id="point-0">ธรรมดา</option>
<option value="2" id="point-1">ปานกลาง</option>
<option value="3" id="point-2">ดี</option>
<option value="4" id="point-3">ดีมาก</option>
<option value="5" id="point-4">แจ่มแมว</option>
</select>
<label for="page">หน้าที่ต้องการหยุด</label>
<input type="tel value="" id="page_current" disabled>
<input type="tel name="page" value="10" id="page">
<button type="button" id="auto_vote_start">ทำงาน</button>
<button type="button" class="red" id="auto_vote_stop">หยุดทำงาน</button>
</form>
</div>
<div style="height:50px;"></div>
`;
$('body').prepend(html)
$('#auto_vote_start').click(function(){
start();
})
$('#auto_vote_stop').click(function(){
stop();
})
//Run
setSetting();
if(run){
start();
}else{
stop();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment