MooMoo.io Show FPS

Display FPS

目前為 2023-11-02 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MooMoo.io Show FPS
// @description  Display FPS
// @author       KOOKY WARRIOR
// @match        *://*.moomoo.io/*
// @icon         https://moomoo.io/img/favicon.png?v=1
// @require      https://cdnjs.cloudflare.com/ajax/libs/msgpack-lite/0.1.26/msgpack.min.js
// @require 	 https://greasyfork.org/scripts/478839-moomoo-io-packet-code/code/MooMooio%20Packet%20Code.js?version=1274028
// @run-at       document-start
// @grant        unsafeWindow
// @license      MIT
// @version      0.3
// @namespace    https://greasyfork.org/users/999838
// ==/UserScript==

;(() => {
	unsafeWindow.showFPS = true

	const element = document.createElement("div")
	element.id = "fps"
	element.style = `
        position: absolute;
        bottom: 20px;
        left: 50%;
        transform: translateX(-50%);
        color: white;
    `

	let inGame = false

	new Promise(async (resolve) => {
		let { send } = WebSocket.prototype

		WebSocket.prototype.send = function (...x) {
			send.apply(this, x)
			this.send = send
			this.addEventListener("message", (e) => {
				if (!e.origin.includes("moomoo.io") && unsafeWindow.privateServer) return
				const [packet, data] = msgpack.decode(new Uint8Array(e.data))
				switch (packet) {
					case OLDPACKETCODE.RECEIVE["1"]:
						inGame = true
						break
					case OLDPACKETCODE.RECEIVE["11"]:
						inGame = false
						update()
						break
				}
			})
			resolve(this)
		}
	})

	unsafeWindow.addEventListener("DOMContentLoaded", () => {
		document.getElementById("mainMenu").appendChild(element)
	})

	function update() {
		const updateDelay = 500
		let lastFpsUpdate = 0
		let frames = 0
		function updateFPS() {
			let now = Date.now()
			let elapsed = now - lastFpsUpdate
			if (elapsed < updateDelay) {
				++frames
			} else {
				element.innerText = `Fps: ${Math.round(frames / (elapsed / 1000))}`
				frames = 0
				lastFpsUpdate = now
			}
			if (!inGame) {
				unsafeWindow.requestAnimationFrame(updateFPS)
			}
		}
		lastFpsUpdate = Date.now()
		unsafeWindow.requestAnimationFrame(updateFPS)
	}
	update()
})()