// ==UserScript== // @name 一介平凡法填空 // @namespace local.hn12348.quiz // @version 1.1.1 // @description 一介平凡法填空:湖南法网普法读本自动答题,内置《九五普法导读(一)/(二)》和《怀化市2026年普法读本》答案,按题目 ID 自动填写并提交;全部完成后自动跳转到学法中心查看学习进度;带状态面板与校验。 // @match https://hn.12348.gov.cn/fxmain/subpage/legalpublicity/fx_regulations.html* // @run-at document-idle // @noframes // @tag 湖南法网 // @tag 自动答题 // @tag 普法读本 // ==/UserScript== (() => { 'use strict'; const COURSES = { "2109476694548021248": { "name": "2026年度《九五普法导读》(一)", "answers": { "2109477466614530048": "D", "2109541928746876928": "ABCD", "2109542078278008832": "A", "2109542399989514240": "ABC", "2109542689987887104": "ABC", "2109542863049064448": "AB", "2109543174543245312": "ABCD", "2109543476004651009": "ABC", "2109543705999310848": "D", "2109485464472780800": "A", "2109485806027538432": "AB", "2109486146315616256": "ABCD", "2109486425211666432": "ABC", "2109486798043348992": "ABC", "2109487036183347200": "C", "2109487392644661248": "B", "2109487996947398656": "D", "2109488231316717568": "B", "2109488657504141312": "ABCD", "2109478960910827520": "A", "2109479206516686848": "B", "2109479994743848960": "C", "2109480225216659456": "A", "2109480515814817792": "B", "2109480744005926912": "A", "2109481101226409984": "B", "2109481753910444032": "C", "2109482638367522816": "AB", "2109482927397011456": "B", "2109483349448851456": "ABC", "2109483732258783232": "AB", "2109537044685389824": "A", "2109537252664147968": "A", "2109537474299559936": "A", "2109537644152094720": "B", "2109537785332367360": "C", "2109537940425146368": "B", "2109538100370735104": "B", "2109538329732055040": "AC", "2109544623725608960": "ABC", "2109544865917304832": "B", "2109545239369744384": "AC", "2109545430965551104": "A", "2109545601250099200": "A", "2109545896092893184": "AB", "2109546173055369216": "B", "2109546364756033536": "BC", "2109539668516143104": "A", "2109540008632254464": "AB", "2109540205886177280": "AB", "2109540428742131712": "A", "2109540874953162752": "ABC", "2109490064072376320": "D", "2109490267793915904": "B", "2109490628965433344": "ABC", "2109491238066454528": "A", "2109491575720509440": "ABCD", "2109535241101115392": "B", "2109535527836319744": "A", "2109535863451942912": "A" } }, "2110166536785231872": { "name": "2026年度《九五普法导读》(二)", "answers": { "2109547296080265216": "A", "2109547461944016896": "B", "2109547618928427008": "A", "2109547762528813056": "B", "2109547977780494336": "B", "2109548252603875328": "AB", "2109548926561419264": "A", "2109549127292420096": "C", "2109554370042396672": "A", "2109554614037643264": "C", "2109555030762717184": "C", "2109555280650960896": "ABC", "2109555530992189440": "A", "2109555753718120448": "B", "2109556147093504000": "ABC", "2109556400936976384": "AB", "2109557438385487872": "A", "2109557573077172224": "B", "2109557717306703872": "C", "2109557906385928192": "A", "2109558267687469056": "A", "2109558423547805696": "A", "2109558653005594624": "B", "2109564317941628928": "B", "2109564604009938944": "ABCD", "2109566020418011136": "ABC", "2109562081962360832": "ABCD", "2109562239949209600": "B", "2109562516890714112": "AB", "2109562691864494080": "A", "2109562980885594112": "ABC", "2109568346474151936": "C", "2109568489730605056": "A", "2109568785517117440": "ABCD", "2109569767651147776": "A", "2109569989081038848": "ABC", "2109570193939234816": "B", "2109567406195081216": "AB", "2109567546440024064": "C", "2109567731253641216": "A" } }, "2115605510341263360": { "name": "怀化市2026年普法读本", "answers": { "2117533300737179648": "B", "2117540866309488640": "B", "2117541875777798144": "B", "2115973699914956800": "B", "2115974608413786112": "ABC", "2115644420652007424": "B", "2115645026179481600": "C", "2115645967716843520": "ABCDE", "2115979077134319616": "A", "2115979890044960768": "A", "2115980446658461696": "CD", "2115981804916703232": "AB", "2115985017724207104": "AC", "2115632040668823552": "C", "2115632616827781120": "ABCD", "2115635878796722176": "B", "2115643475830505472": "B", "2115976431065366528": "A", "2115977077348892672": "A", "2115977644997607424": "B", "2115978160880222208": "ABCD", "2117520339494764544": "C", "2117520837362843648": "A", "2117522282594172928": "A", "2117523566680342528": "ABCD", "2117524787227000832": "ABCDE", "2117524787382190080": "ABCDE", "2115991949805617152": "A", "2115992599608164352": "D", "2115993281237090304": "ABCD", "2115993647693430784": "BC", "2115971020190908416": "C", "2115972120411373568": "D", "2115972857883262976": "ABC", "2116092997257920512": "C", "2116093733018533888": "A", "2116094735880814592": "ABCD", "2116095623903051776": "ABD", "2127337873819566080": "C" } } }; const params = new URLSearchParams(location.search); const objId = params.get('objId') || ''; const course = COURSES[objId] || null; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); let stopped = false; let running = false; // ---------------- 界面 ---------------- const host = document.createElement('div'); host.id = 'hn12348-quiz-panel'; host.style.cssText = 'position:fixed;left:16px;bottom:16px;z-index:2147483647;'; const root = host.attachShadow({ mode: 'open' }); root.innerHTML = `
一介平凡法填空
等待开始
`; document.documentElement.appendChild(host); const startBtn = root.getElementById('start'); const stopBtn = root.getElementById('stop'); const statusEl = root.getElementById('status'); const courseEl = root.getElementById('course'); const countEl = root.getElementById('count'); const logEl = root.getElementById('log'); function setStatus(msg) { statusEl.textContent = msg; } function log(msg) { const line = document.createElement('div'); line.textContent = msg; logEl.appendChild(line); while (logEl.childElementCount > 8) logEl.removeChild(logEl.firstElementChild); } courseEl.textContent = course ? course.name : '当前页面不是已收录的普法读本'; countEl.textContent = course ? '已收录 ' + Object.keys(course.answers).length + ' 题' : ''; if (!course) { startBtn.disabled = true; setStatus('未识别到课程,脚本不会执行'); return; } // ---------------- 答题逻辑 ---------------- function quizInputs() { return [...document.querySelectorAll('form.questions input[type=radio],form.questions input[type=checkbox]')]; } function questionGroups() { const seen = new Set(); const out = []; for (const inp of quizInputs()) { if (seen.has(inp.name)) continue; seen.add(inp.name); out.push({ name: inp.name, inputs: [...document.querySelectorAll(`form.questions input[name="${inp.name}"]`)] }); } return out; } async function waitUntil(fn, timeout) { const end = Date.now() + (timeout || 10000); while (Date.now() < end) { if (stopped) return false; try { if (fn()) return true; } catch (e) {} await sleep(200); } return false; } function checkedLetters(g) { return g.inputs.filter((i) => i.checked).map((i) => i.value).sort().join(''); } async function processSection(anchor, index, total) { const prev = quizInputs()[0]; const prevName = prev ? prev.name : ''; const li = anchor.closest('li'); const wasCurrent = !!(li && li.classList.contains('on')); setStatus(`第 ${index + 1}/${total} 节:加载中…`); anchor.click(); if (!wasCurrent) { await waitUntil(() => li && li.classList.contains('on'), 4000); const changed = await waitUntil(() => { const inp = quizInputs()[0]; return inp && inp.name !== prevName; }, 12000); if (!changed) throw new Error('题目加载超时'); } else { await sleep(350); } if (stopped) throw new Error('已停止'); const gs = questionGroups(); if (!gs.length) throw new Error('没有找到题目'); if (gs.every((g) => g.inputs.every((i) => i.disabled))) { log(`第 ${index + 1} 节:已答过,跳过`); return 'skip'; } for (const g of gs) { if (!course.answers[g.name]) throw new Error(`未收录题目 ${g.name},已停止`); } setStatus(`第 ${index + 1}/${total} 节:填写答案…`); for (const g of gs) { const want = course.answers[g.name]; for (const input of g.inputs) { const should = want.indexOf(input.value) !== -1; if (input.checked !== should) input.click(); } } await sleep(250); for (const g of gs) { const want = course.answers[g.name].split('').sort().join(''); if (checkedLetters(g) !== want) { throw new Error(`第 ${index + 1} 节勾选校验失败,未提交`); } } if (stopped) throw new Error('已停止'); setStatus(`第 ${index + 1}/${total} 节:提交中…`); const btn = document.querySelector('#btn_code'); if (!btn) throw new Error('找不到答题按钮'); btn.click(); const submitted = await waitUntil(() => { const ins = quizInputs(); return ins.length > 0 && ins.every((i) => i.disabled); }, 15000); if (!submitted) throw new Error('提交未完成,已停止'); const falseCount = document.querySelectorAll('form.questions .answerfalse').length; const trueCount = document.querySelectorAll('form.questions .answertrue').length; if (falseCount > 0) { throw new Error(`出现 ${falseCount} 道错题,已停止`); } log(`第 ${index + 1} 节:提交成功(${trueCount} 题)`); return 'ok'; } async function run() { if (running || !course) return; running = true; stopped = false; startBtn.disabled = true; stopBtn.disabled = false; log('开始自动答题'); const anchors = [...document.querySelectorAll('#tree > li > a')]; if (!anchors.length) { setStatus('未找到课程目录'); running = false; startBtn.disabled = false; stopBtn.disabled = true; return; } let okCount = 0; let skipCount = 0; try { for (let i = 0; i < anchors.length; i++) { if (stopped) break; const res = await processSection(anchors[i], i, anchors.length); if (res === 'ok') okCount++; else skipCount++; } if (stopped) { setStatus(`已停止:提交 ${okCount} 节,跳过 ${skipCount} 节`); } else { setStatus(`全部完成:提交 ${okCount} 节,已答过 ${skipCount} 节,3 秒后返回学习进度页…`); log('全部完成,3 秒后跳转学法中心'); setTimeout(() => { location.href = 'https://hn.12348.gov.cn/fxmain/study'; }, 3000); } log('结束'); } catch (e) { const msg = e && e.message ? e.message : String(e); setStatus('出错:' + msg); log('错误:' + msg); } finally { running = false; startBtn.disabled = false; stopBtn.disabled = true; } } startBtn.addEventListener('click', run); stopBtn.addEventListener('click', () => { stopped = true; setStatus('正在停止…'); }); })();