Post-n-Ghost

Fly away after adding an item to your bazaar. Now with a persistent, collapsible UI.

目前為 2025-12-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Post-n-Ghost
// @namespace    http://tampermonkey.net/
// @version      2.6
// @description  Fly away after adding an item to your bazaar. Now with a persistent, collapsible UI.
// @author       You
// @license      MIT
// @match        https://www.torn.com/*
// @grant        GM.getValue
// @grant        GM.setValue
// ==/UserScript==

/*
The MIT License (MIT)

Copyright (c) 2025 You

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

(async function() {
    'use strict';

    // --- PART 1: Independent UI ---
    // A self-contained, draggable, collapsible, and persistent UI panel.

    const styles = `
        #png-container { position: fixed; width: 250px; z-index: 9999; background-color: #f0f0f0; border: 1px solid #ccc; box-shadow: 0 2px 5px rgba(0,0,0,0.2); font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; font-size: 13px; }
        #png-header { display: flex; justify-content: space-between; align-items: center; padding: 8px; cursor: move; background-color: #333; color: white; font-weight: bold; }
        #png-minimize-btn { background: none; border: none; color: white; font-size: 18px; cursor: pointer; padding: 0 5px; }
        #png-content { padding: 10px; }
        #png-content label { display: block; margin-bottom: 5px; }
        #png-content select { width: 100%; padding: 5px; box-sizing: border-box; margin-bottom: 10px; }
        #png-content button { width: 100%; padding: 8px; border: none; background-color: #4CAF50; color: white; cursor: pointer; }
        #png-content button:hover { background-color: #45a049; }
        #png-current-dest { margin-top: 10px; font-style: italic; color: #555; }
        #png-feedback-msg { margin-top: 10px; padding: 8px; background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; border-radius: 4px; text-align: center; display: none; opacity: 0; transition: opacity 0.5s ease-out; }
        #png-button-container { display: flex; justify-content: space-between; gap: 10px; }
    `;

    const styleSheet = document.createElement("style");
    styleSheet.innerText = styles;
    document.head.appendChild(styleSheet);

    let isMinimized = await GM.getValue("png_isMinimized", false);
    let savedPosition = await GM.getValue("png_position", null);

    const container = document.createElement('div');
    container.id = 'png-container';

    if (savedPosition) {
        container.style.top = savedPosition.top;
        container.style.left = savedPosition.left;
    } else {
        container.style.top = '150px';
        container.style.right = '20px';
    }
    document.body.appendChild(container);

    const header = document.createElement('div');
    header.id = 'png-header';
    header.innerHTML = `
        <span>Post-n-Ghost</span>
        <button id="png-minimize-btn" title="Toggle Minimize">${isMinimized ? '&#9660;' : '&#9650;'}</button>
    `;
    container.appendChild(header);

    const content = document.createElement('div');
    content.id = 'png-content';
    if (isMinimized) {
        content.style.display = 'none';
    }
    container.appendChild(content);

    const destinations = ["Mexico", "Cayman Islands", "Canada", "Hawaii", "Switzerland", "United Kingdom", "Argentina", "China", "UAE", "South Africa"];
    let savedDestination = await GM.getValue("ghostDestination", "");

    const label = document.createElement('label');
    label.setAttribute('for', 'png-dest-input');
    label.textContent = 'Destination:';
    content.appendChild(label);

    const select = document.createElement('select');
    select.id = 'png-dest-input';

    const defaultOption = document.createElement('option');
    defaultOption.value = "";
    defaultOption.textContent = "Select a destination...";
    if (savedDestination === "") defaultOption.selected = true;
    select.appendChild(defaultOption);

    destinations.forEach(dest => {
        const option = document.createElement('option');
        option.value = dest;
        option.textContent = dest;
        if (dest === savedDestination) {
            option.selected = true;
        }
        select.appendChild(option);
    });
    content.appendChild(select);

    const spacer = document.createElement('div');
    spacer.style.marginTop = '10px';
    content.appendChild(spacer);

    const buttonContainer = document.createElement('div');
    buttonContainer.id = 'png-button-container';
    content.appendChild(buttonContainer);

    const saveButton = document.createElement('button');
    saveButton.id = 'png-save-btn';
    saveButton.textContent = 'Save'; // Shorter label
    saveButton.style.width = '48%'; // Adjust width
    buttonContainer.appendChild(saveButton);

    const travelButton = document.createElement('button');
    travelButton.id = 'png-travel-btn';
    travelButton.textContent = 'Travel';
    travelButton.style.width = '48%'; // Adjust width
    travelButton.style.backgroundColor = '#007BFF'; // Different color to distinguish
    buttonContainer.appendChild(travelButton);

    const currentDestDisplay = document.createElement('div');
    currentDestDisplay.id = 'png-current-dest';
    currentDestDisplay.textContent = `Current: ${savedDestination || 'Not set'}`;
    content.appendChild(currentDestDisplay);

    const feedbackMsg = document.createElement('div');
    feedbackMsg.id = 'png-feedback-msg';
    content.appendChild(feedbackMsg);

    saveButton.addEventListener('click', async () => {
        const newDestination = select.value;
        await GM.setValue("ghostDestination", newDestination);
        savedDestination = newDestination;
        currentDestDisplay.textContent = `Current: ${savedDestination || 'Not set'}`;

        // Show feedback message
        feedbackMsg.textContent = newDestination ? `Destination saved: ${newDestination}` : "Destination cleared.";
        feedbackMsg.style.display = 'block';
        setTimeout(() => { feedbackMsg.style.opacity = 1; }, 10); // Timeout to allow display property to apply before transition

        // Hide after a delay
        setTimeout(() => {
            feedbackMsg.style.opacity = 0;
            setTimeout(() => {
                feedbackMsg.style.display = 'none';
            }, 500); // Wait for fade out transition to finish
        }, 2500);
    });

    travelButton.addEventListener('click', async () => {
        const destination = await GM.getValue("ghostDestination");
        if (destination) {
            window.location.href = 'https://www.torn.com/page.php?sid=travel';
        } else {
            alert("Post-n-Ghost: No destination set! Please select and save a destination first.");
        }
    });

    const minimizeButton = document.getElementById('png-minimize-btn');
    minimizeButton.addEventListener('click', async () => {
        isMinimized = !isMinimized;
        content.style.display = isMinimized ? 'none' : 'block';
        minimizeButton.innerHTML = isMinimized ? '&#9660;' : '&#9650;';
        await GM.setValue("png_isMinimized", isMinimized);
    });

    function makeDraggable(element, handle, savePosition) {
        let isDragging = false, dragOffsetX = 0, dragOffsetY = 0;

        handle.addEventListener('mousedown', (e) => {
            // Ignore clicks on buttons within the handle
            if (e.target.tagName === 'BUTTON') return;
            isDragging = true;
            dragOffsetX = e.clientX - element.offsetLeft;
            dragOffsetY = e.clientY - element.offsetTop;
            // Prevent text selection while dragging
            e.preventDefault();
        });

        document.addEventListener('mousemove', (e) => {
            if (isDragging) {
                element.style.left = (e.clientX - dragOffsetX) + 'px';
                element.style.top = (e.clientY - dragOffsetY) + 'px';
                element.style.right = ''; // Clear right property if it was set
            }
        });

        document.addEventListener('mouseup', async () => {
            if (isDragging) {
                isDragging = false;
                if (savePosition) {
                    const finalPosition = { top: element.style.top, left: element.style.left };
                    await savePosition(finalPosition);
                }
            }
        });
    }

    // Make the main UI panel draggable
    makeDraggable(container, header, (position) => GM.setValue("png_position", position));

    // --- PART 2: Travel Page Automation (Robust Version) ---
    async function handleTravelPage() {
        const destination = await GM.getValue("ghostDestination");
        if (!destination) return;

        const lowerCaseDest = destination.toLowerCase().replace(/\s/g, ''); // e.g. "unitedkingdom"

        const performCheckAndClick = () => {
            // --- Mobile Layout Check (buttons with text) ---
            const countrySpans = document.querySelectorAll('span.country___bzUdI');
            if (countrySpans.length > 0) {
                for (const span of countrySpans) {
                    if (span.textContent.trim().toLowerCase() === lowerCaseDest) {
                        const clickableElement = span.closest('button');
                        if (clickableElement) {
                            const clickEvent = new MouseEvent('click', { bubbles: true, cancelable: true });
                            clickableElement.dispatchEvent(clickEvent);
                            return true; // Success
                        }
                    }
                }
            }

            // --- Desktop Layout Check (divs with background-image) ---
            // Use a flexible selector to find the pins
            const pinDivs = document.querySelectorAll('div[class*="pin___"]');
            if (pinDivs.length > 0) {
                for (const pin of pinDivs) {
                    // Check if the background image URL contains the destination name
                    if (pin.style.backgroundImage.toLowerCase().includes(lowerCaseDest)) {
                        const clickEvent = new MouseEvent('click', { bubbles: true, cancelable: true });
                        pin.dispatchEvent(clickEvent);
                        return true; // Success
                    }
                }
            }

            return false; // Failure: Found neither layout
        };

        // 1. Try an immediate check for cached pages.
        if (performCheckAndClick()) {
            return;
        }

        // 2. If the immediate check fails, set up an observer for dynamic pages.
        const observer = new MutationObserver((mutations, obs) => {
            if (performCheckAndClick()) {
                obs.disconnect();
            }
        });

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

        // Stop the observer after 10 seconds as a fallback.
        setTimeout(() => {
            observer.disconnect();
        }, 10000);
    }

    // --- Main Router ---
    function router() {
        if (window.location.search.includes('sid=travel')) {
            handleTravelPage();
        }
    }

    window.addEventListener('hashchange', router);
    router();

})();