Sketchful Auto Join

Right-click on a full lobby to auto-refresh and join when there's a spot available.

目前為 2020-06-28 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Sketchful Auto Join
// @namespace     https://greasyfork.org/users/281093
// @match         https://sketchful.io/*
// @grant         none
// @version       1.1
// @author        Bell
// @description   Right-click on a full lobby to auto-refresh and join when there's a spot available.
// jshint esversion: 6
// ==/UserScript==

const refreshInterval = 500; // Refresh interval, in milliseconds

(function requestPermission() {
    if (Notification.permission === "granted") {
        console.log("Notifications allowed");
    } else {
        Notification.requestPermission()
            .then(result => {
                console.log(result);
            });
    }
})();

let tabFocused = true;

window.onfocus = () => {
    tabFocused = true;
};

window.onblur = () => {
    tabFocused = false;
};

const lobbiesTable = document.querySelector("#menuLobbiesTable");
const menuNav = document.querySelector("#menu > div.menuNav > ul");
let refreshing = false;

const config = {
    attributes: false,
    childList: true,
    subtree: true
};

const onLobbyRefresh = function(mutationsList, observer) {
    for (let mutation of mutationsList) {
        let newNode = mutation.addedNodes[0];
        if (newNode.classList.contains("table")) {
            updateLobbies(newNode.querySelectorAll(".menuLobbiesRoom"));
        }
    }
};

const lobbyObserver = new MutationObserver(onLobbyRefresh);

lobbyObserver.observe(lobbiesTable, config);

lobbiesTable.addEventListener("contextmenu", setActiveLobby, false);
lobbiesTable.addEventListener("click", clearActiveLobby, false);
menuNav.addEventListener("click", clearActiveLobby, true);

let activeLobby = null;

function setActiveLobby(e) {
    e.preventDefault();
    let lobby = e.target.parentNode;
    try {
        const hash = getLobbyHash(lobby);
        if (!activeLobby && isLobbyFull(lobby)) {
            activeLobby = hash;
            refreshLobbies();
        } else {
            clearActiveLobby();
        }
    } catch (error) {
        clearActiveLobby();
    }
}

function clearActiveLobby() {
    if (activeLobby) {
        activeLobby = null;
        window.clearTimeout(this.refreshingID);
        refreshLobbies();
    }
}

function refreshLobbies() {
    document.querySelector("#menuLobbiesRefresh").click();
}

function isLobbyFull(lobby) {
    const capacity = getLobbyCapacity(lobby);
    return capacity.current >= capacity.max;
}

function getLobbyCapacity(lobby) {
    const capacityRegexp = /<td>(\d+)\/(\d+)<\/td>/;
    const capacityMatch = lobby.innerHTML.match(capacityRegexp);

    return {
        current: parseInt(capacityMatch[1]),
        max: parseInt(capacityMatch[2])
    };
}

function getLobbyHash(lobby) {
    const hashRegexp = /lobbyConnect\([^#]+(#[\w]+)/;
    return lobby.outerHTML.match(hashRegexp)[1];
}

function notify() {
    if (Notification.permission === "granted") {
        let notification = new Notification("Joined", {
            icon: "https://sketchful.io/res/logo/pencils%20optimized.png",
            body: "Click the notification to return to the game.",
            requireInteraction: true,
        });

        notification.onclick = function() {
            window.focus();
            notification.close();
        };
    } else {
        console.log("Notifications are blocked.");
    }
}

function tryToJoin(lobby) {
    if (isLobbyFull(lobby)) {
        console.log(activeLobby + " is full, refreshing");
        console.log(getLobbyCapacity(lobby));
        this.refreshingID = window.setTimeout(refreshLobbies, refreshInterval);
    } else {
        console.log("Joining " + activeLobby);
        lobbyConnect(activeLobby);
        if (!tabFocused) {
            notify();
        }
        clearActiveLobby();
    }
}

function updateLobbies(lobbies) {
    if (activeLobby) {
        for (let lobby of lobbies) {
            if (getLobbyHash(lobby) === activeLobby) {
                lobby.style.backgroundColor = "pink";
                tryToJoin(lobby);
            }
        }
    } else {
        console.log("No active lobby");
    }
}