MH - Map Helper

Map Helper: Get all mice or loots informations from MHCT in any treasure map in a click!

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         MH - Map Helper
// @description  Map Helper: Get all mice or loots informations from MHCT in any treasure map in a click!
// @author       Snowbits
// @version      1.0.0
// @match        https://www.mousehuntgame.com/*
// @match        https://apps.facebook.com/mousehunt/*
// @icon         https://www.google.com/s2/favicons?domain=mousehuntgame.com
// @namespace    https://greasyfork.org/users/1140737
// @license      MIT
// ==/UserScript==

function injectButton() {

    if (document.querySelector('#mh-map-helper-btn')) {
        return;
    }

    const originalButton = document.querySelector(
        '.mousehuntActionButton.treasureMapView-previewRewardsButton'
    );

    if (!originalButton) {
        return;
    }

    const targetContainer = document.querySelector('.treasureMapRootView-subTabRow');

    const newButton = originalButton.cloneNode(true);

    newButton.id = 'mh-map-helper-btn';
    newButton.title = 'Map Helper Button';
    newButton.style.marginLeft = '10px';
    newButton.classList.remove('lightBlue');

    const span = newButton.querySelector('span');

    if (span) {
        span.textContent = 'Map Helper';
    }

    newButton.addEventListener('click', () => {

        const inputNames = getInputNames();

        const mapName = document
            .querySelector('.mh-ui-map-name')
            ?.textContent
            .trim() || '';

        const isScavengerMap = mapName.includes('Hunt');

        const url = isScavengerMap
            ? 'https://www.mhct.win/scavhelper.php'
            : 'https://www.mhct.win/maphelper.php';

        const form = document.createElement('form');

        form.method = 'POST';
        form.action = url;
        form.target = '_blank';

        const input = document.createElement('input');

        input.type = 'hidden';
        input.name = isScavengerMap ? 'items' : 'mice';
        input.value = inputNames;

        form.appendChild(input);

        document.body.appendChild(form);

        form.submit();

        form.remove();
    });

    targetContainer.insertBefore(newButton,null);
}

function getInputNames() {

    const inputs = {};

    document.querySelectorAll(
        '.treasureMapView-goals-group-goal:not(.complete) .treasureMapView-goals-group-goal-name span'
    ).forEach(span => {

        const name = span.textContent.trim();

        if (name) {
            inputs[name] = name;
        }
    });

    return Object.values(inputs).join('\n');
}

const observer = new MutationObserver(() => {

    const popup = document.querySelector('.treasureMapView');

    if (!popup) {
        return;
    }

    injectButton();
});

observer.observe(document.body, {
    childList: true,
    subtree: true
});