Bonk.io Auto Colour Switching

Automatically rotate through bonk icon colours/teams

Versión del día 25/11/2022. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name			Bonk.io Auto Colour Switching
// @author			Figurative Lag
// @description		Automatically rotate through bonk icon colours/teams
// @match			https://bonk.io/*
// @version			1.4
// @namespace		https://github.com/michaelskyba
// @run-at			document-idle
// @grant			none
// @license			Apache 2.0
// ==/UserScript==

// Index 0 is actually supposed to be Spectate, which we're not going to use
// So, we have to add 1 to our index when submitting data to the web socket
const coloursList = [
	"FFA",
	"Red",
	"Blue",
	"Green",
	"Yellow"
]
const enabled = [
	true,
	true,
	true,
	true,
	true
]
let currentColour = 0

const RNG = (min, max) => {
	return Math.round(Math.random() * (max - min)) + min
}

function getFrame() {
	return document.getElementById("maingameframe").contentWindow.document
}

function getId(id) {
	let frame = getFrame()
	return frame.getElementById(id)
}

function setColour(colour) {
	currentColour = colour

	// We have to add one because we removed "spectate" from index 0
	SEND('42[6,{"targetTeam":' + (colour+1) + '}]')
}

let menu = document.getElementById("descriptioninner")
menu.style.cssText = "background-color: black !important;"

// Clear old screen
while (menu.children.length > 0) {
	menu.children[0].remove()
}

function createCheckbox(colour, i) {
	let id = `${colour}Checkbox`
	let label = document.createElement("label")
	label.innerHTML = colour
	label.htmlFor = id
	menu.appendChild(label)

	let checkbox = document.createElement("input")
	checkbox.type = "checkbox"
	checkbox.id = id
	checkbox.checked = true

	checkbox.onchange = function() {
		enabled[i] = this.checked
	}

	menu.appendChild(checkbox)
	menu.appendChild(document.createElement("br"))
}

let h3 = document.createElement("h3")
h3.innerHTML = "Automatic Colour (Team) Switching"
h3.style.margin = 0
menu.appendChild(h3)

let p = document.createElement("p")
p.innerHTML = "Made by Figurative Lag"
p.style.margin = 0
menu.appendChild(p)

menu.appendChild(document.createElement("hr"))

coloursList.forEach((colour, i) => {
	createCheckbox(colour, i)
})

function timeout() {
	listChecked = []
	for (let i = 0; i < enabled.length; i++) {
		if (enabled[i])
			listChecked.push(i);
	}

	if (listChecked.length == 0)
		return
	if (listChecked.length == 1 && currentColour == listChecked[0])
		return

	// Don't set the colour that is already set
	let colour = currentColour
	while (colour == currentColour) {
		colour = listChecked[RNG(0, listChecked.length-1)]
	}

	setColour(colour)
	console.log("setting", colour)
}
let interval = setInterval(timeout, 1000)