Greasy Fork is available in English.
悬浮窗显示选择题、填空题答案,口语参考文本,支持单元测试
当前为
// ==UserScript==
// @name WELearn英语网课答案显示
// @namespace http://tampermonkey.net/
// @version 0.2.1
// @description 悬浮窗显示选择题、填空题答案,口语参考文本,支持单元测试
// @author SSmJaE
// @match https://course.sflep.com/student/*
// @grant GM_xmlhttpRequest
// @connect *
// @license MIT
// ==/UserScript==
(function() {
'use strict';
var container, title, bufferUrl;
var parser = new DOMParser();
function createContainer() {
container = document.createElement('div');
container.setAttribute('style', "top: 100px; left: 100px; margin: 0 auto; z-index: 99; border-radius: 8px;" +
" box-shadow: 0 11px 15px -7px rgba(0,0,0,.2), 0 24px 38px 3px rgba(0,0,0,.14), 0 9px 46px 8px rgba(0,0,0,.12);" +
" position: absolute; background: #fff; min-width: 250px;max-width:400px; max-height: 500px; min-height: 200px;overflow:auto;")
container.style.visibility = 'hidden';
document.body.appendChild(container);
title = document.createElement('div');
title.textContent = '参考答案';
title.setAttribute("style", "background: inherit; height: 25px; margin-top: 10px; text-align: center; font-size: x-large");
container.appendChild(title);
}
function makeDraggable(elem) {
document.mouseState = 'up'
elem.mouseState = 'up'
elem.lastMousePosY = null
elem.lastMousePosX = null
elem.proposedNewPosY = parseInt(elem.style.top, 10)
elem.proposedNewPosX = parseInt(elem.style.left, 10)
document.onmousedown = _ => {
document.mouseState = 'down'
}
document.onmouseup = _ => {
document.mouseState = 'up'
elem.mouseState = 'up'
}
elem.onmousedown = e => {
elem.lastMousePosY = e.pageY
elem.lastMousePosX = e.pageX
elem.mouseState = 'down'
document.mouseState = 'down'
document.onselectstart = e => {
e.preventDefault()
return false
}
}
elem.onmouseup = e => {
elem.mouseState = 'up'
document.mouseState = 'up'
document.onselectstart = null
}
const getAtInt = (obj, attrib) => parseInt(obj.style[attrib], 10)
document.onmousemove = e => {
if ((document.mouseState === 'down') && (elem.mouseState === 'down')) {
elem.proposedNewPosY = getAtInt(elem.parentElement, 'top') + e.pageY - elem.lastMousePosY
elem.proposedNewPosX = getAtInt(elem.parentElement, 'left') + e.pageX - elem.lastMousePosX
if (elem.proposedNewPosY < 0) {
elem.parentElement.style.top = "0px"
} else if (elem.proposedNewPosY > window.innerHeight - getAtInt(elem.parentElement, 'height')) {
elem.parentElement.style.top = window.innerHeight - getAtInt(elem.parentElement, 'height') + 'px'
} else {
elem.parentElement.style.top = elem.proposedNewPosY + 'px'
}
if (elem.proposedNewPosX < 0) {
elem.parentElement.style.left = "0px"
} else if (elem.proposedNewPosX > window.innerWidth - getAtInt(elem.parentElement, 'width')) {
elem.parentElement.style.left = window.innerWidth - getAtInt(elem.parentElement, 'width') + 'px'
} else {
elem.parentElement.style.left = elem.proposedNewPosX + 'px'
}
elem.lastMousePosY = e.pageY
elem.lastMousePosX = e.pageX
}
}
}
function showAnswers(buffer) {}
function addToContainer(answers) {
if (answers.length > 0) {
for (let i = 0; i < answers.length; i++) {
let content = document.createElement('div');
if (answers[i].tagName == 'ET-BLANK') {
content.textContent = answers[i].textContent;
}
if (answers[i].tagName == 'ET-CHOICE') {
content.textContent = answers[i].getAttribute('key');
}
if (answers[i].tagName == 'ET-REFERENCE') {
content.innerHTML = answers[i].innerHTML;
}
content.setAttribute('style', "margin: 10px 10px; color: orange; font-size: medium; ")
container.appendChild(content);
}
}
}
function parseAjax(htmlDOM) {
container.innerHTML = '';
container.appendChild(title);
let blank = htmlDOM.querySelectorAll('et-blank'); //问答题+填空题
let choice = htmlDOM.querySelectorAll('et-choice'); //选择题(二选一,多选)
let reference = htmlDOM.querySelectorAll('et-reference'); //口语参考
addToContainer(blank);
addToContainer(choice);
addToContainer(reference);
// console.log(blank, choice, reference);
if (container.childNodes.length > 1) {
container.style.visibility = 'visible';
} else {
container.style.visibility = 'hidden';
}
}
function getCurrentUrl() {
let answerUrl = document.querySelector('div.courseware_main_1').firstElementChild.src;
let chapter = /#.*\?/.exec(answerUrl)[0].replace('#', '').replace('?', '');
answerUrl = 'https://centercourseware.sflep.com/inspire%204/data' + chapter + '.html';
return answerUrl;
}
function getAjax(answerUrl) {
let html, htmlDOM;
GM_xmlhttpRequest({
method: "GET",
url: answerUrl,
onload: response => {
html = response.response;
// console.log(html);
htmlDOM = parser.parseFromString(html, 'text/html');
parseAjax(htmlDOM);
}
});
}
function isChange() {
let currentUrl = getCurrentUrl();
if (currentUrl != bufferUrl) {
getAjax(currentUrl);
}
bufferUrl = currentUrl;
}
createContainer();
makeDraggable(title);
isChange();
setInterval(isChange, 5000);
})();