Torn Quick Links

Floating draggable menu to manage and quickly access your favorite links.

当前为 2025-12-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Torn Quick Links
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Floating draggable menu to manage and quickly access your favorite links.
// @author       Omanpx [1906686] + Gemini
// @match        https://www.torn.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // --- State Management ---
    let links = GM_getValue('navigator_links', [
        { id: 1, label: 'Armory', url: 'https://www.torn.com/factions.php?step=your&type=1#/tab=armoury', color: '#4285f4', emoji: '🔫', target: '_self' },
        { id: 2, label: 'Lottery', url: 'https://www.torn.com/page.php?sid=lottery', color: '#ff0000', emoji: '🍀', target: '_self' }
    ]);

    let isMenuOpen = false;
    let dragEnabled = GM_getValue('drag_enabled', false);
    let buttonPos = GM_getValue('button_pos', { top: '20px', left: '20px' });

    // --- Styles ---
    const style = document.createElement('style');
    style.textContent = `
        #nav-pro-container {
            position: fixed;
            z-index: 999999;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            user-select: none;
            touch-action: none;
        }

        #nav-pro-trigger {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #333;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 4px 12px rgba(0,0,0,0.5);
            font-size: 24px;
            transition: transform 0.2s, border-color 0.2s;
            border: 2px solid transparent;
        }

        #nav-pro-trigger.drag-active {
            cursor: move;
            border-color: #4ade80;
        }

        #nav-pro-trigger:active { transform: scale(0.9); }

        #nav-pro-menu {
            position: absolute;
            top: 60px;
            left: 0;
            width: 280px;
            max-height: 500px;
            background: #222;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0,0,0,0.6);
            display: none;
            flex-direction: column;
            overflow: hidden;
            border: 1px solid #444;
            color: #eee;
        }

        .nav-pro-header {
            padding: 12px;
            background: #1a1a1a;
            border-bottom: 1px solid #444;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-weight: bold;
        }

        .nav-pro-toolbar {
            padding: 8px 12px;
            background: #2a2a2a;
            border-bottom: 1px solid #444;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
        }

        .nav-pro-list {
            overflow-y: auto;
            max-height: 300px;
            padding: 8px;
        }

        .nav-pro-item {
            display: flex;
            align-items: center;
            padding: 10px;
            margin-bottom: 4px;
            border-radius: 8px;
            text-decoration: none;
            color: #ddd;
            transition: background 0.2s;
            position: relative;
        }

        .nav-pro-item:hover { background: #333; color: #fff; }

        .nav-pro-color-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 12px;
        }

        .nav-pro-emoji { margin-right: 8px; }

        .nav-pro-add-form {
            padding: 12px;
            border-top: 1px solid #444;
            background: #1a1a1a;
            font-size: 13px;
        }

        .nav-pro-input {
            width: 100%;
            padding: 6px 8px;
            margin-bottom: 6px;
            border: 1px solid #444;
            border-radius: 4px;
            box-sizing: border-box;
            background: #333;
            color: #eee;
        }

        .nav-pro-btn {
            width: 100%;
            padding: 8px;
            background: #444;
            color: white;
            border: 1px solid #555;
            border-radius: 4px;
            cursor: pointer;
            font-weight: 600;
        }

        .nav-pro-btn:hover { background: #555; }

        .nav-pro-delete {
            margin-left: auto;
            color: #ef4444;
            cursor: pointer;
            padding: 4px;
            font-size: 12px;
            opacity: 0.7;
        }

        .nav-pro-delete:hover { opacity: 1; }

        .nav-pro-config-row {
            display: flex;
            gap: 4px;
            margin-bottom: 6px;
        }

        #nav-pro-toggle-add {
            background: none;
            border: 1px solid #444;
            color: #ccc;
            border-radius: 4px;
            padding: 2px 8px;
        }

        #nav-pro-toggle-add:hover { background: #333; color: white; }

        .switch {
            position: relative;
            display: inline-block;
            width: 34px;
            height: 20px;
        }

        .switch input { opacity: 0; width: 0; height: 0; }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0; left: 0; right: 0; bottom: 0;
            background-color: #444;
            transition: .4s;
            border-radius: 20px;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 14px; width: 14px;
            left: 3px; bottom: 3px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }

        input:checked + .slider { background-color: #2563eb; }
        input:checked + .slider:before { transform: translateX(14px); }

        ::-webkit-scrollbar { width: 6px; }
        ::-webkit-scrollbar-thumb { background: #444; border-radius: 10px; }
    `;
    document.head.appendChild(style);

    // --- UI Construction ---
    const container = document.createElement('div');
    container.id = 'nav-pro-container';
    container.style.top = buttonPos.top;
    container.style.left = buttonPos.left;

    container.innerHTML = `
        <div id="nav-pro-trigger" class="${dragEnabled ? 'drag-active' : ''}">🧭</div>
        <div id="nav-pro-menu">
            <div class="nav-pro-header">
                <span>Torn Quick Links</span>
                <button id="nav-pro-toggle-add" style="font-size:12px; cursor:pointer;">+ Add Link</button>
            </div>
            <div class="nav-pro-toolbar">
                <span>Enable Dragging</span>
                <label class="switch">
                    <input type="checkbox" id="drag-toggle-switch" ${dragEnabled ? 'checked' : ''}>
                    <span class="slider"></span>
                </label>
            </div>
            <div class="nav-pro-list" id="nav-pro-items"></div>
            <div class="nav-pro-add-form" id="nav-pro-form" style="display:none;">
                <input type="text" id="add-label" class="nav-pro-input" placeholder="Label (e.g. GitHub)">
                <input type="text" id="add-url" class="nav-pro-input" placeholder="URL (https://...)">
                <div class="nav-pro-config-row">
                    <input type="text" id="add-emoji" class="nav-pro-input" style="width:40%" placeholder="Emoji">
                    <input type="color" id="add-color" class="nav-pro-input" style="width:60%; height:30px; padding:2px; border:none;" value="#4285f4">
                </div>
                <div class="nav-pro-config-row" style="align-items:center; padding-bottom:8px;">
                    <input type="checkbox" id="add-target" unchecked> <label for="add-target">Open in new tab</label>
                </div>
                <button id="nav-pro-save" class="nav-pro-btn">Save Link</button>
            </div>
        </div>
    `;
    document.body.appendChild(container);

    const trigger = container.querySelector('#nav-pro-trigger');
    const menu = container.querySelector('#nav-pro-menu');
    const listContainer = container.querySelector('#nav-pro-items');
    const addForm = container.querySelector('#nav-pro-form');
    const dragToggle = container.querySelector('#drag-toggle-switch');

    // --- Core Functions ---
    function renderLinks() {
        listContainer.innerHTML = '';
        links.forEach(link => {
            const item = document.createElement('div');
            item.className = 'nav-pro-item';
            item.innerHTML = `
                <div class="nav-pro-color-dot" style="background: ${link.color}"></div>
                <span class="nav-pro-emoji">${link.emoji || ''}</span>
                <span style="flex-grow:1; cursor:pointer;" class="nav-link-click">${link.label}</span>
                <span class="nav-pro-delete" data-id="${link.id}">✕</span>
            `;

            item.querySelector('.nav-link-click').onclick = (e) => {
                e.preventDefault();
                window.open(link.url, link.target || '_self');
                toggleMenu(false);
            };

            item.querySelector('.nav-pro-delete').onclick = (e) => {
                e.stopPropagation();
                links = links.filter(l => l.id !== link.id);
                saveAndRefresh();
            };

            listContainer.appendChild(item);
        });
    }

    function saveAndRefresh() {
        GM_setValue('navigator_links', links);
        renderLinks();
    }

    function toggleMenu(force) {
        isMenuOpen = force !== undefined ? force : !isMenuOpen;
        menu.style.display = isMenuOpen ? 'flex' : 'none';

        if (isMenuOpen) {
            const rect = menu.getBoundingClientRect();
            if (rect.right > window.innerWidth) {
                menu.style.left = `-${rect.width - 50}px`;
            } else {
                menu.style.left = '0';
            }
        }
    }

    // --- Interaction Logic (Optimized for Mobile) ---
    let startX, startY, initialTop, initialLeft, hasMoved;

    const onPointerDown = (e) => {
        const ev = e.touches ? e.touches[0] : e;
        startX = ev.clientX;
        startY = ev.clientY;
        initialTop = container.offsetTop;
        initialLeft = container.offsetLeft;
        hasMoved = false;

        document.addEventListener('mousemove', onPointerMove);
        document.addEventListener('touchmove', onPointerMove, { passive: false });
        document.addEventListener('mouseup', onPointerUp);
        document.addEventListener('touchend', onPointerUp);
    };

    const onPointerMove = (e) => {
        if (!dragEnabled) return;

        const ev = e.touches ? e.touches[0] : e;
        const deltaX = ev.clientX - startX;
        const deltaY = ev.clientY - startY;

        // Threshold to differentiate tap from drag
        if (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5) {
            hasMoved = true;
            container.style.left = `${initialLeft + deltaX}px`;
            container.style.top = `${initialTop + deltaY}px`;
            if (e.cancelable) e.preventDefault();
        }
    };

    const onPointerUp = () => {
        document.removeEventListener('mousemove', onPointerMove);
        document.removeEventListener('touchmove', onPointerMove);
        document.removeEventListener('mouseup', onPointerUp);
        document.removeEventListener('touchend', onPointerUp);

        if (!hasMoved) {
            toggleMenu();
        } else if (dragEnabled) {
            GM_setValue('button_pos', { top: container.style.top, left: container.style.left });
        }
    };

    trigger.addEventListener('mousedown', onPointerDown);
    trigger.addEventListener('touchstart', onPointerDown, { passive: false });

    // --- Control Handlers ---
    dragToggle.onchange = (e) => {
        dragEnabled = e.target.checked;
        GM_setValue('drag_enabled', dragEnabled);
        if (dragEnabled) {
            trigger.classList.add('drag-active');
        } else {
            trigger.classList.remove('drag-active');
        }
    };

    container.querySelector('#nav-pro-toggle-add').onclick = () => {
        addForm.style.display = addForm.style.display === 'none' ? 'block' : 'none';
    };

    container.querySelector('#nav-pro-save').onclick = () => {
        const label = document.querySelector('#add-label').value;
        const url = document.querySelector('#add-url').value;
        const emoji = document.querySelector('#add-emoji').value;
        const color = document.querySelector('#add-color').value;
        const target = document.querySelector('#add-target').checked ? '_blank' : '_self';

        if (label && url) {
            links.push({
                id: Date.now(),
                label,
                url: url.startsWith('http') ? url : `https://${url}`,
                emoji,
                color,
                target
            });
            saveAndRefresh();
            document.querySelector('#add-label').value = '';
            document.querySelector('#add-url').value = '';
            addForm.style.display = 'none';
        }
    };

    renderLinks();
})();