// ==UserScript== // @name 批注弹窗快捷键 // @namespace http://tampermonkey.net/ // @version 1.3 // @description 按1/2/3/4自动打开工具栏并切换四个工具按钮 // @match *://*/pointcloud/* // @grant none // @run-at document-idle // ==/UserScript== (function () { 'use strict'; function findToolbarButtons() { let containers = document.querySelectorAll('.el-button-group.top-item'); for (let c of containers) { let style = window.getComputedStyle(c); if (style.display === 'none') continue; let btns = c.querySelectorAll(':scope > button.el-button, :scope > .el-button'); let visible = []; btns.forEach((b) => { let r = b.getBoundingClientRect(); if (r.width > 0 && r.height > 0) visible.push(b); }); if (visible.length === 4) { visible.sort((a, b) => a.getBoundingClientRect().x - b.getBoundingClientRect().x); return visible; } } let allTopBtns = document.querySelectorAll('button.button-theme-top, .el-button.button-theme-top'); let visibleTop = []; allTopBtns.forEach((b) => { let r = b.getBoundingClientRect(); if (r.width > 0 && r.height > 0) visibleTop.push(b); }); let groups = {}; visibleTop.forEach((b) => { let y = Math.round(b.getBoundingClientRect().y / 5) * 5; if (!groups[y]) groups[y] = []; groups[y].push(b); }); for (let y in groups) { if (groups[y].length === 4) { groups[y].sort((a, b) => a.getBoundingClientRect().x - b.getBoundingClientRect().x); return groups[y]; } } return []; } function findPenButton() { let allBtns = document.querySelectorAll('button.el-button, .el-button'); for (let b of allBtns) { let r = b.getBoundingClientRect(); if (r.width === 0 || r.height === 0) continue; let svg = b.querySelector('svg path'); if (!svg) continue; let d = svg.getAttribute('d') || ''; if (d.startsWith('M17.185')) return b; } return null; } let isOpening = false; document.addEventListener('keydown', function (e) { if (e.key !== '1' && e.key !== '2' && e.key !== '3' && e.key !== '4') return; let tag = (e.target.tagName || '').toLowerCase(); if (tag === 'input' || tag === 'textarea' || e.target.isContentEditable) return; let btns = findToolbarButtons(); if (btns.length < 4) { if (isOpening) return; isOpening = true; console.log('[批注快捷键] 工具栏未打开,尝试点击笔按钮...'); let penBtn = findPenButton(); if (penBtn) { console.log('[批注快捷键] 找到笔按钮,点击中...'); penBtn.click(); let retries = 0; let wait = setInterval(function () { retries++; let btns2 = findToolbarButtons(); if (btns2.length >= 4) { clearInterval(wait); isOpening = false; console.log('[批注快捷键] 工具栏已打开,点击第' + e.key + '个按钮'); let idx = parseInt(e.key) - 1; if (btns2[idx]) btns2[idx].click(); } else if (retries > 20) { clearInterval(wait); isOpening = false; console.log('[批注快捷键] 等待工具栏超时,未能打开'); } }, 100); } else { isOpening = false; console.log('[批注快捷键] 未找到笔按钮'); } e.preventDefault(); e.stopImmediatePropagation(); return; } let idx = parseInt(e.key) - 1; let btn = btns[idx]; if (!btn) return; console.log('[批注快捷键] 按下' + e.key + ',点击第' + (idx + 1) + '个按钮'); btn.click(); e.preventDefault(); e.stopImmediatePropagation(); }, true); })();