基于Tampermonkey实现Chrome按键手势:替代FireGestures旧功能求助
Hey there! Let's tackle your Tampermonkey script issues one by one and build a solution that matches your old FireGestures workflow. Here's a revised script that addresses all your requirements:
Complete Revised Script
// ==UserScript== // @name Ctrl+Right Click Hover Link Batch Opener // @namespace http://tampermonkey.net/ // @version 0.1 // @description Batch open hovered links in background tabs when holding Ctrl+Right Click, mimicking old FireGestures behavior // @author Your Name // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // Configuration const LINK_SELECT_DELAY = 150; // Short delay to avoid accidental link selection // State tracking let isActive = false; // Whether Ctrl+Right Click is held let selectedLinks = new Set(); // Store unique links to open let hoverTimer = null; let currentHoveredLink = null; // Block default right-click menu when in active mode document.addEventListener('contextmenu', (e) => { if (e.ctrlKey && isActive) { e.preventDefault(); } }); // Activate selection mode when Ctrl+Right Click is pressed document.addEventListener('mousedown', (e) => { if (e.button === 2 && e.ctrlKey) { isActive = true; selectedLinks.clear(); // Reset selection for new session console.log('Activated: Hover links to select, release right click to open'); } }); // Deactivate and open all selected links when right click is released document.addEventListener('mouseup', (e) => { if (e.button === 2 && isActive) { isActive = false; clearTimeout(hoverTimer); // Open all selected links via middle-click (avoids popup blockers) selectedLinks.forEach(link => { const middleClick = new MouseEvent('click', { button: 1, bubbles: true, cancelable: true, view: window }); link.dispatchEvent(middleClick); }); console.log(`Opened ${selectedLinks.size} links in background tabs`); selectedLinks.clear(); currentHoveredLink = null; } }); // Handle link hover selection when active document.addEventListener('mouseover', (e) => { if (!isActive) return; const link = e.target.closest('a'); if (!link || !link.href) return; clearTimeout(hoverTimer); hoverTimer = setTimeout(() => { if (isActive && link !== currentHoveredLink) { currentHoveredLink = link; selectedLinks.add(link); // Optional visual feedback: temporary blue outline link.style.outline = '2px solid #2196F3'; setTimeout(() => link.style.outline = '', 300); } }, LINK_SELECT_DELAY); }); // Clear hover timer when mouse leaves a link document.addEventListener('mouseout', (e) => { if (!isActive) return; clearTimeout(hoverTimer); currentHoveredLink = null; }); // Deactivate early if Ctrl key is released first document.addEventListener('keyup', (e) => { if (e.key === 'Control' && isActive) { isActive = false; clearTimeout(hoverTimer); selectedLinks.clear(); currentHoveredLink = null; console.log('Deactivated early (Ctrl released)'); } }); })();
Key Fixes & Walkthrough
1. Ctrl+Right Click Activation
- We trigger the selection mode only when you hold Ctrl + Right Mouse Button (listens for
mousedownon right button +ctrlKeycheck) - Added safeguards to deactivate if you release Ctrl early, and block the default right-click menu while active to avoid distractions
2. Bypass Chrome Popup Blockers
- Instead of using
target="_blank"+click(), we simulate a middle mouse button click (button: 1). Browsers treat middle-click opens as explicit user actions, so they rarely get blocked by popup blockers - This also automatically opens links in background tabs (the default behavior for middle-click in Chrome/Firefox)
3. Batch Link Selection & Opening
- Used a
Setto track unique links, so hovering the same link multiple times won't open duplicates - Added a short delay (
LINK_SELECT_DELAY) before marking a link as selected, preventing accidental picks when quickly moving your mouse - Optional visual feedback (a temporary blue outline) confirms which links you've selected
- When you release the right click, all selected links open in background tabs at once
How to Use
- Install this script in Tampermonkey
- Hold Ctrl and press + hold the Right Mouse Button
- Hover over any links you want to open – each will get a brief blue outline to confirm selection
- Release the Right Mouse Button (or Ctrl) to open all selected links in background tabs
内容的提问来源于stack exchange,提问作者Daniel Sachs
相关产品推荐
相关产品推荐

