Display FPS
目前為
// ==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()
})()