Ortenskung hack / script 1.7

Automates crime

当前为 2025-09-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         Ortenskung hack / script 1.7 
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Automates crime
// @match        https://www.ortenskung.com/en/*
// @grant        none
// ==/UserScript==


// Dummy functions for example — replace with your actual code

(function() {
    'use strict';

    // --- KEEP ALIVE CODE STARTS HERE ---

    // 1. Heartbeat: keeps JS engine awake
    setInterval(() => {
        console.log('Heartbeat: tab still active');
    }, 30000); // 30 seconds

    // 2. Silent audio: prevents tab suspension in some browsers
    try {
        const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        const oscillator = audioCtx.createOscillator();
        oscillator.frequency.value = 0; // near silent
        oscillator.connect(audioCtx.destination);
        oscillator.start();
    } catch (err) {
        console.log('Audio context failed', err);
    }

    // --- KEEP ALIVE CODE ENDS HERE ---
 let running = false;
    let timer = null;
    let selectedCrimeId = 1;
    let autoClose = true;

    // --- CREATE GUI ---
    const panel = document.createElement('div');
    panel.style.position = 'fixed';
    panel.style.top = '50px';
    panel.style.right = '50px';
    panel.style.width = '300px';
    panel.style.background = '#222';
    panel.style.color = '#fff';
    panel.style.borderRadius = '10px';
    panel.style.boxShadow = '0 4px 12px rgba(0,0,0,0.5)';
    panel.style.fontFamily = 'sans-serif';
    panel.style.zIndex = '9999';
    panel.style.transition = 'width 0.3s, height 0.3s';
    panel.style.userSelect = 'none';

    panel.innerHTML = `
        <div id="panelHeader" style="cursor: move; background:#111; padding:8px; border-radius:10px 10px 0 0; display:flex; justify-content:space-between; align-items:center;">
            <strong>Crime Bot</strong>
            <div>
                <button id="minimizeBtn" style="background:none; border:none; color:#fff; cursor:pointer;">_</button>
                <button id="closeBtn" style="background:none; border:none; color:#fff; cursor:pointer;">X</button>
            </div>
        </div>
        <div id="panelBody" style="padding:10px;">
            <div id="tabs" style="display:flex; margin-bottom:10px;">
                <button class="tabBtn" data-tab="main" style="flex:1; background:#333; color:#fff; border:none; padding:5px; cursor:pointer;">Main</button>
                <button class="tabBtn" data-tab="settings" style="flex:1; background:#333; color:#fff; border:none; padding:5px; cursor:pointer;">Settings</button>
            </div>
            <div id="tabContent">
                <div class="tabPanel" data-tab="main">
                    <label for="crimeIdSelect">Crime ID:</label>
                    <select id="crimeIdSelect" style="width:100%; margin-bottom:10px;">
                        ${Array.from({length:30}, (_,i)=>i+1)
                            .map(id => `<option value="${id}" ${id===1?'selected':''}>${id}</option>`).join('')}
                    </select>
                    <label><input type="checkbox" id="autoCloseCheckbox" checked> Close GUI</label><br><br>
                    <button id="crimeToggleBtn" style="width:100%; padding:5px;">Start</button>
                </div>
                <div class="tabPanel" data-tab="settings" style="display:none;">
                    <p>Additional options can go here</p>
                    <p>You could add timers, hotkeys, logs, etc.</p>
                </div>
            </div>
        </div>
    `;
    document.body.appendChild(panel);

    // --- DRAGGING ---
    const header = document.getElementById('panelHeader');
    let isDragging = false, offsetX = 0, offsetY = 0;
    header.addEventListener('mousedown', e => {
        isDragging = true;
        offsetX = e.clientX - panel.offsetLeft;
        offsetY = e.clientY - panel.offsetTop;
    });
    document.addEventListener('mousemove', e => {
        if (isDragging) {
            panel.style.left = `${e.clientX - offsetX}px`;
            panel.style.top = `${e.clientY - offsetY}px`;
        }
    });
    document.addEventListener('mouseup', () => isDragging = false);

    // --- TABS ---
    const tabButtons = panel.querySelectorAll('.tabBtn');
    const tabPanels = panel.querySelectorAll('.tabPanel');
    tabButtons.forEach(btn => {
        btn.addEventListener('click', () => {
            tabButtons.forEach(b => b.style.background = '#333');
            btn.style.background = '#555';
            tabPanels.forEach(p => p.style.display = p.dataset.tab === btn.dataset.tab ? 'block' : 'none');
        });
    });

    // --- MINIMIZE / CLOSE ---
    const panelBody = document.getElementById('panelBody');
    document.getElementById('minimizeBtn').addEventListener('click', () => {
        panelBody.style.display = panelBody.style.display === 'none' ? 'block' : 'none';
    });
    document.getElementById('closeBtn').addEventListener('click', () => panel.remove());

    // --- KEYBOARD ESC ---
    document.addEventListener('keydown', e => { if (e.key === 'Escape') panel.remove(); });

    // --- ELEMENTS ---
    const toggleBtn = document.getElementById('crimeToggleBtn');
    const crimeIdSelect = document.getElementById('crimeIdSelect');
    const autoCloseCheckbox = document.getElementById('autoCloseCheckbox');

    // --- EVENT LISTENERS ---
    crimeIdSelect.addEventListener('change', () => selectedCrimeId = parseInt(crimeIdSelect.value,10));
    autoCloseCheckbox.addEventListener('change', () => autoClose = autoCloseCheckbox.checked);

    toggleBtn.addEventListener('click', () => {
        running = !running;
        toggleBtn.textContent = running ? 'Stop' : 'Start';
        if (running) performNeighborhoodCrime();
        else if (timer) clearTimeout(timer);
    });

    // --- AUTOMATION FUNCTION ---
    function performNeighborhoodCrime() {
        try {
            console.log('Performing crime', selectedCrimeId);
            // put your real automation code here
        } catch(err) {
            console.log('Error in automation:', err);
        }
        timer = setTimeout(() => { if (running) performNeighborhoodCrime(); }, 5000);
    }


    // ------------------ Helper ------------------
    function waitForElement(selector, timeout = 5000) {
        return new Promise((resolve, reject) => {
            const interval = 100;
            let elapsed = 0;
            const timer = setInterval(() => {
                const el = document.querySelector(selector);
                if (el) {
                    clearInterval(timer);
                    resolve(el);
                }
                elapsed += interval;
                if (elapsed >= timeout) {
                    clearInterval(timer);
                    reject('Element not found: ' + selector);
                }
            }, interval);
        });
    }

    // ------------------ Main Loop ------------------
// ------------------ Main Loop ------------------
async function performNeighborhoodCrime() {
    if (!running) return; // bail if stopped

    try {
        // Step 0: Check timers
        const timerEl = document.querySelector('#my_timers > div.val');
        if (!timerEl) {
            console.log('No timer element found. Retrying in 5s...');
            timer = setTimeout(performNeighborhoodCrime, 5000);
            return;
        }

        const m = timerEl.textContent.trim().match(/^(\d+)\s*\/\s*(\d+)$/);
        if (!m) {
            console.log('Timer text not in n/n format. Retrying in 5s...');
            timer = setTimeout(performNeighborhoodCrime, 5000);
            return;
        }

        const used = parseInt(m[1], 10);
        const total = parseInt(m[2], 10);
        const available = total - used;
        console.log(`Timers used: ${used} / ${total}, Available: ${available}`);

        // Step 1: If no timers available, wait and retry
        if (available === 0) {
            console.log('No timers available. Waiting 5s...');
            timer = setTimeout(performNeighborhoodCrime, 5000);
            return; // Skip everything else
        }

        // Step 2: Only open crime GUI if timers are available
        if (!document.querySelector('#go_crimes_dialog')) {
            const crimeDialogBtn = document.querySelector('#go_crimes');
            if (crimeDialogBtn) crimeDialogBtn.click();
        }

        // Step 3: Click "Neighborhood" only if not already in it
        const neighborhoodBtn = Array.from(document.querySelectorAll('.crimes_button'))
                                     .find(el => el.textContent.trim() === "Neighborhood");
        if (neighborhoodBtn && !document.querySelector('#neighborhood_crimes')) {
            neighborhoodBtn.click();
        }

        // Step 4: Commit selected crime if ready
        const commitBtn = Array.from(document.querySelectorAll('.crime_button_small'))
                               .find(el => el.getAttribute('onclick')?.includes(`'id':'${selectedCrimeId}'`));
        if (!commitBtn) {
            console.log(`Crime button ID ${selectedCrimeId} not ready. Waiting 5s...`);
            timer = setTimeout(performNeighborhoodCrime, 5000);
            return;
        }

        commitBtn.click();

        // ✅ Step 5: Close dialogs after committing the crime
        setTimeout(() => {
            // close "social" dialog
            if (typeof close_dialog === 'function') {
                close_dialog('social');
            }
            // close "levelup" popup
            if (typeof close_dialog === 'function') {
                close_dialog('levelup');
            }
            // close "crimes" popup only if autoClose is true
            if (autoClose && typeof close_dialog === 'function') {
                close_dialog('crimes');
            }

            // also close notification button if present
            const notifBtn = document.querySelector('.button.first');
            if (notifBtn) notifBtn.click();
        }, 1000);

        // Step 6: Wait 4s before next iteration
        timer = setTimeout(performNeighborhoodCrime, 4000);

    } catch (err) {
        console.log('Error in automation:', err);
        timer = setTimeout(performNeighborhoodCrime, 5000);
    }
}



})();