1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| // ==UserScript== // @name Gemini 极速删除助手 (安全版+AltQ开关) // @namespace http://tampermonkey.net/ // @version 5.0 // @description 绕过 TrustedHTML 限制,按 Alt+Q 显示删除图标,点击直接删除(无确认)。 // @author Frank Salvio & Gemini // @match https://gemini.google.com/* // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // ==/UserScript==
(function() { 'use strict';
const STORAGE_KEY = 'gemini_quick_del_mode'; let isEnabled = GM_getValue(STORAGE_KEY, false);
// --- 1. 安全创建 SVG 图标 (绕过 innerHTML) --- function createTrashIcon() { const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); svg.setAttribute("viewBox", "0 -960 960 960"); svg.setAttribute("height", "20px"); svg.setAttribute("width", "20px"); svg.setAttribute("fill", "currentColor");
const path = document.createElementNS("http://www.w3.org/2000/svg", "path"); path.setAttribute("d", "M280-120q-33 0-56.5-23.5T200-200v-520h-40v-80h200v-40h240v40h200v80h-40v520q0 33-23.5 56.5T680-120H280Zm400-600H280v520h400v-520ZM360-280h80v-360h-80v360Zm160 0h80v-360h-80v360ZM280-720v520-520Z");
svg.appendChild(path); return svg; }
// --- 2. 注入 CSS (使用 textContent 安全注入) --- const style = document.createElement('style'); style.textContent = ` /* 仅在激活模式下,强制显示操作容器 */ body.del-mode-active .conversation-actions-container { display: flex !important; opacity: 1 !important; visibility: visible !important; padding-right: 4px; }
/* 默认隐藏删除按钮 */ .safe-del-btn { display: none; align-items: center; justify-content: center; width: 28px; height: 28px; cursor: pointer; border-radius: 50%; margin-right: 2px; color: #ea4335; background-color: rgba(234, 67, 53, 0.05); transition: all 0.2s; }
/* 激活模式下显示按钮 */ body.del-mode-active .safe-del-btn { display: flex !important; }
.safe-del-btn:hover { background-color: #ea4335; color: white !important; box-shadow: 0 1px 3px rgba(0,0,0,0.2); } `; document.head.appendChild(style);
// --- 3. 提示框 Toast (DOM 创建) --- function showToast(text, active) { const existing = document.getElementById('g-del-toast'); if (existing) existing.remove();
const toast = document.createElement('div'); toast.id = 'g-del-toast'; toast.textContent = text; Object.assign(toast.style, { position: 'fixed', bottom: '30px', left: '50%', transform: 'translateX(-50%)', background: active ? '#188038' : '#5f6368', color: 'white', padding: '8px 16px', borderRadius: '24px', fontSize: '14px', zIndex: '999999', pointerEvents: 'none', opacity: '0', transition: 'opacity 0.3s' });
document.body.appendChild(toast); requestAnimationFrame(() => toast.style.opacity = '1'); setTimeout(() => { toast.style.opacity = '0'; setTimeout(() => toast.remove(), 300); }, 2000); }
// --- 4. 模式切换 --- function applyMode() { if (isEnabled) { document.body.classList.add('del-mode-active'); } else { document.body.classList.remove('del-mode-active'); } }
function toggleMode() { isEnabled = !isEnabled; GM_setValue(STORAGE_KEY, isEnabled); applyMode(); showToast(isEnabled ? "⚡️ 极速删除模式:ON" : "💤 极速删除模式:OFF", isEnabled); }
// --- 5. 删除逻辑 (无确认弹窗) --- function executeDelete(container) { const menuBtn = container.querySelector('button[data-test-id="actions-menu-button"]'); if (!menuBtn) return;
// 视觉反馈 const btn = container.querySelector('.safe-del-btn'); if(btn) btn.style.opacity = '0.5';
menuBtn.click(); // 打开菜单
// 轮询点击删除 const waiter = setInterval(() => { const delOption = document.querySelector('button[data-test-id="delete-button"]'); if (delOption) { clearInterval(waiter); delOption.click();
// 轮询点击确认 const confirmer = setInterval(() => { const confirmBtn = document.querySelector('button[data-test-id="confirm-button"]'); if (confirmBtn) { clearInterval(confirmer); confirmBtn.click(); console.log('Gemini 删除助手:已删除'); } }, 20); } }, 20);
// 超时清除 setTimeout(() => clearInterval(waiter), 2000); }
// --- 6. 注入按钮 (DOM 操作) --- function inject() { // 查找所有尚未注入的容器 const containers = document.querySelectorAll('.conversation-actions-container:not(.has-safe-del)');
containers.forEach(container => { // 创建按钮容器 const btn = document.createElement('div'); btn.className = 'safe-del-btn'; btn.title = '直接删除 (无确认)';
// 插入 SVG 图标 btn.appendChild(createTrashIcon());
// 绑定点击事件 (无 confirm) btn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); executeDelete(container); });
// 插入到 DOM const menuBtn = container.querySelector('button[data-test-id="actions-menu-button"]'); if (menuBtn) { container.insertBefore(btn, menuBtn); container.classList.add('has-safe-del'); } }); }
// --- 7. 初始化与监听 --- applyMode(); inject();
// 监听 Alt+Q window.addEventListener('keydown', (e) => { if (e.altKey && e.key.toLowerCase() === 'q') { e.preventDefault(); toggleMode(); } });
// 观察页面变化 (动态加载) const observer = new MutationObserver(inject); observer.observe(document.body, { childList: true, subtree: true });
// 菜单命令 GM_registerMenuCommand("切换删除模式 (Alt+Q)", toggleMode); })();
|