Digdig.IO Server Selector

try to take over the world!

Tính đến 06-07-2021. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Digdig.IO Server Selector
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  try to take over the world!
// @author       Zertalious (Zert)
// @match        *://digdig.io/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        unsafeWindow
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

GM_addStyle(`
.container {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    pointer-events: none;
    user-select: none;
}

.active {
    pointer-events: auto;
}

.btn,
.modalUI {
    background: #aeaeae;
    color: #fff;
    font-size: 14px;
    font-family: Ubuntu;
    border: 4px solid #8f8f8f;
    border-radius: 6px;
    pointer-events: auto;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}

.btn {
    position: absolute;
    right: 10px;
    bottom: 10px;
    padding: 5px 15px;
    cursor: pointer;
    outline: 0;
    transition: 0.2s;
}

.btn-active {
    transform: translate(0, 0);
}

.container:not(.active) .btn:hover {
    background: #c7c7c7;
}

.active .btn {
    background: #c7c7c7;
}

.modalUI {
    position: absolute;
    bottom: 55px;
    right: 6px;
    padding: 10px;
    width: 300px;
    max-height: 400px;
    transition: 0.2s;
    transform: translate(0, calc(100% + 55px));
    overflow: auto;
}

.active .modalUI {
    transform: translate(0, 0);
}

.modalUI-title {
    font-size: 1.6em;
    text-align: center;
    margin-top: 0;
}

.modalUI-close-btn {
    position: absolute;
    right: 5px;
    top: 7px;
    width: 24px;
    height: 24px;
    border-radius: 4px;
    background: #bb5555;
    border: 4px solid #974545;
    cursor: pointer;
    outline: 0;
}

.modalUI-close-btn:hover {
    background: #da6868;
}

.modalUI-close-btn:after,
.modalUI-close-btn:before {
    content: "";
    position: absolute;
    left: 51%;
    top: 52.5%;
    width: 3px;
    height: 100%;
    background: #cccccc;
    border-radius: 4px;
}

.modalUI-close-btn:after {
    transform: translate(-50%, -50%) rotate(45deg);
}

.modalUI-close-btn:before {
    transform: translate(-50%, -50%) rotate(-45deg);
}

.modalUI-element {
    background: rgba(0, 0, 0, 0.2);
    padding: 10px;
    border-radius: 5px;
    margin-bottom: 5px;
    cursor: pointer;
}

.modalUI-element:last-child {
    margin-bottom: 0;
}

.modalUI-element-active {
    box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.2);
}

.modalUI-loader {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 10px solid rgba(0, 0, 0, 0.5);
    border-top-color: rgba(0, 0, 0, 0.8);
    border-radius: 50%;
    animation: spin 0.5s infinite;
}

.spinner-text {
    margin-top: 5px;
    font-size: 1.25em;
}

.modalUI-info {
    position: absolute;
    right: 30px;
    top: 6px;
    font-size: 1.20em;
    padding: 4px 5px;
}

.modalUI-info:hover .modalUI-info-content {
    opacity: 1;
    pointer-events: inherit;
}

.modalUI-info-content {
    position: absolute;
    bottom: 170%;
    right: 0;
    background: rgba(0, 0, 0, 0.4);
    border-radius: 4px;
    opacity: 0;
    padding: 8px;
    font-size: 0.85rem;
    width: 450px;
    transition: 0.2s;
    pointer-events: none;
    display: flex;
    flex-direction: row;
}

.modalUI-info-title {
    margin: 0;
    margin-bottom: 5px;
    font-size: 1.40rem;
}

.version-label {
    font-size: 0.75em;
    text-align: right;
}

.divider {
    width: 3px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    margin: 0 10px;
}

.modalUI-info-content > div:not(.divider) {
    flex: 1;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}
`);

(async function() {
	const html = `
    <button class="btn">Servers</button>
    <div class="modalUI">
      <h1 class="modalUI-title">Servers</h1>
      <button class="modalUI-close-btn"></button>
      <div class="modalUI-element modalUI-loader">
        <div class="spinner"></div>
        <div class="spinner-text">Fetching...</div>
      </div>
    </div>
    `;
    const container = document.createElement("div");
    container.classList.add('container');
    container.innerHTML = html;
    document.body.appendChild(container);
    const modalUIEl = document.querySelector(".modalUI");
    const btnEl = document.querySelector(".btn");
    const closeBtnEl = document.querySelector(".modalUI-close-btn");
    const loaderEl = document.querySelector(".modalUI-loader");
    btnEl.onclick = () => {
        container.classList.toggle("active");
    }
    closeBtnEl.onclick = () => {
        closeModal();
    }
    const closeModal = () => {
        container.classList.remove("active");
    }
    window.addEventListener('click', (evt) => {
        if (evt.target === container) {
            closeModal();
        }
    });
    function removeActiveClass() {
        let activeEl = document.querySelector(".modalUI-element-active");
        if (activeEl) {
            activeEl.classList.remove("modalUI-element-active");
        }
    }

	const sockets = [];
	let gameSocket = null;
	const KEY = 'digdig-selected-server';
	unsafeWindow.WebSocket = new Proxy(unsafeWindow.WebSocket, {
		construct(target, args) {
			let isGameSocket;
			if (servers) {
				for (let key in servers) {
					if (args[0].indexOf(servers[key].ipv4) > -1) { // is a game socket
						// disconnect socket and connect to selected server
						isGameSocket = true;
						console.log('game server found!');
						if (args[0].indexOf(GM_getValue(KEY)) == -1) {
							let old = args[0];
							args[0] = args[0].split('//')[0] + '//' + GM_getValue(KEY) + ':443';
							console.log('Was connecting to ' + old + ', overriding to ' + GM_getValue(KEY) + '...');
						} else {
							GM_setValue(KEY, args[0].split('//')[1].split(':')[0]);
							console.log('No saved server found! Saving current server...');
							const el = document.querySelector('[data-server="' + GM_getValue(KEY) + '"]');
							if (el) {
								el.classList.add('modalUI-element-active');
							}
						}
						break;
					}
				}
			}
			const socket = Reflect.construct(...arguments);
			if (isGameSocket) {
				gameSocket = socket;
			}
			sockets.push(socket);
			return socket;
		}
	});
	const response = await fetch('https://api.n.m28.io/endpoint/digdig/findEach/');
	const json = await response.json();
	const servers = json.servers;
	for (let i = 0; i < sockets.length; i++) {
		const socket = sockets[i];
		for (let key in servers) {
			if (socket.url.indexOf(servers[key].ipv4) > -1) {
				gameSocket = socket;
				reconnect();
			}
		}
	}
	for (let key in servers) {
		const server = servers[key];
		let el = document.createElement("div");
        el.classList.add("modalUI-element");
        el.innerHTML = key;
        el.setAttribute('data-server', server.ipv4);
        el.onclick = function() {
            removeActiveClass();
            el.classList.add("modalUI-element-active");
            GM_setValue(KEY, server.ipv4);
            reconnect();
        }
        if (server.ipv4 == GM_getValue(KEY)) {
        	el.classList.add('modalUI-element-active');
        }
        modalUIEl.appendChild(el);
	}
	loaderEl.style.display = 'none';
	function reconnect() {
		while (sockets.length > 0) {
			try {
				sockets.shift().close();
			} catch (e) {}
		}
	}
})();