// ==UserScript==
// @name WELearn - Auto Answer
// @version 9.178
// @description 点击试题页面即答题
// @match https://welearn.sflep.com/*
// @match https://centercourseware.sflep.com/*
// @author Rito
// @grant none
// ==/UserScript==
(function () {
'use strict';
// --- 核心逻辑 (仅在iframe内运行) ---
function runAutoAnswer() {
console.log('[WELearn Auto Answer] Running in iframe:', location.href);
// 0) 先展开,避免未渲染导致找不到
document.querySelectorAll('.white_frame').forEach(frame => {
const toggleBtn = frame.querySelector('[data-itemtype="toggle"], .toggle, .expand');
if (toggleBtn) toggleBtn.click();
frame.style.display = 'block';
frame.style.visibility = 'visible';
});
// 已填标记,避免重复填同一控件
const filledInputs = new Set();
const filledChoices = new Set();
// 1) 按页面顺序遍历每一个 data-solution(每个小问一个)
const nodes = document.querySelectorAll('[data-solution]');
nodes.forEach((node, idx) => {
// 提取答案文本(优先属性)
const ans = node.getAttribute('data-solution')?.trim() || node.textContent.trim();
if (!ans) {
console.log(`第${idx + 1}小问:答案为空,跳过`);
return;
}
// 2) 选择题:data-solution 在
上,直接标记该
if (node.tagName.toLowerCase() === 'li') {
if (!filledChoices.has(node)) {
node.setAttribute('data-choiced', '');
// 可选:触发点击以激活平台事件
try { node.click(); } catch { }
filledChoices.add(node);
}
console.log(`第${idx + 1}小问(选择题)已选中:${node.textContent.trim()}`);
return;
}
// 3) 填空题:data-solution 不在 上,需要只填入当前小问的控件
// 3.1 找到当前小问的最近容器(避免作用到整题)
const subContainer =
node.closest('[data-itemtype="blank"], [data-itemtype="options"], [data-controltype]') ||
node.parentElement;
// 3.2 在该小问容器内寻找尚未填过的输入控件(input/textarea/contenteditable)
const target =
subContainer.querySelector('input:not([data-filled]), textarea:not([data-filled]), [contenteditable]:not([data-filled])')
// 如果上面没找到,退一步:先找跟 node 最近的一个输入控件
|| node.closest('.white_frame')?.querySelector('input:not([data-filled]), textarea:not([data-filled]), [contenteditable]:not([data-filled])');
if (!target) {
console.log(`第${idx + 1}小问:未找到可填入的控件,答案=${ans}`);
return;
}
// 3.3 精准填入当前控件,并标记避免重复
if ('value' in target) target.value = ans;
else target.textContent = ans;
target.setAttribute('data-filled', '');
filledInputs.add(target);
// 可选:触发事件让平台感知输入变化
try {
target.dispatchEvent(new Event('input', { bubbles: true }));
target.dispatchEvent(new Event('change', { bubbles: true }));
target.dispatchEvent(new Event('blur', { bubbles: true }));
} catch { }
console.log(`第${idx + 1}小问(填空题)已填入:${ans}`);
});
console.log('完成:按小问粒度填入,已避免整题串填。');
}
// --- 脚本入口 ---
if (window.top !== window) {
// 在 iframe 中,直接执行核心逻辑
runAutoAnswer();
} else {
// 在顶层窗口中,只输出提示
console.log('[WELearn Auto Answer] Script loaded in top frame. Answering is active in iframes.');
}
})();