Bonk.io Custom Mods

Some custom enhancements to bonk.io

目前为 2022-03-17 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bonk.io Custom Mods
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Some custom enhancements to bonk.io
// @author       You
// @match        https://bonk.io/
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
// @grant        none
// ==/UserScript==

/* globals $ */
/* jshint esversion:6 */

(function () {
    'use strict';

    document.getElementById('maingameframe').contentWindow.Event.prototype.preventDefault = () => {};

    window.addEventListener('load', () => {
        let frame = document.getElementById('maingameframe');
        let frameDoc = document.getElementById('maingameframe').contentDocument;

        let setup;
        let observer = new MutationObserver((mutations, me) => {
            if (frameDoc.getElementById('roomlisttopbar')) {
                me.disconnect(); // stop observing
                setup();
                return;
            }
        });
        observer.observe(frameDoc, {
            childList: true,
            subtree: true
        });

        setup = () => {
            let style1 = `
            <style>
                body { overflow: hidden; }
                #bonkioheader { display: none; }
                #adboxverticalleftCurse { display: none; }
                #adboxverticalCurse { display: none; }
                #descriptioncontainer { display: none; }
                #maingameframe { margin: 0 !important; }
            </style>
            `;
            document.head.insertAdjacentHTML("beforeend", style1);
            console.log(document.head);

            let $ = frame.contentWindow.$;

            function filterRooms(s) {
                s = s.toLowerCase();
                let matches = el => el.children[0].textContent.toLowerCase().includes(s);
                $('#roomlisttable tr').each((i, el) => {
                    el.hidden = !matches(el);
                });
            }

            let inp = `<input type="text" id="roomSearchInputBox" placeholder="Search Rooms.." style="
                float: right;
                padding: 2px 8px;
                margin: 5px 20px;
                border: 2px solid #006157;
                border-radius: 5px;
                font: large futurept_b1;
                ">`;

            $('#roomlisttopbar').append(inp);

            function debounce (fn) {

                // Setup a timer
                let timeout;

                // Return a function to run debounced
                return function () {

                    // Setup the arguments
                    let context = this;
                    let args = arguments;

                    // If there's a timer, cancel it
                    if (timeout) {
                        window.cancelAnimationFrame(timeout);
                    }

                    // Setup the new requestAnimationFrame()
                    timeout = window.requestAnimationFrame(function () {
                        fn.apply(context, args);
                    });

                };

            }

            $('#roomSearchInputBox').keyup(debounce(ev => filterRooms(ev.target.value)));

            $('body').keydown(debounce(ev => {
                if (ev.altKey) {
                    if (ev.key === 'q' && $('#roomListContainer')[0].offsetParent === null) {
                        $('#pretty_top_exit').click(); $('#leaveconfirmwindow_okbutton').click();
                    }
                    else if (ev.key === 'r') {$('#roomlistrefreshbutton').click();}
                }
            }));

    /****** Implemented by Chaz *******

            // Can double click room to trigger join
            let room_join_dblclick_handler = (ev) => $("#roomlistjoinbutton").click();
            $("#roomlisttable").on("dblclick", "tr", room_join_dblclick_handler);

            // Press enter to submit password for joining room
            let room_pass_enter_handler = (ev) => {
                ev.key === "Enter" && $("#roomlistpassjoinbutton").click();
            };
            $("#roomlistjoinpasswordtext").on("keydown", room_pass_enter_handler);
   *******/
        };
    });
})();