// ==UserScript==
// @name         网页一键屏蔽与举报(移动端+PC)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  一键屏蔽当前网页并举报到12321(支持手机和电脑)
// @author       YourName
// @match        *://*/*
// @grant        GM_openInTab
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_notification
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // 检查是否已屏蔽当前网页
    function checkBlocked() {
        // 优先检查GM存储(兼容旧版本)
        const gmBlocked = GM_getValue('blocked_sites', []);
        if (gmBlocked.includes(location.href)) {
            window.location.href = 'about:blank';
            return;
        }
        
        // 检查localStorage
        const localBlocked = JSON.parse(localStorage.getItem('blocked_sites') || '[]');
        if (localBlocked.includes(location.href)) {
            window.location.href = 'about:blank';
        }
    }

    // 举报到12321网络不良与垃圾信息举报受理中心
    function reportTo12321(url) {
        // 仅复制网站地址到剪贴板
        navigator.clipboard.writeText(url).then(() => {
            GM_notification({
                text: '网站地址已复制到剪贴板,请前往12321.cn粘贴举报',
                title: '操作提示',
                timeout: 3000
            });
            
            // 仍然打开举报网站但不带参数
            GM_openInTab('https://www.12321.cn/web', {
                active: true,
                insert: true,
                setParent: true
            });
        });
    }

    // 创建响应式按钮
    function createBlockButton() {
        // 检查是否已隐藏按钮
        const hiddenSites = GM_getValue('hidden_sites', []);
        if (hiddenSites.includes(location.hostname)) {
            return; // 如果已隐藏则不再创建按钮
        }

        const container = document.createElement('div');
        container.style.position = 'fixed';
        container.style.bottom = '15px';
        container.style.right = '15px';
        container.style.zIndex = '99999';

        // 主按钮
        const mainBtn = document.createElement('div');
        mainBtn.innerHTML = '👻'; // 使用鬼魂emoji
        mainBtn.style.width = '20px';  // 更小尺寸
        mainBtn.style.height = '20px'; // 更小尺寸
        mainBtn.style.backgroundColor = 'transparent'; // 透明背景
        mainBtn.style.color = '#f44336'; // 红色emoji
        mainBtn.style.borderRadius = '50%';
        mainBtn.style.display = 'flex';
        mainBtn.style.justifyContent = 'center';
        mainBtn.style.alignItems = 'center';
        mainBtn.style.fontSize = '14px'; // 稍大字体保持可见性
        mainBtn.style.cursor = 'pointer';
        mainBtn.style.transition = 'all 0.2s ease';
        
        // 悬停效果
        mainBtn.addEventListener('mouseenter', function() {
            mainBtn.style.transform = 'scale(1.2)';
            mainBtn.style.textShadow = '0 0 5px rgba(244,67,54,0.5)';
        });
        mainBtn.addEventListener('mouseleave', function() {
            mainBtn.style.transform = 'scale(1)';
            mainBtn.style.textShadow = 'none';
        });

        // 操作菜单
        const menu = document.createElement('div');
        menu.style.display = 'none';
        menu.style.position = 'absolute';
        menu.style.bottom = '35px';
        menu.style.right = '0';
        menu.style.backgroundColor = 'white';
        menu.style.borderRadius = '8px';
        menu.style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)';
        menu.style.overflow = 'hidden';

        // 举报按钮
        const reportBtn = document.createElement('div');
        reportBtn.textContent = '举报';
        reportBtn.style.padding = '8px 15px';
        reportBtn.style.fontSize = '12px';
        reportBtn.style.color = 'black';
        reportBtn.style.cursor = 'pointer';
        reportBtn.style.borderBottom = '1px solid #eee';
        // 检查是否已屏蔽当前网页
        function checkBlocked() {
            // 优先检查GM存储(兼容旧版本)
            const gmBlocked = GM_getValue('blocked_sites', []);
            if (gmBlocked.includes(location.href)) {
                window.location.href = 'about:blank';
                return;
            }
            
            // 检查localStorage
            const localBlocked = JSON.parse(localStorage.getItem('blocked_sites') || '[]');
            if (localBlocked.includes(location.href)) {
                window.location.href = 'about:blank';
            }
        }
    
        // 举报按钮点击事件
        reportBtn.addEventListener('click', function() {
            if (confirm('确定要举报并屏蔽当前网页吗?')) {
                // 同时存储到GM和localStorage
                const blockedSites = GM_getValue('blocked_sites', []);
                if (!blockedSites.includes(location.href)) {
                    blockedSites.push(location.href);
                    GM_setValue('blocked_sites', blockedSites);
                }
                
                const localBlocked = JSON.parse(localStorage.getItem('blocked_sites') || '[]');
                if (!localBlocked.includes(location.href)) {
                    localBlocked.push(location.href);
                    localStorage.setItem('blocked_sites', JSON.stringify(localBlocked));
                }
                
                // 举报到12321
                reportTo12321(location.href);
                
                // 立即跳转
                window.location.href = 'about:blank';
                
                // 显示操作结果
                GM_notification({
                    text: `已屏蔽并举报: ${location.href}`,
                    title: '操作成功',
                    timeout: 3000
                });
            }
        });

        // 隐藏按钮
        const hideBtn = document.createElement('div');
        hideBtn.textContent = '隐藏';
        hideBtn.style.padding = '8px 15px';
        hideBtn.style.fontSize = '12px';
        hideBtn.style.color = 'black';  // 添加黑色字体
        hideBtn.style.cursor = 'pointer';
        // 修改隐藏按钮的点击事件
        hideBtn.addEventListener('click', function() {
            // 将当前网站域名加入隐藏列表
            const hiddenSites = GM_getValue('hidden_sites', []);
            if (!hiddenSites.includes(location.hostname)) {
                hiddenSites.push(location.hostname);
                GM_setValue('hidden_sites', hiddenSites);
            }
            container.style.display = 'none';
        });

        // 组装菜单
        menu.appendChild(reportBtn);
        menu.appendChild(hideBtn);
        container.appendChild(menu);
        container.appendChild(mainBtn);

        // 点击主按钮切换菜单显示
        mainBtn.addEventListener('click', function() {
            menu.style.display = menu.style.display === 'none' ? 'block' : 'none';
        });

        // 点击页面其他区域关闭菜单
        document.addEventListener('click', function(e) {
            if (!container.contains(e.target)) {
                menu.style.display = 'none';
            }
        });

        document.body.appendChild(container);
    }

    // 初始化
    checkBlocked();
    createBlockButton();
})();