// ==UserScript==
// @name 9gag tagger
// @namespace http://9gag.com
// @include http://9gag.com/*
// @version 0.1
// @description Tag all the people!
// @author flufflz
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var savedSettings = getSavedValues("tagList");
// If no delete button exists it's not our post. Maybe we should allow it for other posts though?
if(document.getElementsByClassName("badge-item-delete hide")[0])
return;
var oPostButton, newElement, oPopup, aNames, oTextBox, oSubmitButton, iCommentsPresent, iCommentsPosted, iFailedAttempts, sNextNames;
oPopup = ' \
\
';
oPostButton = document.getElementsByClassName("cmnt-btn size-30 submit-comment badge-post-trigger")[0];
if (oPostButton) {
newElement = oPostButton.cloneNode(true);
newElement.id = "tag_button";
newElement.text = "Tag";
newElement.onclick = function()
{
if(document.getElementById("gmPopupContainer"))
{
$("#gmPopupContainer").show();
return;
}
$("body").append ( oPopup );
if(savedSettings)
document.getElementById("gmTextBox").value = savedSettings;
//--- Use jQuery to activate the dialog buttons.
$("#gmStartTagBtn").click ( function () {
$("#gmPopupContainer").hide();
var sNames = document.getElementById("gmTextBox").value.replace(/ +(?= )/g,'');
saveValues(sNames);
setTimeout(function() {
startTagging(sNames);
}, 500);
} );
$("#gmCloseDlgBtn").click ( function () {
$("#gmPopupContainer").hide ();
} );
};
oPostButton.parentNode.insertBefore(newElement, oPostButton.nextSibling);
} else return;
function startTagging(sNames) {
// Actual name array. Replace my name with actual names and fill it as much as you like.
aNames = [];
aNames = sNames.split(" ");
aNames = shuffle(aNames);
// You don't need to touch anything down there. Just let it do it's magic.
oTextBox = document.getElementsByClassName("post-text-area badge-post-textarea focus");
oSubmitButton = document.getElementsByClassName("cmnt-btn size-30 submit-comment badge-post-trigger");
iCommentsPresent = document.getElementsByClassName("comment-entry badge-comment").length;
iCommentsPosted = 0;
iFailedAttempts = 0;
createNewPost();
}
// We might trigger a spam filter if we use the same pattern every single time
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function createNewPost() {
if(aNames.length > 0)
{
sNextNames = "@";
for(var i = 0; i <= 2; ++i)
{
sNextNames = (sNextNames + aNames.pop());
if(aNames.length === 0 || i === 2)
break;
sNextNames = (sNextNames + "\n" + " @");
}
oTextBox[0].value = sNextNames;
setTimeout(function() {
oSubmitButton[0].click();
}, 100);
++iCommentsPosted;
setTimeout(function() {
checkComments();
}, 500);
}
}
function waitSomeMore() {
setTimeout(function() {
checkComments();
}, 500);
}
function checkComments() {
setTimeout(function() {
if(iFailedAttempts > 20)
throw new Error("Waited too long for new comment to appear. We most likely lost track of it for some reason.");
if(document.getElementsByClassName("comment-entry badge-comment").length === (iCommentsPresent + iCommentsPosted))
{
iFailedAttempts = 0;
createNewPost();
} else {
++iFailedAttempts;
waitSomeMore();
}
}, 200);
}
function getSavedValues() {
return GM_getValue("tagList");
}
function saveValues(values) {
return GM_setValue("tagList", values);
}
GM_addStyle ( " \
#gmPopupContainer { \
position: fixed; \
top: 30%; \
left: 20%; \
padding: 2em; \
background: powderblue; \
border: 3px double black; \
border-radius: 1ex; \
z-index: 777; \
} \
#gmPopupContainer button{ \
cursor: pointer; \
margin: 1em 1em 0; \
border: 1px outset buttonface; \
} \
" );
})();