Last active
June 20, 2017 11:10
-
-
Save tarekis/704836f8b3dd02d1e8796e6d887876cd to your computer and use it in GitHub Desktop.
Spams in any SE or SO chatroom.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name SE Chat Spammer | |
// @namespace http://chat.meta.stackexchange.com | |
// @version 1.0 | |
// @description Spam dem Boys! | |
// @author Clemens Himmer | |
// @include *://chat.meta.stackexchange.com/rooms/* | |
// @include *://chat.stackoverflow.com/rooms* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var fKey = fkey().fkey; | |
var splitHref = location.href.split("rooms/"); | |
var messagePostURL = splitHref[0] + "chats/" + parseInt(splitHref[1])+"/messages/new"; | |
function spamMessage(textSpawner,iterationAmount){ | |
function sendMessage(initialCount, countLeft){ | |
if(countLeft === undefined){ | |
countLeft = initialCount-1; | |
} | |
var text = textSpawner(initialCount, countLeft); | |
var r = new XMLHttpRequest(); | |
r.open("POST", messagePostURL, true); | |
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
r.onreadystatechange = function () { | |
if (r.readyState != 4 ) return; | |
if(countLeft > 0 || (countLeft === 0 && r.status !== 200)){ | |
var checkString = "You can perform this action again in"; | |
if(r.status !== 200){ | |
var timeOutMs = 1000 * (parseInt(r.responseText.split(checkString)[1])+1); | |
setTimeout( | |
function(){ | |
sendMessage(initialCount,countLeft); | |
},timeOutMs); | |
} | |
else{ | |
sendMessage(initialCount,countLeft-1); | |
} | |
} | |
}; | |
r.send("fkey="+fKey+"&text="+encodeURIComponent(text)); | |
} | |
sendMessage(iterationAmount); | |
} | |
var handleSpamSubmit = function() { | |
var inputElement = document.getElementById("input"); | |
var inputText = inputElement.value; | |
var numberElement = document.getElementById("spam-amount"); | |
var spamAmount = Number(numberElement.value); | |
inputElement.value = ""; | |
numberElement.value = ""; | |
var textSpawner = function(initialCount, countLeft){ | |
return inputText.replace("currentAmount",initialCount-countLeft).replace("totalAmount",initialCount).replace("leftAmount",countLeft); | |
}; | |
if(!isNaN(spamAmount) && spamAmount > 0){ | |
spamMessage(textSpawner,spamAmount); | |
console.info("Started to spam message with "+spamAmount+" iterations"); | |
} | |
else{ | |
throw new Error("You messed up the amount input, dude!"); | |
} | |
}; | |
// Initiate spam UI | |
function insertAfter(newNode, referenceNode) { | |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); | |
} | |
// Add CSS | |
var sheet = (function() { | |
var style = document.createElement("style"); | |
style.appendChild(document.createTextNode("")); | |
document.head.appendChild(style); | |
return style.sheet; | |
})(); | |
var isSO = location.host.indexOf("stackoverflow") > -1; | |
sheet.addRule("#spam-button", "border-bottom-left-radius: 0;border-top-left-radius: 0;", 0); | |
sheet.addRule(".tooltip", "display:inline;", 0); | |
sheet.addRule(".tooltip .tooltiptext", "display:none;width:auto;background-color:#000;color:#fff;text-align:center;border-radius:3px;padding:5px;position:absolute;z-index:1; margin-left: 10px; margin-top: 5px;font-size: 9px;line-height: 14px;height: 14px;", 0); | |
sheet.addRule(".tooltip:hover .tooltiptext", "display:inline",0); | |
sheet.addRule("#spam-amount", "margin-left: 5px;width: 30px;padding: 0;border: 0;" + (isSO? "border-radius: 10px 0 0 10px;height: 17px;": "border-radius: 3px 0 0 3px;box-shadow: 0px 2px 0 #086586;height: 21px;"),0); | |
// Add HTML | |
var input = document.createElement("input"); | |
input.id = "spam-amount"; | |
input.type= "number"; | |
var hackyButton = document.createElement("id"); | |
hackyButton.id = "spam-button"; | |
hackyButton.classList = "button"; | |
var buttonText = document.createTextNode("spam"); | |
hackyButton.appendChild(buttonText); | |
var tooltip = document.createElement("div"); | |
tooltip.classList = "tooltip"; | |
var tooltipB = document.createElement("b"); | |
var boldText = document.createTextNode(" ?"); | |
tooltipB.appendChild(boldText); | |
tooltip.appendChild(tooltipB); | |
var span = document.createElement("span"); | |
span.classList = "tooltiptext"; | |
var spanText = document.createTextNode("currentAmount, totalAmount and leftAmount will be replaced accordingly."); | |
span.appendChild(spanText); | |
tooltip.appendChild(span); | |
hackyButton.appendChild(tooltip); | |
insertAfter(input,document.getElementById("sayit-button")); | |
insertAfter(hackyButton,document.getElementById("spam-amount")); | |
// Bind listeners | |
document.getElementById("spam-button").addEventListener("click", handleSpamSubmit); | |
document.getElementById("spam-amount").addEventListener("keypress", function(e) { | |
if (e.keyCode === 13) { | |
handleSpamSubmit(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment