Bloxd.io Pointer Lock + L Toggle

Keep the cursor frozen in-game (pointer lock) with an L toggle, normal cursor in menus.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bloxd.io Pointer Lock + L Toggle
// @namespace    https://greasyfork.org/users/your-name
// @version      1.0
// @description  Keep the cursor frozen in-game (pointer lock) with an L toggle, normal cursor in menus.
// @author       You
// @match        *://bloxd.io/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // CONFIG
  const TOGGLE_KEY = 'l'; // press "L" to toggle lock on/off

  let stickyLock = true;        // we want lock in-game by default
  let userToggleLock = true;    // user-controlled "allow lock" flag
  let lastRequestedCanvas = null;

  function getGameCanvas() {
    // Bloxd uses a big main canvas; grab the largest one.
    const canvases = Array.from(document.getElementsByTagName('canvas'));
    if (!canvases.length) return null;
    return canvases.reduce((biggest, c) => {
      const area = c.width * c.height;
      const biggestArea = biggest.width * biggest.height;
      return area > biggestArea ? c : biggest;
    });
  }

  function isPointerLocked() {
    return document.pointerLockElement != null;
  }

  function requestLock(canvas) {
    if (!canvas) return;
    lastRequestedCanvas = canvas;

    const req =
      canvas.requestPointerLock ||
      canvas.mozRequestPointerLock ||
      canvas.webkitRequestPointerLock;

    if (req) {
      try {
        req.call(canvas);
      } catch (e) {
        // ignore
      }
    }
  }

  function exitLock() {
    const exit =
      document.exitPointerLock ||
      document.mozExitPointerLock ||
      document.webkitExitPointerLock;

    if (exit) {
      try {
        exit.call(document);
      } catch (e) {
        // ignore
      }
    }
  }

  function lockIfAllowed() {
    if (!userToggleLock) return; // user disabled lock with L
    const canvas = getGameCanvas();
    if (!canvas) return;
    if (!isPointerLocked()) {
      requestLock(canvas);
    }
  }

  function handlePointerLockChange() {
    const locked = isPointerLocked();

    if (locked) {
      // We’re in FPS mode; cursor frozen. Good.
      stickyLock = true;
    } else {
      // Lost lock. Only auto‑relock if user wants lock AND we think we're in-game.
      if (userToggleLock) {
        // Small delay to avoid fighting Bloxd when it *really* wants to unlock
        setTimeout(() => {
          // Recheck before relocking
          if (!isPointerLocked()) {
            lockIfAllowed();
          }
        }, 50);
      }
    }
  }

  function handleClick(e) {
    // When player clicks inside the main canvas, we assume they intend to play.
    const canvas = getGameCanvas();
    if (!canvas) return;

    if (e.target === canvas || canvas.contains(e.target)) {
      if (userToggleLock) {
        lockIfAllowed();
      }
    }
  }

  function handleKeydown(e) {
    if (e.key.toLowerCase() === TOGGLE_KEY) {
      // Toggle user-controlled lock
      userToggleLock = !userToggleLock;

      if (!userToggleLock) {
        // User turned lock OFF: release pointer if locked
        if (isPointerLocked()) {
          exitLock();
        }
      } else {
        // User turned lock ON: try to lock immediately if in-game
        lockIfAllowed();
      }
    }
  }

  function injectCursorHintStyle() {
    if (document.getElementById('bloxd-lock-hint-style')) return;

    const style = document.createElement('style');
    style.id = 'bloxd-lock-hint-style';
    style.textContent = `
      /* When pointer is locked, the system cursor is hidden at OS level.
         We don't mess with menus: Bloxd decides when it's in-game. */
      canvas:focus {
        cursor: none !important;
      }
    `;
    document.documentElement.appendChild(style);
  }

  function init() {
    injectCursorHintStyle();

    document.addEventListener('click', handleClick, true);
    window.addEventListener('keydown', handleKeydown, true);

    document.addEventListener('pointerlockchange', handlePointerLockChange, false);
    document.addEventListener('mozpointerlockchange', handlePointerLockChange, false);
    document.addEventListener('webkitpointerlockchange', handlePointerLockChange, false);

    // If the game already locked pointer before our script loaded, respect that
    if (isPointerLocked()) {
      stickyLock = true;
    } else {
      // Try to auto-lock once when the canvas is ready and userToggleLock is true
      setTimeout(() => {
        if (userToggleLock) {
          lockIfAllowed();
        }
      }, 500);
    }
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init, { once: true });
  } else {
    init();
  }
})();