// ==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 = '' + '