// ==UserScript== // @name WELearn - Auto Answer // @version 78.13 // @description 点击试题页面即答题 // @match https://welearn.sflep.com/* // @match https://centercourseware.sflep.com/* // @author Rito // ==/UserScript== (function () { 'use strict'; const SCRIPT_STATE_KEY = 'weleran_auto_answer_enabled'; // --- UI�������� (���ڶ��㴰������) --- function createToggleUI() { const isEnabled = () => localStorage.getItem(SCRIPT_STATE_KEY) !== 'false'; const button = document.createElement('div'); button.style.position = 'fixed'; button.style.bottom = '20px'; button.style.right = '20px'; button.style.width = '60px'; button.style.height = '60px'; button.style.borderRadius = '50%'; button.style.color = 'white'; button.style.display = 'flex'; button.style.justifyContent = 'center'; button.style.alignItems = 'center'; button.style.cursor = 'pointer'; button.style.zIndex = '9999'; button.style.fontSize = '14px'; button.style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)'; button.title = '����л��Զ�����'; const updateButtonState = () => { if (isEnabled()) { // button.style.backgroundColor = '#4CAF50'; // Green button.textContent = '����:��'; } else { // button.style.backgroundColor = '#f44336'; // Red button.textContent = '����:��'; } }; // --- ��ɫѭ����ʽ --- let hue = 0; setInterval(() => { hue = (hue + 1) % 360; if (!isEnabled()) { button.style.backgroundColor = `hsl(${hue}, 80%, 60%)`; } else { button.style.backgroundColor = `hsl(${hue}, 80%, 30%)`; // Darker when on } }, 20); // --- �϶����� --- let isDragging = false; let hasDragged = false; let offsetX, offsetY; button.addEventListener('mousedown', (e) => { isDragging = true; hasDragged = false; const rect = button.getBoundingClientRect(); offsetX = e.clientX - rect.left; offsetY = e.clientY - rect.top; button.style.cursor = 'grabbing'; e.preventDefault(); }); document.addEventListener('mousemove', (e) => { if (!isDragging) return; hasDragged = true; let newX = e.clientX - offsetX; let newY = e.clientY - offsetY; // �������Ӵ��� newX = Math.max(0, Math.min(newX, window.innerWidth - button.offsetWidth)); newY = Math.max(0, Math.min(newY, window.innerHeight - button.offsetHeight)); button.style.left = `${newX}px`; button.style.top = `${newY}px`; button.style.bottom = 'auto'; button.style.right = 'auto'; }); document.addEventListener('mouseup', () => { if (isDragging) { isDragging = false; button.style.cursor = 'pointer'; } }); button.addEventListener('click', (e) => { // ����������϶�������ֹ����¼� if (hasDragged) { e.stopPropagation(); return; } const current = isEnabled(); localStorage.setItem(SCRIPT_STATE_KEY, !current); updateButtonState(); // ˢ��iframe������Ӧ�ø��� const iframe = document.querySelector('iframe'); if (iframe) { iframe.contentWindow.location.reload(); } }); updateButtonState(); document.body.appendChild(button); } // --- �����߼� (����iframe������) --- function runAutoAnswer() { // ��鿪��״̬ if (localStorage.getItem(SCRIPT_STATE_KEY) === 'false') { console.log('[WELearn Auto Answer] �����ѹرգ�����ִ�С�'); return; } 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 ��׼���뵱ǰ�ؼ�������DZ����ظ� 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 { // �ڶ��㴰���У�����UI�������ʾ console.log('[WELearn Auto Answer] Running in top frame (UI created):', location.href); // �ȴ�DOM��������ٴ���UI if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', createToggleUI); } else { createToggleUI(); } } })();