Skip to content

Instantly share code, notes, and snippets.

@valuks
Created January 4, 2019 21:22
Show Gist options
  • Save valuks/ce49e55eca4e92f291efdb1548b4e351 to your computer and use it in GitHub Desktop.
Save valuks/ce49e55eca4e92f291efdb1548b4e351 to your computer and use it in GitHub Desktop.
var Likes, Shares, count, next, random;
next = function(query, callback, times = 20, time = 0) {
return setTimeout(() => {
if (time >= times) {
return console.error(`script error ${times} ${time}`);
}
if (query()) {
return callback();
}
return next(query, callback, times, time + 1);
}, 200);
};
Likes = (function() {
class Likes {
button(callback) {
document.querySelectorAll(this._button)[0].click();
return next((() => {
return document.querySelectorAll(this._list).length > 0;
}), callback);
}
_list_load_button() {
if (this.$popup.querySelectorAll(this._more_button).length === 0) {
return false;
}
this.$popup.querySelectorAll(this._more_button)[0].click();
return true;
}
list_load(callback) {
var times;
if (this._count && this._count === document.querySelectorAll(this._list).length) {
return callback();
}
this._count = document.querySelectorAll(this._list).length;
if (!this._list_load_button()) {
return callback();
}
times = 0;
return next((() => {
times++;
if (times > 10) {
return true;
}
return this._count !== document.querySelectorAll(this._list).length;
}), () => {
return this.list_load(callback);
});
}
close(callback) {
this.$popup.querySelectorAll('.layerCancel')[0].click();
return next((() => {
return document.querySelectorAll(this._list).length === 0;
}), () => {
this.$popup = null;
return callback();
});
}
numbers() {
return document.querySelectorAll(this._list).forEach((el, i) => {
return el.querySelectorAll(this._list_a)[0].innerHTML = (i + 1) + '. ' + el.querySelectorAll(this._list_a)[0].innerHTML;
});
}
mark(id) {
return document.querySelectorAll(this._list)[id - 1].querySelectorAll(this._list_a)[0].innerHTML = '::WINNER:: ' + document.querySelectorAll(this._list)[id - 1].querySelectorAll(this._list_a)[0].innerHTML;
}
get(callback) {
return this.button(() => {
this.$popup = document.querySelectorAll(this._list)[0].closest("[role='dialog']");
return this.list_load(callback);
});
}
};
Likes.prototype._button = '._3dlf';
Likes.prototype._list = '._5i_q';
Likes.prototype._list_a = 'a';
Likes.prototype._more_button = '[rel="async"]';
return Likes;
}).call(this);
Shares = (function() {
class Shares extends Likes {
constructor() {
super();
this._count_times = 0;
}
_list_load_button() {
window.scroll(0, 1000000);
return true;
}
};
Shares.prototype._button = '._355t a';
Shares.prototype._list = '#repost_view_dialog ._3ccb';
Shares.prototype._list_a = '.profileLink';
return Shares;
}).call(this);
count = (callback) => {
var likes, shares;
likes = new Likes();
shares = new Shares();
return likes.get(() => {
console.info(`likes: ${likes._count}`);
return likes.close(() => {
return shares.get(() => {
console.info(`shares: ${shares._count}`);
return shares.close(() => {
return callback({
likes: likes._count,
shares: shares._count
});
});
});
});
});
};
random = ({likes, shares}) => {
var items;
items = (function() {
var results = [];
for (var j = 1; 1 <= likes ? j <= likes : j >= likes; 1 <= likes ? j++ : j--){ results.push(j); }
return results;
}).apply(this).map(function(v) {
return {
id: v,
platform: 'likes'
};
}).concat((function() {
var results = [];
for (var j = 1; 1 <= shares ? j <= shares : j >= shares; 1 <= shares ? j++ : j--){ results.push(j); }
return results;
}).apply(this).map(function(v) {
return {
id: v,
platform: 'shares'
};
}));
return items[Math.floor(Math.random() * items.length)];
};
count((res) => {
var popup, winner;
winner = random(res);
console.info(`Winner is on ${winner.platform} nr.: ${winner.id}`);
if (winner.platform === 'likes') {
popup = new Likes();
} else {
popup = new Shares();
}
return popup.get(() => {
popup.numbers();
return popup.mark(winner.id);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment