MooMoo.io Show FPS

Display FPS

当前为 2023-11-02 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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()
})()