Greasy Fork is available in English.

Bau-Hotkey

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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;
  }
})()