MPP auto-refresh connection

small userscript that refreshes the page automatically if connecting to mpp takes too long

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MPP auto-refresh connection
// @version      1.0.0 (beta test)
// @description  small userscript that refreshes the page automatically if connecting to mpp takes too long
// @author       ccjt
// @match        *://*/*
// @license      MIT
// @namespace    https://greasyfork.org/users/1459137
// ==/UserScript==

let refreshTimeout = 5e3 // adjust as needed, in milliseconds (5e3 = 5000)
if (typeof MPP !== 'undefined' && typeof MPP === 'object') {
    let connected
    let connectionOpened = null
    MPP.client.ws.onopen = () => {
        // .startsWith because of url formatting quirkiness
        if (!MPP.client.ws.url.startsWith('wss://mppclone.com')) return
        connectionOpened = Date.now()

        function connectedCheck() {
            if (Date.now() - connectionOpened > refreshTimeout && !MPP.client.isConnected()) {
                let failedDiv = document.createElement('div')
                failedDiv.style.cssText = `
                width: 100vw;
                height: 100vh;
                position: fixed;
                top: 0;
                left: 0;
                background-color: #000a;
                backdrop-filter: saturate(150%) blur(10px);
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                gap: 20px;
                opacity: 0%;
                transition: opacity .5s ease;
                `
                document.body.appendChild(failedDiv)
                requestAnimationFrame(()=>{
                    let i = 2
                    let failedText = document.createElement('a')
                    failedText.textContent = `connection timed out (${refreshTimeout / 1e3}s), refreshing in ${i}s`
                    failedDiv.appendChild(failedText)
                    failedDiv.style.opacity = '100%'
                    setInterval(()=>{
                        i--
                        failedText.textContent = `connection failed, refreshing in ${i}s`
                        if (i <= 0) window.location.reload()
                    }, 1e3)
                })
                return
            }
            requestAnimationFrame(connectedCheck())
        }
        connectedCheck()
    }
}