Skip to turn numeric input

From Infinite Worlds
Jump to navigation Jump to search
Prompt that says "Select a turn number between 1 and 146". Below it is a numeric input, allowing the user to use buttons to select a number between 1 and the latest turn number.
Numeric input allowing the use of buttons to select a number between 1 and the latest turn number.

This is a userscript that changes the "Skip to turn number" input type to number. This input field currently allows you to enter any text, but in practice only numbers are accepted.

To use this userscript:

  1. Install a userscript manager extension for your browser
  2. Save this script to your computer
  3. Drag the downloaded script into your userscript manager extension
// ==UserScript==
// @name         Skip to turn numeric input
// @namespace    https://infiniteworlds.app/
// @version      1.0
// @description  Changes the "Skip to turn number" input type to number
// @match        https://infiniteworlds.app/
// @icon         https://infinite-worlds-images.us-east-1.linodeobjects.com/00000__logo_square_on_background_512.jpg
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function skipToTurnHandler(e) {
        const modal = document.getElementById("alert-modal");
        if (modal === null) {
            return;
        }
        const instructionsSelector = "//h4[starts-with(text(), 'Select a turn number')]";
        const instructions = evaluateXpathFirstNode(instructionsSelector, modal);
        if (instructions === null) {
            return;
        }
        const regex = /\d+$/;
        const turnInputSelector = "//h4[starts-with(text(), 'Select a turn number')]/ancestor::div[@class = 'modal-content']/descendant::input[@type = 'text']";
        const turnInput = evaluateXpathFirstNode(turnInputSelector, modal);
        if (turnInput !== null) {
            turnInput.type = "number";
            turnInput.min = "1";
            turnInput.max = instructions.textContent.match(regex)[0];
            turnInput.value = "1";
        }
    }
    let skipToTurnObserver = new MutationObserver(skipToTurnHandler);
    skipToTurnObserver.observe(document.body, { childList: true, subtree: true });

    function evaluateXpathFirstNode(expression, contextNode = document) {
        return document.evaluate(
            expression,
            contextNode,
            null,
            XPathResult.FIRST_ORDERED_NODE_TYPE,
            null
        ).singleNodeValue;
    }

})();