pid0r / Дополнение к навигатору

// ==UserScript==
// @name         Дополнение к навигатору
// @namespace    https://catwar.su/cw3/
// @version      1.0.0.1
// @description  Бегаем по территориям
// @author       pid0r
// @match        https://catwar.su/cw3/
// @license      MIT; https://opensource.org/licenses/MIT
// ==/UserScript==

(function () {
  'use strict';

  const panel = document.createElement('div');
  panel.style.position = 'fixed';
  panel.style.width = '160px';
  panel.style.backgroundColor = 'rgb(34, 34, 34)';
  panel.style.color = 'rgb(131, 131, 131)';
  panel.style.borderRadius = '10px';
  panel.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.3)';
  panel.style.padding = '10px';
  panel.style.zIndex = '1000';
  panel.style.fontFamily = 'Arial, sans-serif';
  panel.style.cursor = 'grab';
  panel.style.userSelect = 'none';

  const storedLeft = localStorage.getItem('panelLeft');
  const storedTop = localStorage.getItem('panelTop');
  panel.style.left = storedLeft ? `${storedLeft}px` : '10px';
  panel.style.top = storedTop ? `${storedTop}px` : '10px';

  panel.innerHTML = `
        <div style="display: flex; align-items: center; margin-bottom: 5px;">
            <span>Переход:</span>
            <input type="number" id="interval" value="45" style="width: 100%; margin-left: 5px; background-color: rgb(17, 17, 17); color: rgb(131, 131, 131); border: none; border-radius: 5px; ">
        </div>
        <button id="startBtn" style="width: 100%; background-color: rgb(17, 17, 17); color: rgb(131, 131, 131); border: none; border-radius: 5px; cursor: pointer;">Включить</button>
    `;

  const styleb = document.createElement('styleb');
  styleb.innerHTML = `
        button:hover {
            background-color: rgb(14, 14, 14) !important;
        }
        button {
            cursor: pointer !important;
        }
    `;
  document.head.appendChild(styleb);

  document.body.appendChild(panel);

  let isDragging = false;
  let offsetX, offsetY;

  panel.addEventListener('mousedown', function (e) {
    isDragging = true;
    offsetX = e.clientX - panel.getBoundingClientRect().left;
    offsetY = e.clientY - panel.getBoundingClientRect().top;
    panel.style.cursor = 'grabbing';
  });

  document.addEventListener('mousemove', function (e) {
    if (isDragging) {
      const left = e.clientX - offsetX;
      const top = e.clientY - offsetY;
      panel.style.left = `${left}px`;
      panel.style.top = `${top}px`;
      localStorage.setItem('panelLeft', left);
      localStorage.setItem('panelTop', top);
    }
  });

  document.addEventListener('mouseup', function () {
    isDragging = false;
    panel.style.cursor = 'grab';
  });

  panel.addEventListener('touchstart', function (e) {
    isDragging = true;
    const touch = e.touches[0];
    offsetX = touch.clientX - panel.getBoundingClientRect().left;
    offsetY = touch.clientY - panel.getBoundingClientRect().top;
    panel.style.cursor = 'grabbing';
  });

  document.addEventListener('touchmove', function (e) {
    if (isDragging) {
      const touch = e.touches[0];
      const left = touch.clientX - offsetX;
      const top = touch.clientY - offsetY;
      panel.style.left = `${left}px`;
      panel.style.top = `${top}px`;
      localStorage.setItem('panelLeft', left);
      localStorage.setItem('panelTop', top);
    }
  });

  document.addEventListener('touchend', function () {
    isDragging = false;
    panel.style.cursor = 'grab';
  });

  let intervalId;

  document.getElementById('startBtn').onclick = function () {
    const interval = parseInt(document.getElementById('interval').value, 10) * 1000;

    if (intervalId) {
      clearInterval(intervalId);
    }

    const performClick = () => {
      const moveElement = document.querySelector('.move_parent[style*="background-color: rgba(255, 0, 0, 0.4)"]');
      if (moveElement) {
        moveElement.click();
        console.log("Clicked on the red element");
      }
    };

    const startCycle = async () => {
      while (true) {
        performClick();
        await new Promise(r => setTimeout(r, interval));
        const randomDelay = Math.floor(Math.random() * 7000) + 3000;
        await new Promise(r => setTimeout(r, randomDelay));
      }
    };

    intervalId = startCycle();
  };
})();