Buy & Sell Shortcuts

Insert a convenient Buy button and Sell button on item menus

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Buy & Sell Shortcuts
// @namespace    http://tampermonkey.net/
// @version      1.02
// @description  Insert a convenient Buy button and Sell button on item menus
// @author       McPeyen
// @match        https://www.milkywayidle.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let buttonsInsertedForCurrentSubmenu = false;

    function findButtonByTextAndClass(text, partialClass, container = document) {
        const buttons = container.querySelectorAll(`.${partialClass}`);
        for (const button of buttons) {
            if (button.textContent.trim() === text) {
                return button;
            }
        }
        return null;
    }

    // Function to handle the click actions for Sell/Buy
    function handleButtonClick(buttonType) {
        const viewMarketplaceButton = findButtonByTextAndClass('View Marketplace', 'Button_fullWidth__17pVU');
        if (viewMarketplaceButton) {
            console.log('[Custom Buttons] Clicking View Marketplace button');
            viewMarketplaceButton.click();

            setTimeout(() => {
                let targetButtonText;
                let targetButtonClass;

                if (buttonType === 'Sell') {
                    targetButtonText = '+ New Sell Listing';
                    targetButtonClass = 'Button_sell__3FNpM';
                } else if (buttonType === 'Buy') {
                    targetButtonText = '+ New Buy Listing';
                    targetButtonClass = 'Button_buy__3s24l';
                }

                const newListingButton = findButtonByTextAndClass(targetButtonText, targetButtonClass);

                if (newListingButton) {
                    console.log(`[Custom Buttons] Clicking ${targetButtonText} button`);
                    newListingButton.click();
                } else {                    
                }
            }, 400); // Increased delay slightly
        } else {            
        }
    }

    function insertCustomButtons() {
        if (buttonsInsertedForCurrentSubmenu) {
            // console.log('[Custom Buttons] Buttons already inserted for current submenu, skipping.');
            return;
        }

        const amountInputContainer = document.querySelector('.Item_amountInputContainer__1RT17');
        const viewMarketplaceButton = findButtonByTextAndClass('View Marketplace', 'Button_fullWidth__17pVU');

        if (!amountInputContainer || !viewMarketplaceButton) {
            // console.log('[Custom Buttons] Required elements for insertion not found yet.');
            return;
        }

        const buttonParent = viewMarketplaceButton.parentElement; // Assumes 'View Marketplace' is in a suitable parent

        if (!buttonParent) {
            console.error('[Custom Buttons] Could not find parent element for button insertion.');
            return;
        }

        console.log('[Custom Buttons] Attempting to insert custom buttons...');

        const buttonsToInsert = [
            { text: 'New Sell Listing', action: () => handleButtonClick('Sell'), className: '' },
            { text: 'New Buy Listing', action: () => handleButtonClick('Buy'), className: '' }
        ];

        let lastSibling = viewMarketplaceButton; // Start inserting after View Marketplace

        buttonsToInsert.forEach(btnConfig => {
            const existingButton = findButtonByTextAndClass(btnConfig.text, 'Button_fullWidth__17pVU', buttonParent);

            if (!existingButton) {
                const newButton = document.createElement('button');
                newButton.className = `Button_button__1Fe9z Button_fullWidth__17pVU ${btnConfig.className}`;
                newButton.textContent = btnConfig.text;

                lastSibling.insertAdjacentElement('afterend', newButton);
                newButton.addEventListener('click', btnConfig.action);

                lastSibling = newButton;
            } else {
                lastSibling = existingButton; // Ensure correct placement for next button if it exists
            }
        });

        buttonsInsertedForCurrentSubmenu = true;
        console.log('[Custom Buttons] Buttons insertion attempt completed. Set buttonsInsertedForCurrentSubmenu to true.');
    }

    const observer = new MutationObserver((mutationsList, observer) => {
        let submenuAppeared = false;
        let submenuDisappeared = false;

        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === 1 && node.matches && (node.matches('.Item_amountInputContainer__1RT17') || node.querySelector('.Item_amountInputContainer__1RT17'))) {
                        submenuAppeared = true;
                    }
                });

                mutation.removedNodes.forEach(node => {
                    if (node.nodeType === 1 && node.matches && (node.matches('.Item_amountInputContainer__1RT17') || node.querySelector('.Item_amountInputContainer__1RT17'))) {
                        submenuDisappeared = true;
                    }
                });
            }
        }

        if (submenuAppeared) {
            setTimeout(() => {
                insertCustomButtons();
            }, 50); // Small delay
        } else if (submenuDisappeared) {
            buttonsInsertedForCurrentSubmenu = false;
        }
    });


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

    insertCustomButtons();
})();