☰

fdc-emp-seeker

Add a button to FDC torrent page to search for Torrents on EMP

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         fdc-emp-seeker
// @namespace    https://codeberg.org/countmancy
// @version      1.0.0
// @description  Add a button to FDC torrent page to search for Torrents on EMP
// @author       CountMancy
// @match        https://femdomcult.org/torrents.php?id=*
// @match        https://femdomcult.org/torrents.php?*id=*
// @match        https://www.femdomcult.org/torrents.php?id=*
// @match        https://www.femdomcult.org/torrents.php?*id=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=femdomcult.org
// @grant        none
// @license      AGPL-3.0-or-later
// @homepageURL  https://codeberg.org/countmancy/fdc-emp-seeker-userscript
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const searchUrlTemplate = 'https://www.empornium.sx/torrents.php?searchtext=%s';
    const tagSearchUrlTemplate = 'https://www.empornium.sx/torrents.php?order_by=time&order_way=desc&taglist=%s';

    function removeCustomMenu() {
        const menu = document.getElementById('custom-tag-menu');
        if (menu) menu.remove();
    }

    // Right click context for tags
    document.addEventListener('contextmenu', (e) => {
        // Always dismiss any existing menu on right click
        removeCustomMenu();

        const tagList = e.target.closest('#torrent_tags_list');
        if (!tagList) return;

        const clickedLink = e.target.closest('a');
        if (clickedLink) {
            const tagText = clickedLink.innerText.trim();

            // Ignore tag voting or modifier buttons like [-], [+], [x]
            if (tagText && !/^\[.+\]$/.test(tagText)) {
                e.preventDefault();

                const menu = document.createElement('div');
                menu.id = 'custom-tag-menu';
                menu.style.cssText = `
                    position: absolute;
                    top: ${e.pageY}px;
                    left: ${e.pageX}px;
                    background: #ffffff;
                    border: 1px solid #d1d5db;
                    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
                    z-index: 100000;
                    padding: 4px 0;
                    border-radius: 6px;
                    min-width: 150px;
                    font-family: sans-serif;
                    font-size: 14px;
                `;

                const encodedQuery = encodeURIComponent(tagText).replace(/%20/g, '+');
                const finalUrl = tagSearchUrlTemplate.replace('%s', encodedQuery);

                const searchItem = document.createElement('a');
                searchItem.href = finalUrl;
                searchItem.target = '_blank';
                searchItem.rel = 'noopener noreferrer';
                searchItem.innerText = `🔍 Search EMP for "${tagText}"`;
                searchItem.style.cssText = `
                    display: block;
                    padding: 8px 16px;
                    text-decoration: none;
                    color: #1f2937;
                    transition: background 0.1s;
                `;

                searchItem.onmouseover = () => searchItem.style.background = '#f3f4f6';
                searchItem.onmouseout = () => searchItem.style.background = 'transparent';

                searchItem.addEventListener('click', () => {
                    removeCustomMenu();
                });

                menu.appendChild(searchItem);
                document.body.appendChild(menu);
            }
        }
    });

    // Close menu when clicking outside
    document.addEventListener('click', (e) => {
        const menu = document.getElementById('custom-tag-menu');
        if (menu && e.target !== menu && !menu.contains(e.target)) {
            removeCustomMenu();
        }
    });

    // Close menu when pressing Escape
    document.addEventListener('keydown', (e) => {
        if (e.key === 'Escape') {
            removeCustomMenu();
        }
    });

    // Search button for title/selected text
    function initFloatingButton() {
        if (document.getElementById('custom-emp-search-btn')) return;

        let fileTitle = "Page";
        const rawTitle = document.title;

        if (rawTitle) {
            const splitTitle = rawTitle.split('::')[0].trim();
            if (splitTitle) {
                fileTitle = splitTitle;
            }
        }

        const defaultEncodedQuery = encodeURIComponent(fileTitle).replace(/%20/g, '+');
        const defaultUrl = searchUrlTemplate.replace('%s', defaultEncodedQuery);

        const searchLink = document.createElement('a');
        searchLink.id = 'custom-emp-search-btn';
        searchLink.href = defaultUrl;
        searchLink.target = '_blank';
        searchLink.rel = 'noopener noreferrer';
        searchLink.innerText = `🔍 Search: ${fileTitle}`;

        searchLink.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 999999;
            padding: 10px 15px;
            background: #2563eb;
            color: white;
            text-decoration: none;
            display: inline-block;
            border: none;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            font-family: sans-serif;
            transition: all 0.2s ease;
            max-width: 300px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        `;

        document.body.appendChild(searchLink);

        let isTextSelected = false;
        let clearSelectionTimeout;

        document.addEventListener('selectionchange', () => {
            const selectedText = window.getSelection().toString().trim();

            if (selectedText) {
                clearTimeout(clearSelectionTimeout);

                isTextSelected = true;
                searchLink.innerText = `🔍 Search EMP`;
                searchLink.style.background = '#059669';

                const encodedQuery = encodeURIComponent(selectedText).replace(/%20/g, '+');
                searchLink.href = searchUrlTemplate.replace('%s', encodedQuery);
            } else {
                // If the highlight disappears (like when pressing a binding), wait 300ms before reverting
                clearSelectionTimeout = setTimeout(() => {
                    isTextSelected = false;
                    searchLink.innerText = `🔍 Search: ${fileTitle}`;
                    searchLink.style.background = '#2563eb';
                    searchLink.href = defaultUrl;
                }, 300);
            }
        });

        searchLink.onmouseover = () => {
            searchLink.style.background = isTextSelected ? '#047857' : '#1d4ed8';
            searchLink.style.transform = 'scale(1.05)';
        };
        searchLink.onmouseout = () => {
            searchLink.style.background = isTextSelected ? '#059669' : '#2563eb';
            searchLink.style.transform = 'scale(1)';
        };
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initFloatingButton);
    } else {
        initFloatingButton();
    }
})();