doubtingreality / Twitch Auto Messager

// ==UserScript==
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAJcUlEQVR42tVZa2xUxxWe2Ye9T5u18QOMCQECIiC5RGCweRNMeIRSrSltVQklpf+itjRq1ZZKKarUtH8aJamq5k8pKSj4R6E0sqU4caG83w+BA40xEGObIvBz7+56X/dOvzN3n95dvHZMgIHxPHbu3POd882ZM3M5e0rSn29WVDPGb7/xQlfPaJ7jT1pwSn9qq3ipwFx4gTPuUcKe34VD4v0d87qCzwyA97+oqOPM0JxnyGMFeROYL6K0D0WGfvaj2Z0fPxMA3r0xpY5z/qneEsJhKuAWk5UNBvs/DWuRN38y5+7njxMA/8FvFrKXvus1h4XfhrYV2fboLGJ1O7IReRryluh8guYkwSbkFwvQKjIQ6v9AVbVdb87r6BtXAG///m16nr9QbWP8+UsFQTFQjveXIZcilwimlaAsZkwrQt2FcgJyoWBqAUqnEBEHficATAkPsqAaSHuHkZuYK7+YhbRgrxLy7Ar4tQ92VneoKQBOnDhhsVqtO/Ly8upNJlMp57nhElJZei3AenlIDBrCTDGEhQ+l1xBCDguvMayXhmGl7NdYhMe0nmyB4XWL0SqwPjhAtAYiQz/9edXtFgkAwtscDsdnLperxmAwcBIeOduEyW1ZRkEIVQQAQGFBzSNQcpQsKDwCJfo9sp/qwWgdYOUYlYV1TfDoO0ao0yLPN1rEQKD3X2FN3cEvXLiwq7i4+C0hBDMajQwYcmaWLrxuBVUEITABUGBuhUUFZVHhZX96XxTAKJMB/1yWicwTGjjFr169+l+bzTbbbDYLgy49/IAQPMqj5Hpym8qYdqgOAJwAQHhoWolqWtG1Lq2RVKd+oddVEc5KmxHqPkj1YwLgsdvtTgKQSehRAdCUqHCKrAckbZS40AEJQInSSeFoM230ADTk/Wj+8rc1bV38ypUritPptJvNppypk0ohPUW0AATzMl1wnUpREEl9HhZQlZR21AI5pWLTDFbMZr/x2uI//iXWFwPggPcZswUISURawEcgREjzch2MF5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cAlN57791yhCIV9DVI5zwfgu4f9vX1de/c+evQeAo8PP0fduAaS+u3PUkAAAAASUVORK5CYII=
// @name         Twitch Auto Messager
// @namespace    https://github.com/doubtingreality/twitch-auto-messager
// @version      0.7
// @description  Adds a new panel on Twitch.tv to easily automate sending of messages.
// @author       Murtada al Mousawy
// @match        https://www.twitch.tv/*
// @grant        none
// ==/UserScript==

(function() {
	function init() {
		var automessager_style = document.createElement("style");
		automessager_style.appendChild(document.createTextNode(".auto-automessager-holder.minimized { min-height: 4rem !important; height: 4rem !important; max-height: 4rem !important; width: 4.8rem; } .auto-automessager-holder.minimized * { display: none; } .auto-automessager-holder.minimized .automessager-min-button { display: block !important; }"));
		document.body.appendChild(automessager_style);

		var automessager_holder = document.createElement("div");
		automessager_holder.classList.add('auto-automessager-holder');
		automessager_holder.classList.add('minimized');
		automessager_holder.setAttribute("style", "position: fixed;bottom: 0; left: 1rem; overflow: hidden; min-height: 21rem;z-index: 999;background: #282828;padding: 1rem 1.5rem 1.5rem 1.5rem;");
		automessager_holder.innerHTML = "<div class='automessager-close-button' style='float: right; margin-left: 1rem; cursor: pointer; width: 2rem; border: 1px solid #444; text-align: center;'>&#10060;</div><div class='automessager-min-button' style='float: right; cursor: pointer; width: 2rem; border: 1px solid #444; text-align: center;'>&#95;</div><br><textarea class='automessager-input' style='margin: 0.5rem 0 1.5rem 0; background-color: rgb(20,20,20);color: #D3D3D3;border: solid 1px #333;' placeholder='Enter your message'></textarea><br><input value='5' onkeypress='return event.charCode >= 48 && event.charCode <= 57' type='text' style='padding: 0.5rem; width: 120px;background-color: rgb(20,20,20);color: #D3D3D3;border: solid 1px #333;' placeholder='Interval (seconds)' class='automessager-interval-input'><button class='button primary float-right stop-automessager-button' disabled>Stop</button><button class='button primary float-right start-automessager-button' style='margin-right: 1rem;'>Start</button>";
		document.body.appendChild(automessager_holder);

		var evenodd = 0;
		    chat_message = "",
		    automessager_interval = null,
		    $automessager_chat_input         = $('.chat_text_input'),
		    $automessager_chat_button        = $('button.qa-chat-buttons__submit'),
		    $automessager_holder             = $('.auto-automessager-holder'),
		    $automessager_automessager_input = $('.automessager-input'),
		    $automessager_start_button       = $('.start-automessager-button'),
		    $automessager_stop_button        = $('.stop-automessager-button'),
		    $automessager_interval_input     = $('.automessager-interval-input'),
		    $automessager_min_button         = $('.automessager-min-button'),
		    $automessager_close_button       = $('.automessager-close-button');

		if ($("#bttv-channel-state-contain .slow-time").length > 0) {
			console.log("test");
			$automessager_interval_input.val($("#bttv-channel-state-contain .slow-time")[0].textContent);
		}

		$automessager_min_button.click(function() { $automessager_holder.toggleClass('minimized') });
		$automessager_close_button.click(function() { stop_automessager(); $automessager_holder.remove(); });

		$automessager_start_button.click(function() { start_automessager(); });
		$automessager_stop_button.click(function() { stop_automessager(); });

		function start_automessager() {
			if ($('.chat-container').length > 0) {
				chat_message = $automessager_automessager_input.val();
				$automessager_start_button.attr("disabled", "disabled");
				$automessager_automessager_input.attr("disabled", "disabled");
				$automessager_interval_input.attr("disabled", "disabled");
				$automessager_stop_button.removeAttr("disabled");

				automessager_interval = setInterval(function() {
					if (evenodd === 0) {
						evenodd = -1;
						$automessager_chat_input.val(chat_message);
					} else {
						evenodd = 0;
						$automessager_chat_input.val(chat_message+" .");
					}
					$automessager_chat_button.click();

				}, $automessager_interval_input.val()*1000);
			}
		}

		function stop_automessager() {
			$automessager_start_button.removeAttr("disabled");
			$automessager_automessager_input.removeAttr("disabled", "disabled");
			$automessager_interval_input.removeAttr("disabled", "disabled");
			$automessager_stop_button.attr("disabled", "disabled");
			clearInterval(automessager_interval);
		}
	}

	var init_interval = setInterval(function() {
		if ($(".chat-room").length > 0 && $(".auto-automessager-holder").length < 1) {
			init();
			clearInterval(init_interval);
		}
	}, 500);
})();