Sinister Streets Tower Hotkeys

Use number keys (1–8) to focus and click tower buttons on Sinister Streets tower page.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sinister Streets Tower Hotkeys
// @namespace    https://sinisterstreets.com/
// @version      1.2.1
// @description  Use number keys (1–8) to focus and click tower buttons on Sinister Streets tower page.
// @match        https://sinisterstreets.com/tower.php
// @grant        none
// @license      MIT
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  // ✅ Key to selector map
  const keyMap = {
    '1': '#attack-btn',
    '2': '#continue-tower-btn',
    '3': '#continue-tower-btn',
    '4': '#repeat-floor-btn',
    '5': '#next-floor-btn',
    '6': '#use-item-btn',
    '7': 'button.healing-item-btn[data-id="25734"]',
    '8': 'button.healing-item-btn[data-id="25735"]'
  };

  const pressCooldown = 400; // ms for double press detection
  const lastPress = {};

  // Random delay range (ms)
  const minDelay = 80;
  const maxDelay = 250;

  function randomDelay() {
    return Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay;
  }

  function isTyping(e) {
    const t = e.target;
    const tag = (t.tagName || '').toLowerCase();
    return tag === 'input' || tag === 'textarea' || t.isContentEditable;
  }

  function focusElement(el) {
    if (!el) return;
    el.focus({ preventScroll: true });
    el.classList.add('hotkey-focus');
    setTimeout(() => el.classList.remove('hotkey-focus'), 200);
  }

  function clickElement(el) {
    if (!el) return;
    el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
    el.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
    el.click();
  }

  window.addEventListener('keydown', (e) => {
    if (isTyping(e)) return;

    const key = e.key;
    if (!keyMap[key]) return;

    const el = document.querySelector(keyMap[key]);
    if (!el) return;

    e.preventDefault();

    const now = Date.now();
    if (lastPress[key] && now - lastPress[key] < pressCooldown) {
      // Second press → click after random delay
      const delay = randomDelay();
      setTimeout(() => {
        clickElement(el);
      }, delay);
      lastPress[key] = 0;
    } else {
      // First press → focus immediately
      focusElement(el);
      lastPress[key] = now;
    }
  });

  const style = document.createElement('style');
  style.textContent = `
    .hotkey-focus {
      outline: 3px solid gold !important;
      box-shadow: 0 0 10px gold !important;
    }
  `;
  document.head.appendChild(style);
})();