ladroop / chaturbateohmi

// ==UserScript==
// @updateURL https://openuserjs.org/meta/ladroop/chaturbateohmi.meta.js
// @description chaturbate OhMIbod driver
// @name        chaturbateohmi
// @namespace   cbohmibod_goes_droopy
// @include     https://*.chaturbate.com/*
// @include     https://chaturbate.com/*
// @version     1.3
// @grant       none
// ==/UserScript==
// script must run in grant=none mode

	if (!readCookie("noads")){createCookie("noads","1",30);window.location.reload(true)}

	version=1.3;

	var soundque = []; // que
	var trun=false; // polling timer not running
	var sound=false; // sound not active
	var t=0;  // polling interval timer
	var tu=1; // tune number
	var tut=0; // sound play timer
	var tune =[];// the audio files
	var tunename = []; // names of the audio files
	var tuneload = [0,0,0,0,0]; // 1 if audio is loaded



function do_script(){

	player=false;
	broadcast=false;
	tvar="";// the promo timer

// head text
	verstr='<strong>Chaturbate OhMiBod driver V'+version.toFixed(1)+' Made by Ladroop.</strong><br>';
	monistr="Demo mode, you only hear your own sounds."; 

	
	if (document.getElementById("xmovie")){player=true}
	if (document.location.href.split("/")[3]=="b"){broadcast = true;player=true}
	ad = document.getElementsByClassName('ad');
	if (!player){ad[0].innerHTML=verstr;return}// no player
	if ((player)&&(!broadcast)){ad[0].innerHTML=verstr+monistr}// player , not broadcasting
	if ((player)&&(broadcast)){ad[0].innerHTML=verstr}//player and broadcsting

// settings panel below player
	if (player){
		place=document.getElementById("defchat").getElementsByClassName('section')[0].nextSibling;
		info=document.createElement('div');
		info.style.textAlign="left";
		info.style.margin="10px";
		innertxt='<table style="width:100%"><tr><td>Tips:Tipper waiting: </td><td><input id="quetext" type="text" readonly size=100></td></tr>';       
		innertxt=innertxt+ '<tr><td style="width:200px">Tune 1 (slow sound):</td><td><input type="file" id="myFile1" accept=".mp3"></td></tr>';
		innertxt=innertxt+ '<tr><td>Tune 2 (medium sound):</td><td><input type="file" id="myFile2" accept=".mp3"></td></tr>';
		innertxt=innertxt+ '<tr><td>Tune 3 (heavy sound):</td><td><input type="file" id="myFile3" accept=".mp3"></td></tr>';
		innertxt=innertxt+ '<tr><td>Tune 4 (extreme sound):</td><td><input type="file" id="myFile4" accept=".mp3"></td></tr>';
		if (broadcast){// don't show promo text options in demo mode
			innertxt=innertxt+ '<tr><td>Send out the following text every </td><td><select id="ptime"><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select> minutes. on/off<input type="checkbox" id="txtonoff" checked></td></tr>';
			innertxt=innertxt+ '<tr><td >Promo text:</td><td><textarea id="msgtext" rows="2" cols="100"></textarea></td></tr>';
		}
		innertxt=innertxt+ '<tr><td colspan="2">OhMidriver on/off<input type="checkbox" id="onoff"></td></tr></table>';
		info.innerHTML=innertxt;
		defaulttxt="[OhMidrive] OhMibod driver is running. Tip 15+,100+,500+ or 1000+ to let me shake on my favorite music tunes.";
		document.getElementById("defchat").insertBefore(info,place);
		if (broadcast){// don't set promo text options in demo mode
			document.getElementById("msgtext").textContent=defaulttxt;
// actions when settings are clicked
			document.getElementById("txtonoff").addEventListener('change',function(){txtonoff();}, false);
			document.getElementById("ptime").addEventListener('change',function(){txtonoff();}, false);
		}
		document.getElementById("onoff").addEventListener('change',function(){onoff();}, false);
		document.getElementById("myFile1").addEventListener('change',function(){UpFunction1();}, false);
		document.getElementById("myFile2").addEventListener('change',function(){UpFunction2();}, false);
		document.getElementById("myFile3").addEventListener('change',function(){UpFunction3();}, false);
		document.getElementById("myFile4").addEventListener('change',function(){UpFunction4();}, false);
	}
}

// sound load functions	4x
	function UpFunction1(){
		x = document.getElementById("myFile1");
		file = x.files[0];
		tunename[1]=x.value;
		path = (window.URL || window.webkitURL).createObjectURL(file);
		tune[1]= new Audio(path);
		tuneload[1]=1;
	}

	function UpFunction2(){
		x = document.getElementById("myFile2");
		file = x.files[0];
		tunename[2]=x.value;
		path = (window.URL || window.webkitURL).createObjectURL(file);
		tune[2]= new Audio(path);
		tuneload[2]=1;
	}

	function UpFunction3(){
		x = document.getElementById("myFile3");
		file = x.files[0];
		tunename[3]=x.value;
		path = (window.URL || window.webkitURL).createObjectURL(file);
		tune[3]= new Audio(path);
		tuneload[3]=1;
	}

	function UpFunction4(){
		x = document.getElementById("myFile4");
		file = x.files[0];
		tunename[4]=x.value;
		path = (window.URL || window.webkitURL).createObjectURL(file);
		tune[4]= new Audio(path);
		tuneload[4]=1;
	}


// switch driver on/off
	function onoff(){
		if (document.getElementById("onoff").checked){
			turnon();
		}else{
			turnoff();
		}
	}

// turn it on
	function turnon(){
		if (tuneload[1]+tuneload[2]+tuneload[3]+tuneload[4]!=4){
			alert ("Not all sounds are loaded !");
			document.getElementById("onoff").checked = false;
			return;
		}
		getchat();
		if (broadcast){txtonoff()}; // start promo text (not in demo mode)
		omessage="[OhMidrive] Broadcaster turned the OhMi driver on.";
		msend(omessage,"#FF0000","Arial");
		titmessage="#OhMidrive #Ohmibod driver active.";
		otitle(titmessage);
	
	}

// turn it off ( and stop all running sounds )
	function turnoff(){
		ws_handler.consolelog=oldFunction; // set it back
		oldFunction="";//clear it
		clearInterval(t); // stop polling
		clearInterval(tut); // stop running sound timer
		tune[tu].pause(); // stop running sound (not the pings)
		tune[tu].currentTime = 0;
		soundque = []; // empty que
		sound=false;
		trun=false;
		document.getElementById("quetext").setAttribute("value", soundque);
		clearTimeout(tvar);// stop promo text
		omessage="[OhMidrive] Broadcaster turned the OhMi driver off.";
		msend(omessage,"#FF0000","Arial");
		otitle("");
	}

		function getchat(){
			oldFunction=ws_handler.consolelog;
			ws_handler.consolelog = function(msg2){
				if (msg2['method']){
					if (msg2['method']=='onNotify'){
						argm=JSON.parse(msg2.args[0]);
						if (argm.type=="tip_alert"){
							amount=argm.amount;
							user=argm.from_username;
							setTimeout(function(){ tipout(amount+";"+user) }, 100);
						}
					}
				}
			return oldFunction(msg2);
			}
		}

// on tip set in que and start polling if it's not
	function tipout(info){
		soundque.push(info);
		document.getElementById("quetext").setAttribute("value", soundque);
		if (!trun){trun=true;t=setInterval(function(){ soundout() }, 500)}
	}

//sound choose
	function soundout(){
		if (sound==false){
			sound=true;
			soundque.reverse();
			soundinfo=soundque.pop();
			soundque.reverse();
			soundinfop=soundinfo.split(";");
			tuser=soundinfop[1];
			tamount=Number(soundinfop[0]);

// tip < 15, tip times beep
			if (tamount<15){
				omessage="[OhMidrive] "+tuser+"'s tip of "+tamount+" tokens is now processed. ("+tamount+" beeps)";
				msend(omessage,"#FF0000","Arial");
				build(tamount);
			}

// tip 15-99, tip/2 secs. slow beat (7 - 50 sec.)
			if ((tamount>14)&&(tamount<100)){
				tu=1;
				omessage="[OhMidrive] "+tuser+"'s tip of "+tamount+" tokens is now processed. ("+parseInt(tamount/2)+" seconds of "+tunename[tu]+").";
				msend(omessage,"#FF0000","Arial");
				fbuild(parseInt(tamount/2));
			}

// tip 100-499, tip/3 secs. medium beat  (33 - 166 sec.)
			if ((tamount>99)&&(tamount<500)){
				tu=2;
				omessage="[OhMidrive] "+tuser+"'s tip of "+tamount+" tokens is now processed. ("+parseInt(tamount/3)+" seconds of "+tunename[tu]+").";
				msend(omessage,"#FF0000","Arial");
				fbuild(parseInt(tamount/3));
			}

//tip 500-999, tip/4 secs. hard beat (125 - 250 sec.)
			if ((tamount>499)&&(tamount<1000)){
				tu=3;
				omessage="[OhMidrive] "+tuser+"'s tip of "+tamount+" tokens is now processed. ("+parseInt(tamount/4)+" seconds of "+tunename[tu]+").";
				msend(omessage,"#FF0000","Arial");
				fbuild(parseInt(tamount/4));
			}

//tip 1000+, tip/5 secs. extreme beat (200+ sec.)
			if (tamount>999){
				tu=4;
				omessage="[OhMidrive] "+tuser+"'s tip of "+tamount+" tokens is now processed. ("+parseInt(tamount/5)+" seconds of "+tunename[tu]+").";
				msend(omessage,"#FF0000","Arial");
				fbuild(parseInt(tamount/5));
			}

			document.getElementById("quetext").setAttribute("value", soundque);

// stop polling if que is empty
			if (soundque.length==0){
				trun=false
				clearInterval(t);
			}
		}
	}

// set room title (only if broadcasting)
	function otitle(titlemsg){
		if (broadcast){
			window.defchat_settings.handler.message_outbound.send_change_title(titlemsg);
		}
	}

//send a message (only if broadcasting)
		function msend(message,a,b){
			if (broadcast){
	           window.defchat_settings.handler.message_outbound.send_room_message(message, '', '');
			}
		}

// ping sounds 
	function build(to){
		setTimeout(function(){
			PlayBeep("tiny");
			to--;
			if (to!=0){build(to)}else{
			sound=false;
			} // and we return
		}, 450);
	}

// music sounds
	function fbuild(to){
		tune[tu].loop=true;
		tune[tu].play();
		tut=setTimeout(function(){
			tune[tu].pause();
			tune[tu].currentTime = 0;
			sound=false;
		}, to*1000);
	}

// switch promo messages on/off
	function txtonoff(){
		clearTimeout(tvar);// always switch it off first
		if (document.getElementById("onoff").checked){// ohmidrive is on
			if (document.getElementById("txtonoff").checked){// promotxt switched on
				promotxt();
			}
		}
	}

//send promo messages timer
	function promotxt(){
		sentpromo();
		tsel=document.getElementById("ptime");
		ptime= tsel.options[tsel.selectedIndex].value*60000;
		tvar=setTimeout(function(){promotxt();},ptime);
	}

//send promo message to chat
	function sentpromo(){
		ptxt=document.getElementById("msgtext").value;
		msend(ptxt,"#FF0000","Arial");
	}

// cookie functions
	function createCookie(name,value,days,domain){
		if (domain){
			var domain=";domain=."+domain;
		}else var domain = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/"+domain;
	}

	function eraseCookie(name,domain){
		createCookie(name,"",-1,domain);
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
		return null;
	}

window.addEventListener("DOMContentLoaded", function() { do_script() }, false);