// ==UserScript== // @name Cookie 管理工具 // @namespace cookie // @version 1.1 // @description 一款非常实用的网站cookie管理工具,支持增加或删除特定的cookie // @author wangshiwei // @license MIT // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @grant GM_registerMenuCommand // @grant GM_addStyle // @grant GM_notification // @grant GM_log // @run-at document-start // ==/UserScript== (function() { 'use strict'; // 添加调试信息 console.log('Cookie Tool Script Loaded on:', window.location.href); // 样式 GM_addStyle(` .cookie-dialog { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); z-index: 9999; width: 400px; font-family: Arial, sans-serif; } .cookie-dialog h3 { margin-top: 0; color: #333; border-bottom: 1px solid #eee; padding-bottom: 10px; } .tab-container { margin-bottom: 20px; } .tab-buttons { display: flex; border-bottom: 1px solid #ddd; margin-bottom: 20px; } .tab-button { flex: 1; padding: 10px 15px; background: none; border: none; cursor: pointer; font-size: 14px; font-weight: bold; color: #666; border-bottom: 2px solid transparent; transition: all 0.3s ease; } .tab-button.active { color: #4285f4; border-bottom-color: #4285f4; } .tab-button:hover { color: #4285f4; background-color: #f5f5f5; } .tab-content { display: none; } .tab-content.active { display: block; } .form-group { margin-bottom: 15px; } .form-group label { display: block; margin-bottom: 5px; font-weight: bold; color: #555; } .form-group input, .form-group select { width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; } .button-group { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; } .button { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; font-weight: bold; } .primary-button { background-color: #4285f4; color: white; } .secondary-button { background-color: #f1f1f1; color: #333; } .danger-button { background-color: #dc3545; color: white; } .cookie-list { max-height: 200px; overflow-y: auto; margin-top: 15px; border: 1px solid #eee; border-radius: 4px; padding: 10px; } .cookie-item { display: flex; justify-content: space-between; align-items: center; padding: 5px 0; border-bottom: 1px solid #f1f1f1; } .cookie-item:last-child { border-bottom: none; } .cookie-item .cookie-info { flex: 1; min-width: 0; margin-right: 10px; } .cookie-item .cookie-key { font-weight: bold; color: #333; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 150px; } .cookie-item .cookie-value { color: #666; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 150px; } .cookie-item .cookie-actions { display: flex; gap: 5px; flex-shrink: 0; } .cookie-item button { background: none; border: none; cursor: pointer; font-size: 12px; padding: 2px 6px; border-radius: 3px; transition: background-color 0.2s; } .cookie-item button:hover { background-color: #f0f0f0; } .cookie-item button.set-cookie { color: #4285f4; } .cookie-item button.delete-cookie { color: #dc3545; } .domain-info { font-size: 12px; color: #666; margin-bottom: 15px; } .cookie-trigger { position: fixed; bottom: 20px; right: 20px; z-index: 9998; padding: 8px 16px; background-color: #4285f4; color: white; border: none; border-radius: 4px; cursor: pointer; font-weight: bold; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); user-select: none; } .cookie-trigger:hover { background-color: #3367d6; } .cookie-trigger.dragging { opacity: 0.8; } .expiry-info { font-size: 12px; color: #666; margin-top: 5px; } .current-cookies { max-height: 200px; overflow-y: auto; margin-top: 15px; border: 1px solid #eee; border-radius: 4px; padding: 10px; } .current-cookie-item { display: flex; justify-content: space-between; align-items: center; padding: 5px 0; border-bottom: 1px solid #f1f1f1; } .current-cookie-item:last-child { border-bottom: none; } .current-cookie-item .cookie-info { flex: 1; min-width: 0; margin-right: 10px; } .current-cookie-item .cookie-key { font-weight: bold; color: #333; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 150px; } .current-cookie-item .cookie-value { color: #666; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 150px; } .current-cookie-item .cookie-actions { display: flex; gap: 5px; flex-shrink: 0; } .current-cookie-item button { background: none; border: none; color: #dc3545; cursor: pointer; font-size: 12px; padding: 2px 6px; border-radius: 3px; transition: background-color 0.2s; } .current-cookie-item button:hover { background-color: #f0f0f0; } .security-warning { background-color: #fff3cd; border: 1px solid #ffeaa7; color: #856404; padding: 10px; border-radius: 4px; margin: 10px 0; font-size: 12px; } .cookie-details { font-size: 11px; color: #666; margin-top: 5px; } `); // 存储键前缀 const STORAGE_PREFIX = 'cookie_tool_'; const POSITION_KEY = 'cookie_tool_position'; const DEFAULT_EXPIRY_DAYS = 7; // 默认过期时间7天 // 过期时间选项 const EXPIRY_OPTIONS = [ { value: 1, label: "1天" }, { value: 7, label: "7天" }, { value: 30, label: "30天" }, { value: 365, label: "1年" }, { value: 3650, label: "10年" }, { value: -1, label: "会话Cookie(关闭浏览器失效)" } ]; // 获取当前域名 const currentDomain = window.location.hostname; // 获取当前页面的所有Cookie(包含详细信息) function getCurrentCookies() { const cookies = document.cookie.split(';'); const cookieList = []; cookies.forEach(cookie => { const trimmed = cookie.trim(); if (trimmed) { const [key, value] = trimmed.split('='); if (key && value) { cookieList.push({ key: key.trim(), value: value.trim(), // 尝试获取更多信息 canDelete: true // 默认假设可以删除 }); } } }); return cookieList; } // 检查Cookie是否可以被JavaScript删除 function checkCookieDeletability(key) { try { // 尝试设置一个测试Cookie document.cookie = `test_${key}=test; path=/`; const canSet = document.cookie.includes(`test_${key}=test`); if (canSet) { // 尝试删除测试Cookie document.cookie = `test_${key}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`; const canDelete = !document.cookie.includes(`test_${key}=`); return canDelete; } return false; } catch (e) { console.warn('Cookie deletability check failed:', e); return false; } } // 增强的Cookie删除函数 function deleteCookie(key) { console.log('Attempting to delete cookie:', key); // 获取当前页面的所有Cookie const currentCookies = getCurrentCookies(); const targetCookie = currentCookies.find(c => c.key === key); if (!targetCookie) { console.log('Cookie not found in current page:', key); return Promise.resolve(false); } console.log('Found cookie to delete:', targetCookie); // 检查Cookie是否可以被删除 const canDelete = checkCookieDeletability(key); if (!canDelete) { console.warn('Cookie may be protected by browser security policies:', key); } // 使用多种方式删除Cookie const deletionMethods = [ // 方法1: 标准删除 - 设置过期时间为过去 () => { const pastDate = new Date(0).toUTCString(); document.cookie = `${key}=; expires=${pastDate}; path=/`; document.cookie = `${key}=; expires=${pastDate}; path=/; domain=${currentDomain}`; document.cookie = `${key}=; expires=${pastDate}; path=/; domain=.${currentDomain}`; }, // 方法2: 设置空值 () => { document.cookie = `${key}=; path=/`; document.cookie = `${key}=; path=/; domain=${currentDomain}`; document.cookie = `${key}=; path=/; domain=.${currentDomain}`; }, // 方法3: 使用昨天的时间 () => { const yesterday = new Date(); yesterday.setTime(yesterday.getTime() - 24 * 60 * 60 * 1000); const expires = yesterday.toUTCString(); document.cookie = `${key}=; expires=${expires}; path=/`; document.cookie = `${key}=; expires=${expires}; path=/; domain=${currentDomain}`; document.cookie = `${key}=; expires=${expires}; path=/; domain=.${currentDomain}`; }, // 方法4: 尝试不同的path组合 () => { const pastDate = new Date(0).toUTCString(); const paths = ['/', '/path/', window.location.pathname, window.location.pathname + '/']; const domains = [currentDomain, '.' + currentDomain, '']; paths.forEach(path => { domains.forEach(domain => { const domainStr = domain ? `; domain=${domain}` : ''; document.cookie = `${key}=; expires=${pastDate}; path=${path}${domainStr}`; }); }); }, // 方法5: 尝试设置SameSite属性 () => { const pastDate = new Date(0).toUTCString(); document.cookie = `${key}=; expires=${pastDate}; path=/; SameSite=Lax`; document.cookie = `${key}=; expires=${pastDate}; path=/; SameSite=Strict`; document.cookie = `${key}=; expires=${pastDate}; path=/; SameSite=None`; } ]; // 执行所有删除方法 deletionMethods.forEach((method, index) => { try { console.log(`Trying deletion method ${index + 1}`); method(); } catch (e) { console.warn(`Deletion method ${index + 1} failed:`, e); } }); // 等待一段时间后验证删除结果 return new Promise((resolve) => { setTimeout(() => { const cookies = document.cookie.split(';'); const stillExists = cookies.some(cookie => { const trimmed = cookie.trim(); return trimmed.startsWith(key + '=') && trimmed.split('=')[1]; }); if (!stillExists) { console.log('Cookie successfully deleted:', key); resolve(true); } else { console.warn('Cookie deletion failed or still exists:', key); console.log('Current cookies after deletion attempt:', document.cookie); resolve(false); } }, 500); // 增加等待时间 }); } // 截断文本 function truncateText(text, maxLength = 18) { if (text.length <= maxLength) return text; return text.substring(0, maxLength) + '...'; } // 显示对话框 function showCookieDialog() { console.log('Opening cookie dialog for domain:', currentDomain); // 如果已经存在对话框,则先移除 const existingDialog = document.querySelector('.cookie-dialog'); if (existingDialog) { existingDialog.remove(); } // 创建对话框元素 const dialog = document.createElement('div'); dialog.className = 'cookie-dialog'; // 获取已保存的cookie const savedCookies = GM_getValue(`${STORAGE_PREFIX}${currentDomain}`, []); const currentCookies = getCurrentCookies(); // 生成过期时间选项 const expiryOptionsHTML = EXPIRY_OPTIONS.map(option => `` ).join(''); dialog.innerHTML = `
当前页面没有Cookie
'}