// ==UserScript== // @name 1A2X 快捷键 // @namespace https://1a2x.com/ // @version 1.0.0 // @description 只剩 5 个快捷键:? 打开/关闭小面板、g m 私信、g n 消息通知、n 发布帖子、Ctrl+Enter 发布。右下角有个 ? 图标,点开是小面板,只写这 5 个。别的全删了。注:1a2x 的 CSP 是 style-src 'self',会拦截内联样式,所以这版一个内联样式都不用,全部用 CSSStyleSheet + adoptedStyleSheets 下发。发帖页提交按钮的文字就是“发布”(type=submit),不是“发布帖子”,Ctrl+Enter 按这个找。 // @author littlecrab // @license MIT // @homepageURL https://1a2x.com/ // @downloadURL https://raw.githubusercontent.com/ZEROPUFF/1a2x-shortcuts/main/1a2x-shortcuts.user.js // @updateURL https://raw.githubusercontent.com/ZEROPUFF/1a2x-shortcuts/main/1a2x-shortcuts.user.js // @match https://1a2x.com/* // @grant none // @run-at document-idle // ==/UserScript== (function () { 'use strict'; const isTyping = () => { const el = document.activeElement; return el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT' || el.isContentEditable); }; const isVisible = (el) => !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length); const findButton = (text, root) => [...(root || document).querySelectorAll('button')] .find(b => b.textContent.trim().includes(text) && isVisible(b)); // 在一个表单里找提交按钮:先找 type=submit 的可见按钮,再找文字正好是“发布”的, // 再找“发表回复”。发帖页那个按钮就是 , // 文字只有“发布”两个字,之前按“发布帖子”找是找不到的。 const submitIn = (form) => { if (!form) return null; const btns = [...form.querySelectorAll('button')].filter(isVisible); return btns.find(b => b.type === 'submit') || btns.find(b => b.textContent.trim() === '发布') || btns.find(b => b.textContent.trim().includes('发表回复')) || null; }; // ── 样式:不用内联样式标签、不用内联属性、不用 JS 直接改样式 ────────── // 1a2x 的 CSP 是 style-src 'self',上面那些写法都会被拦,面板才会没底色、 // 挤到左上角、滚不动。Constructed stylesheet(CSSStyleSheet + adoptedStyleSheets) // 是 JS API 下发的,不走内联样式那条 CSP 规则。 const SHADOW_CSS = '*{box-sizing:border-box;margin:0;padding:0}' + 'button{font-family:inherit;cursor:pointer}' + '#a2x-fab{position:fixed;right:16px;bottom:16px;z-index:2147483647;pointer-events:auto;width:44px;height:44px;border-radius:50%;border:1.5px solid #9ca3af;background:#111827;color:#fff;font-size:22px;font-weight:800;line-height:1;box-shadow:0 8px 24px rgba(0,0,0,.35)}' + '#a2x-overlay{position:fixed;left:0;top:0;right:0;bottom:0;z-index:2147483647;pointer-events:auto;display:flex;align-items:center;justify-content:center;padding:16px;background:rgba(0,0,0,.55)}' + '#a2x-overlay[hidden]{display:none}' + '#a2x-card{display:block;width:360px;max-width:100%;background:#fff;color:#111827;border-radius:14px;padding:20px 22px;box-shadow:0 24px 64px rgba(0,0,0,.45);font-family:system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;font-size:16px;line-height:1.5;text-align:left}' + '.a2x-headrow{display:flex;align-items:flex-start;gap:12px}' + '.a2x-flex1{flex:1}' + '.a2x-title{font-size:18px;font-weight:900}' + '.a2x-close{flex:none;font-size:15px;font-weight:700;padding:6px 12px;border-radius:8px;border:1.5px solid #e5e7eb;background:#f9fafb;color:#111827}' + '.a2x-row{display:flex;align-items:center;gap:12px;padding:10px 2px;border-bottom:1px solid #e5e7eb}' + '.a2x-row:last-child{border-bottom:none}' + '.a2x-kbd{min-width:52px;text-align:center;padding:5px 8px;border-radius:6px;border:1.5px solid #9ca3af;background:#f3f4f6;color:#111827;font-weight:800;font-size:14px;font-family:ui-monospace,monospace;white-space:nowrap}' + '.a2x-label{flex:1;font-size:16px;font-weight:700}' + '.a2x-note{font-size:13px;color:#6b7280;margin-top:12px}'; function adopt(target, css) { try { const sheet = new CSSStyleSheet(); sheet.replaceSync(css); target.adoptedStyleSheets = [...target.adoptedStyleSheets, sheet]; return true; } catch (e) { return false; } } // ── ? 小面板 ── let shadow = null; function ensureShadow() { if (shadow) return shadow; const host = document.createElement('div'); host.id = 'a2x-host'; // host 本身的定位也走 adoptedStyleSheets,不写内联样式 adopt(document, '#a2x-host{position:fixed;left:0;top:0;width:0;height:0;z-index:2147483647;pointer-events:none}'); shadow = host.attachShadow({ mode: 'open' }); adopt(shadow, SHADOW_CSS); shadow.innerHTML = '' + ''; document.body.appendChild(host); shadow.getElementById('a2x-fab').addEventListener('click', openPanel); shadow.getElementById('a2x-close').addEventListener('click', closePanel); shadow.getElementById('a2x-overlay').addEventListener('click', (e) => { if (e.target.id === 'a2x-overlay') closePanel(); }); return shadow; } function panelOpen() { ensureShadow(); return !shadow.getElementById('a2x-overlay').hidden; } function openPanel() { ensureShadow(); shadow.getElementById('a2x-overlay').hidden = false; } function closePanel() { if (!shadow) return; shadow.getElementById('a2x-overlay').hidden = true; } function publish() { // 1) 先提交正在输入的那个表单:你在主回帖框里写,就发主回复; // 你在楼中楼(thread-body-xxxx,在这一楼继续交流)里写,就发那一楼。 // 绝不在全页找第一个匹配按钮,藏起来的楼中楼表单里有个空的必填项, // 点到它浏览器就报 “An invalid form control … is not focusable”。 const active = document.activeElement; if (active && active.closest) { const btn = submitIn(active.closest('form')); if (btn) { btn.click(); return; } } // 2) 没在输入框里,才找主表单。发帖页没有 #reply-form, // 主表单里那个提交按钮文字就是“发布”,按这个找。 if (location.pathname === '/new') { const btn = [...document.querySelectorAll('button')] .find(b => isVisible(b) && b.textContent.trim() === '发布') || [...document.querySelectorAll('button')] .find(b => isVisible(b) && b.type === 'submit'); if (btn) btn.click(); } else if (/^\/t\/\d+/.test(location.pathname)) { const btn = submitIn(document.querySelector('#reply-form')) || findButton('发表回复'); if (btn) btn.click(); } } // 按 ? 弹出面板:e.key 是 '?' 最常见,但有的键盘布局下 Shift+/ 只给 '/', // 再加 e.code 是 Slash 且按着 Shift 的情况一起认,免得按了没反应。 const isHelpKey = (e) => e.key === '?' || (e.shiftKey && (e.key === '/' || e.code === 'Slash')); let pendingG = false, gTimer = null; document.addEventListener('keydown', (e) => { if (e.ctrlKey && !e.metaKey && !e.altKey && e.key === 'Enter') { if (location.pathname === '/new' || /^\/t\/\d+/.test(location.pathname)) { e.preventDefault(); publish(); } return; } if (e.key === 'Escape') { if (panelOpen()) { closePanel(); return; } if (isTyping()) document.activeElement.blur(); return; } if (e.metaKey || e.ctrlKey || e.altKey) return; if (panelOpen()) { if (isHelpKey(e)) { e.preventDefault(); closePanel(); } return; } if (pendingG && !isTyping()) { pendingG = false; clearTimeout(gTimer); if (e.key === 'm') { e.preventDefault(); location.href = '/messages'; return; } if (e.key === 'n') { e.preventDefault(); location.href = '/notifications'; return; } } if (isTyping()) return; if (isHelpKey(e)) { e.preventDefault(); openPanel(); return; } if (e.key === 'g') { pendingG = true; clearTimeout(gTimer); gTimer = setTimeout(() => { pendingG = false; }, 900); e.preventDefault(); return; } if (e.key === 'n') { e.preventDefault(); const btn = findButton('发布帖子'); if (btn && /^\/b\//.test(location.pathname)) btn.click(); else location.href = '/new'; } }, true); ensureShadow(); })();