// ==UserScript== // @name 粉笔错题页草稿纸 // @namespace https://www.fenbi.com/ // @version 1.0.0 // @description 给粉笔网页版错题板块的每道题加一张手写草稿纸:题干和 ABCD 选项都能直接落笔,笔迹自动存在本机,切题、刷新都不丢。顶部自带 ABCD 字母盒,写字时不会误选到选项;每题一套撤销 / 重做 / 清空 / 橡皮。资料分析自动左右分栏铺满一屏:左材料、右题目,中间分隔条可拖动,另有字号滑块与一屏缩放。支持 Apple Pencil——笔能写、手掌防误触,两侧空白照常滚动。 // @match *://www.fenbi.com/spa/* // @grant none // @run-at document-idle // ==/UserScript== (function () { 'use strict'; var GUARD = '__FBP_PRACTICE_DRAFT__'; var VER = '1.0.0'; // 旧版用布尔锁,会把重新注入的新实例直接挡掉(升级后必须手动刷新的根因)。 // 改为:若页面已有旧实例,先调用它的 destroy 彻底清理,再接管。 var __old = window[GUARD]; if (__old) { if (typeof __old.destroy === 'function') { try { __old.destroy(); } catch (e) {} } else if (window.__fbppDraft && typeof window.__fbppDraft.destroy === 'function') { try { window.__fbppDraft.destroy(); } catch (e) {} } } // 先占锁再初始化,防止两个实例并发都进到 start window[GUARD] = { version: VER, destroy: function () {} }; // 统一收集本实例的定时器/监听器/history hook,destroy 时全部撤销,避免新旧实例打架 var CLEAN = { timers: [], listeners: [], histHooks: [] }; function ownInterval(fn, ms) { var id = setInterval(fn, ms); CLEAN.timers.push(id); return id; } function ownTimeout(fn, ms) { var id = setTimeout(fn, ms); CLEAN.timers.push(id); return id; } function ownOn(target, type, fn, opt) { target.addEventListener(type, fn, opt); CLEAN.listeners.push([target, type, fn, opt]); } /* ============ 0. 选择器 & 配置 ============ */ // 练习页有两套题目渲染,必须都支持: // A. 已作答/解析态:div.ti-container(老 DOM,选项是 li.choice-radio) // B. 未作答态:fb-local-question(Angular 新组件,选项是 li.theme-ques-option) var SEL = { // A 套(ti-container) ti: 'div.ti-container', material: 'div.ti-container.fb-material-block', content: 'div.ti-content', question: 'article.question-content', li: 'li.choice-radio', label: 'label.choice-radio-label', titleIndex: '.title-index', solution: '.fb-solution-block', // B 套(fb-local-question) fbQ: 'fb-local-question', fbContent: 'fb-ng-choice-question', fbQWrap: '.ques-options-dry', fbQuestion: 'article.question-content', fbLi: 'li.theme-ques-option', fbLabel: 'label.fb-label', // B 套作答/解析态:fb-ng-choice-question 被换成 app-fb-practice-solution fbSol: 'app-fb-practice-solution', fbSolWrap: '.fb-solution-sub', fbSolQ: '.question-content', fbSolLi: 'ul.options > li' }; // 两套画布/工具条共用同一批 class,靠 host 的 fbpp-host 定位,无需额外区分。 /* 适配器:封装两套 DOM 的差异(找选项、判分、题号、下一题) */ var ADP = { ti: { root: SEL.ti, li: SEL.li, label: SEL.label, qWrap: SEL.content, question: SEL.question, idx: function (ti) { var e = ti.querySelector(SEL.titleIndex); return e ? e.textContent.trim() : ''; }, selCls: /\bselected\b/, rightCls: /\bright\b/, wrongCls: /\bwrong\b/, next: function (ti) { var n = ti.nextElementSibling; while (n && (!n.matches || !n.matches(SEL.ti))) n = n && n.nextElementSibling; return n; } }, fb: { root: SEL.fbQ, li: SEL.fbLi, label: SEL.fbLabel, qWrap: SEL.fbQWrap, question: SEL.fbQuestion, idx: function (q) { var e = q.querySelector('.options-title'); return e ? e.textContent.trim().replace(/[^\d]/g, '') : ''; }, // fb 套判分:选中态在 label 的 input:checked;对错态在 li 的类名 right/wrong 或 fb-radioInput 的类 selCls: null, rightCls: /\bright\b/, wrongCls: /\bwrong\b/, checked: function (li) { var r = li.querySelector('input[type=radio]'); return !!(r && (r.checked || r.hasAttribute('checked'))); }, next: function (q) { var n = q.nextElementSibling; while (n && n.tagName !== 'FB-LOCAL-QUESTION') n = n && n.nextElementSibling; return n; } }, // 作答/解析态:选项在 ul.options>li(裸 li),题干用 .question-content 兼容 article/div fbSol: { root: SEL.fbQ, li: SEL.fbSolLi, label: SEL.fbLabel, qWrap: SEL.fbSolWrap, question: SEL.fbSolQ, idx: function (q) { var e = q.querySelector('.options-title'); return e ? e.textContent.trim().replace(/[^\d]/g, '') : ''; }, selCls: null, rightCls: /\bright-options\b/, wrongCls: /\berror-options\b/, checked: function (li) { var r = li.querySelector('input[type=radio]'); return !!(r && (r.checked || r.hasAttribute('checked'))); }, next: function (q) { return null; } } }; var LS_PREFIX = 'fbpp-cv-'; // 笔迹 var BRUSH_KEY = 'fbpp-brush'; // 笔刷配置 var MAX_STROKES = 300; var MAX_POINTS = 1200; var SAVE_DELAY = 400; var brushColor = '#000000'; var brushWidth = 1; try { var sb = JSON.parse(localStorage.getItem(BRUSH_KEY) || 'null'); if (sb) { if (typeof sb.color === 'string' && /^#[0-9a-f]{6}$/i.test(sb.color)) brushColor = sb.color; if (typeof sb.width === 'number' && isFinite(sb.width)) brushWidth = Math.min(4, Math.max(0.5, sb.width)); } } catch (e) {} function saveBrush() { try { localStorage.setItem(BRUSH_KEY, JSON.stringify({ color: brushColor, width: brushWidth })); } catch (e) {} } /* ============ 1. 工具函数 ============ */ function hashStr(s) { var h = 2166136261 >>> 0; s = String(s || ''); for (var i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 16777619); } return (h >>> 0).toString(36); } function cssText(el, text) { el.setAttribute('style', text); } function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) { return { '&': '&', '<': '<', '>': '>', '"': '"' }[c]; }); } /* ============ 2. 样式(具名