Bau-Hotkey

Ermöglicht das Setzen von Gebäuden und POIs mittels Tastenkürzel.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Bau-Hotkey
// @namespace    leeSalami.lss
// @version      1.0.2
// @description  Ermöglicht das Setzen von Gebäuden und POIs mittels Tastenkürzel.
// @author       leeSalami
// @license      MIT
// @match        https://*.leitstellenspiel.de
// ==/UserScript==

(() => {
  'use strict'

  // You can customize the hotkeys according to your needs. Helpful tool to find the codes: https://www.toptal.com/developers/keycode
  const BUILD_HOTKEY = 'KeyK';

  const observerBuildingStart = new MutationObserver(mutationRecords => {
    mutationRecords.forEach(mutation => {
      if (!mutation.target.querySelector('#new_building') || !mutation.addedNodes.length) {
        return;
      }

      observerBuildingStart.disconnect();
      observeBuilding(observerEnd);
      document.addEventListener('keydown', setBuilding);
    });
  });

  const observerPoiStart = new MutationObserver(mutationRecords => {
    mutationRecords.forEach(mutation => {
      if (!mutation.target.querySelector('#new_mission_position') || !mutation.addedNodes.length) {
        return;
      }

      observerPoiStart.disconnect();
      observeBuilding(observerEnd);
      document.addEventListener('keydown', setPoi);
    });
  });

  const observerEnd = new MutationObserver(mutationRecords => {
    mutationRecords.forEach(mutation => {
      if (!mutation.target.querySelector('#build_new_building') || !mutation.addedNodes.length) {
        return;
      }

      observerEnd.disconnect();
      observeBuilding(observerBuildingStart);
      observeBuilding(observerPoiStart);
      document.removeEventListener('keydown', setPoi);
      document.removeEventListener('keydown', setBuilding);
    });
  });

  function observeBuilding(observer) {
    observer.observe(document.getElementById('buildings'), {
      childList: true,
    });
  }

  observeBuilding(observerBuildingStart);
  observeBuilding(observerPoiStart);

  function setPoi(event) {
    if (event.code !== BUILD_HOTKEY || inputHasFocus()) {
      return;
    }

    document.getElementById('new_mission_position').querySelector('.form-actions > input[type="submit"]')?.click();
  }

  function setBuilding(event) {
    if (event.code !== BUILD_HOTKEY || inputHasFocus()) {
      return;
    }

    document.querySelector('.building_build_costs_active > div > input[id^="build_credits_"]')?.click();
  }

  function inputHasFocus() {
    const inputs = document.querySelectorAll('input[type="text"]');

    for (let i = 0, n = inputs.length; i < n; i++) {
      if (inputs[i] === document.activeElement) {
        return true;
      }
    }

    return false;
  }
})()