Neopets: Vending Machine

Plays vending machine if you have nerkmids.

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.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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         Neopets: Vending Machine
// @namespace    http://tampermonkey.net/
// @version      1.2.0
// @description  Plays vending machine if you have nerkmids.
// @author       Nyu@Clraik
// @match        *://*.neopets.com/vending.phtml*
// @match        *://*.neopets.com/vending2.phtml*
// @match        *://*.neopets.com/vending3.phtml*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM.getValue
// @grant        GM.setValue
// ==/UserScript==

(async function() {
    'use strict';
    if (typeof GM_getValue == "undefined") GM_getValue = GM.getValue
    if (typeof GM_setValue == "undefined") GM_setValue = GM.setValue

    const sleep = ms => new Promise(r => setTimeout(r, ms))
    const randomBetween = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min

    const isRunning = async () => await GM_getValue('vm.running', false)
    const setRunning = async val => await GM_setValue('vm.running', val)

    const pageTitle = document.querySelector('.page-title__2020')

    const panel = document.createElement('div')
    panel.style.width = '450px'
    panel.style.margin = '20px auto'
    panel.style.padding = '10px'
    panel.style.textAlign = 'center'
    panel.style.border = '1px solid #ccc'
    panel.style.borderRadius = '10px'

    const title = document.createElement('div')
    title.textContent = 'Vending Machine Auto-Player'
    title.style.fontSize = '1.5em'
    title.style.fontFamily = '"Cafeteria", "Arial Bold", sans-serif'
    title.style.marginBottom = '10px'

    const btnRow = document.createElement('div')
    btnRow.style.display = 'flex'
    btnRow.style.justifyContent = 'center'
    btnRow.style.gap = '10px'
    btnRow.style.marginBottom = '10px'

    const toggleBtn = document.createElement('button')
    toggleBtn.className = 'button-default__2020 btn-single__2020 button-green__2020'

    const clearBtn = document.createElement('button')
    clearBtn.textContent = 'Clear Log'
    clearBtn.className = 'button-default__2020 btn-single__2020 button-yellow__2020'

    btnRow.append(toggleBtn)

    const logBox = document.createElement('textarea')
    logBox.readOnly = true
    logBox.style.width = '90%'
    logBox.style.height = '150px'
    logBox.style.fontSize = '1.2em'
    logBox.style.fontFamily = '"Cafeteria", "Arial Bold", sans-serif'
    logBox.style.resize = 'vertical'
    logBox.style.borderRadius = '5px'
    logBox.style.boxShadow = '0 0 5px black'
    logBox.style.border = 'none'
    logBox.style.outline = 'none'
    logBox.style.padding = '5px 10px'
    logBox.style.backgroundColor = 'rgba(0, 0, 0, 0.1)'

    panel.append(title, btnRow)

    if (pageTitle) pageTitle.before(panel)
    else {
        const content = document.querySelector('td.content')
        if (content) content.prepend(panel)
    }

    const getLog = async () => await GM_getValue('vm.log', [])

    const renderLog = async () => {
        const log = await getLog()
        if (log.length === 0) {
            btnRow.contains(clearBtn) && clearBtn.remove()
            panel.contains(logBox) && logBox.remove()
            return
        }
        if (!btnRow.contains(clearBtn)) btnRow.append(clearBtn)
        if (!panel.contains(logBox)) panel.append(logBox)
        logBox.value = log.join('\n')
        logBox.scrollTop = logBox.scrollHeight
    }

    clearBtn.addEventListener('click', async () => {
        await GM_setValue('vm.log', [])
        await renderLog()
    })

    const updateUI = async () => {
        const running = await isRunning()
        toggleBtn.textContent = running ? 'Stop' : 'Start'
        toggleBtn.className = running
            ? 'button-default__2020 btn-single__2020 button-red__2020'
            : 'button-default__2020 btn-single__2020 button-green__2020'
    }

    const doAction = async () => {
        await sleep(randomBetween(100, 5000))
        if (!await isRunning()) return

        const page = location.pathname

        if (page === '/vending.phtml') {
            document.querySelector("input[value='Press me to Continue!']")?.click()
        } else if (page === '/vending2.phtml') {
            const nerkmidSelect = document.querySelector('select[name="nerkmid_id"]')
            if (!nerkmidSelect) {
                await setRunning(false)
                await updateUI()
                toggleBtn.textContent = 'Out of Nerkmids!'
                toggleBtn.className = 'button-default__2020 btn-single__2020'
                toggleBtn.disabled = true
                return
            }
            nerkmidSelect.selectedIndex = 1

            const setIndex = (name, index) => {
                const sel = document.querySelector(`select[name="${name}"]`)
                if (sel) sel.selectedIndex = index
            }
            setIndex('large_button', 3)
            setIndex('small_button', randomBetween(1, 6))
            setIndex('button_presses', randomBetween(1, 5))
            setIndex('lever_pulls', 6)

            document.querySelector("input[value='GO!!!']")?.click()
        } else if (page === '/vending3.phtml') {
            const content = document.querySelector('td.content')
            const paragraphs = content.querySelectorAll('center > p')
            const prizeText = paragraphs[0]?.textContent.trim() || 'Unknown prize'
            const itemName = paragraphs[1]?.querySelector('b')?.textContent.trim()
            const entry = itemName ? `${prizeText} - ${itemName}` : prizeText
            const log = await getLog()
            log.push(entry)
            await GM_setValue('vm.log', log)

            document.querySelector("input[value='Play Again!']")?.click()
        }
    }

    toggleBtn.addEventListener('click', async () => {
        const running = await isRunning()
        await setRunning(!running)
        await updateUI()
        if (!running) doAction()
    })

    await updateUI()
    await renderLog()

    if (await isRunning()) doAction()
})();