FC2PPV 番號提取器 (元素定位版)

從頁面上的文章連結 (href="/articles/...") 精確提取6-8位數字番號並複製到剪貼簿

// ==UserScript==
// @name         FC2PPV 番號提取器 (元素定位版)
// @namespace    http://tampermonkey.net/
// @version      2.0.0
// @description  從頁面上的文章連結 (href="/articles/...") 精確提取6-8位數字番號並複製到剪貼簿
// @author       YourName (Modified by Assistant)
// @match        https://fc2ppvdb.com/*
// @icon         https://www.google.com/s2/favicons?domain=fc2ppvdb.com
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 自定義提示框樣式
    GM_addStyle(`
        .custom-alert {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px;
            background: #9999ff; /* 紫色背景表示成功 */
            color: white;
            border: 1px solid #3a8a3e;
            border-radius: 4px;
            z-index: 9999;
            box-shadow: 0 2px 4px rgba(0,0,0,0.2);
            font-size: 14px;
        }
        .custom-alert.error { /* 添加錯誤樣式 */
             background: #f44336; /* 紅色背景表示未找到或錯誤 */
             border-color: #d32f2f;
        }
    `);

    // 創建可自動關閉的提示框 (稍作修改以支持不同樣式)
    function showAutoCloseAlert(message, duration = 3000, isError = false) {
        // 移除已存在的提示框,避免堆疊
        const existingAlert = document.querySelector('.custom-alert');
        if (existingAlert) {
            existingAlert.remove();
        }

        const alertBox = document.createElement('div');
        alertBox.className = 'custom-alert';
        if (isError) {
            alertBox.classList.add('error'); // 添加錯誤樣式類
        }
        alertBox.textContent = message;
        document.body.appendChild(alertBox);

        setTimeout(() => {
             // 確保元素還在 DOM 中再移除
            if (alertBox && alertBox.parentNode) {
               alertBox.remove();
            }
        }, duration);
    }

    // --- 主要修改:提取數字並複製到剪貼板 (元素定位法) ---
    function extractAndCopy() {
        // 1. 選取所有 href 屬性以 "/articles/" 開頭的 <a> 標籤
        const articleLinks = document.querySelectorAll('a[href^="/articles/"]');
        const numbers = new Set(); // 使用 Set 自動去重

        // 2. 遍歷找到的連結
        articleLinks.forEach(link => {
            const href = link.getAttribute('href'); // 獲取 href 屬性值,例如 "/articles/4585788"
            if (href) {
                // 3. 從 href 中提取數字部分
                // 方法一:分割字符串取最後一部分
                // const parts = href.split('/');
                // const numberString = parts[parts.length - 1];

                // 方法二:使用正則匹配末尾的數字 (更健壯)
                const match = href.match(/\/(\d+)$/); // 匹配斜線後面的數字直到結尾
                if (match && match[1]) {
                    const numberString = match[1]; // 提取匹配到的數字字符串

                    // 4. 驗證數字長度是否為 6-8 位
                    if (numberString.length >= 6 && numberString.length <= 8) {
                        numbers.add(numberString); // 將符合條件的數字加入 Set
                    }
                }
            }
        });

        // 5. 檢查是否找到了數字
        if (numbers.size === 0) {
            showAutoCloseAlert("未在頁面連結中找到符合條件的番號 (6-8位數字)", 3000, true); // 顯示錯誤提示
            return;
        }

        // 6. 格式化結果並複製到剪貼簿
        const result = [...numbers].join('\r\n'); // 將 Set 轉為 Array,用換行符連接
        GM_setClipboard(result);
        showAutoCloseAlert(`已複製 ${numbers.size} 個番號到剪貼簿 (來源: 連結)`, 3000); // 更新成功提示
    }

    // --- 事件監聽部分 (保持不變) ---
    document.addEventListener('keydown', (e) => {
        // 如需使用單獨F8做觸發,則將下面這些區域註解移除即可
		/*
        if (e.key === 'F8') { // 直接按F8觸發
            extractAndCopy();
            e.preventDefault(); // 阻止瀏覽器默認行為
        }
        */

        // 使用 Ctrl+Shift+F8作為啟動鍵
        if (e.ctrlKey && e.shiftKey && e.key === 'F8') {
            extractAndCopy();
            e.preventDefault();
        }
    });

    // 可選:腳本加載時提示用戶如何操作
    // showAutoCloseAlert("番號提取腳本(元素定位版)已加載。按 Ctrl+Shift+F8 複製。", 5000);

})();