用途僅供學(xué)習(xí)參考
// ==UserScript==
// @name 優(yōu)學(xué)院自動(dòng)靜音播放默责、自動(dòng)做練習(xí)題、自動(dòng)翻頁(yè)固阁、修改播放速率
// @namespace [url=mailto:moriartylimitter@outlook.com]moriartylimitter@outlook.com[/url]
// @version 1.6.0
// @description 自動(dòng)靜音播放每頁(yè)視頻、自動(dòng)作答、修改播放速率!
// @author EliotZhang期升、Brush-JIM
// @match *://*.ulearning.cn/learnCourse/*
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @require https://code.jquery.com/ui/1.12.1/jquery-ui.js
// @run-at document-start
// @grant unsafeWindow
// ==/UserScript==
(function () {
'use strict';
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* 優(yōu)學(xué)院自動(dòng)靜音播放拜姿、自動(dòng)做練習(xí)題、自動(dòng)翻頁(yè)贴汪、修改播放速率腳本v1.6.0由EliotZhang @ 2020/04/27 最后更新
* 特別感謝Brush-JIM (Mail:Brush-JIM@protonmail.com) 提供的腳本改進(jìn)支持脐往!
* 使用修改播放速率功能請(qǐng)謹(jǐn)慎!0夤 业簿!產(chǎn)生的不良后果恕某概不承擔(dān)!Q舳梅尤!
* 請(qǐng)保持網(wǎng)課播放頁(yè)面在瀏覽器中活動(dòng),避免長(zhǎng)時(shí)間后臺(tái)掛機(jī)(平臺(tái)有掛機(jī)檢測(cè)功能)岩调,以減少不必要的損失
* 自動(dòng)作答功能由于精力有限目前只支持單/多項(xiàng)選擇巷燥、判斷題、部分填空問答題号枕,如果出現(xiàn)問題請(qǐng)嘗試禁用這個(gè)功能!
* 如果腳本無(wú)效請(qǐng)優(yōu)先嘗試刷新頁(yè)面缰揪,若是無(wú)效請(qǐng)查看腳本最后的解決方案,如果還是不行請(qǐng)反饋給本人葱淳,本人將會(huì)盡快修復(fù)
* 如果是因?yàn)榫W(wǎng)絡(luò)問題钝腺,本人也無(wú)能為力
* 如果在使用中還有什么問題請(qǐng)通過郵箱聯(lián)系EliotZhang:moriartylimitter@outlook.com
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
var N = 1.50;
var EnableAutoPlay = true;
var EnableAutoMute = true;
var EnableAutoChangeRate = true;
var EnableAutoFillAnswer = true;
var EnableAutoShowAnswer = true;
var EnableAutoAnswerChoices = true;
var EnableAutoAnswerJudges = true;
var EnableAutoAnswerFills = true;
// 新增函數(shù) By Brush-JIM
function Video(func = {}, slept = false) {
if (!EnableAutoPlay)
return;
if (autoAnswering) {
setTimeout(function () {
Video({}, true);
}, '1000');
return;
}
if (!slept) {
setTimeout(function () {
Video({}, true);
}, '3000');
return;
}
var A_tmp = $('video');
var A = [];
for (let d = 0; d < A_tmp.length; d++) {
if (A_tmp[d].src != "") {
A.push(A_tmp[d]);
}
}
if (A.length === 0) {
GotoNextPage();
return;
}
var B = [];
var tmp = $('div[class="video-bottom"]');
for (let b = 0; b < tmp.length; b++) {
let span = tmp[b].getElementsByTagName("span")[0];
if (span) {
let data_bind = span.getAttribute('data-bind');
if (data_bind == 'text: $root.i18nMessageText().finished' || data_bind == 'text: $root.i18nMessageText().viewed' || data_bind == 'text: $root.i18nMessageText().unviewed') {
B.push(data_bind);
}
}
}
function video_ctrl(func) {
for (let c = 0; c < A.length; c++) {
if (B[c] == 'text: $root.i18nMessageText().viewed' || B[c] == 'text: $root.i18nMessageText().unviewed' || B[c] === false) {
if (c - 1 >= 0) {
if (A[c - 1].currentTime != 0) {
if (A[c - 1].paused === true) {
A[c - 1].play()
}
if (EnableAutoMute && A[c - 1].muted === false) {
A[c - 1].muted = true;
}
if (EnableAutoChangeRate && A[c - 1].playbackRate != N) {
A[c - 1].playbackRate = N
}
break;
}
}
if (A[c].paused === true) {
A[c].play();
}
if (EnableAutoMute && A[c].muted === false) {
A[c].muted = true;
}
if (EnableAutoChangeRate && A[c].playbackRate != N) {
A[c].playbackRate = N;
}
break;
}
}
if (B[B.length - 1] == 'text: $root.i18nMessageText().finished' || B[B.length - 1] === true) {
if (A[A.length - 1].currentTime != 0) {
if (A[A.length - 1].paused === true) {
A[A.length - 1].play()
}
if (EnableAutoMute && A[A.length - 1].muted === false) {
A[A.length - 1].muted = true
}
if (EnableAutoChangeRate && A[A.length - 1].playbackRate != N) {
A[A.length - 1].playbackRate = N
}
setTimeout(func, "2000", func);
} else {
GotoNextPage();
}
} else {
setTimeout(func, "2000", func);
}
}
if (A.length == B.length) {
video_ctrl(Video);
} else {
B = [];
for (let d = 0; d < A.length; d++) {
B[d] = false;
A[d].addEventListener("ended", function () {
B[d] = true;
}, true);
}
video_ctrl(video_ctrl);
}
}
function GotoNextPage() {
if (autoAnswering || !EnableAutoPlay || checkingModal)
return;
var nextPageBtn = $('.mobile-next-page-btn');
if (nextPageBtn.length === 0)
return;
nextPageBtn.each((k, n) => {
n.click();
});
setTimeout(Video, "1000");
}
function CheckModal(slept = false) {
if (autoAnswering)
return;
if (!slept) {
setTimeout(function () {
CheckModal(true);
}, '2000');
return;
}
if (EnableAutoPlay) {
CheckModal();
}
checkingModal = true;
var qw = $('.question-wrapper');
if (qw.length > 0 && EnableAutoFillAnswer) {
ShowAndFillAnswer();
checkingModal = false;
return;
}
var statModal = $('#statModal');
if (statModal.length > 0) {
var ch = statModal[0];
ch.getElementsByTagName('button');
if (ch.length >= 2)
ch[1].click();
}
var err = $('.mobile-video-error');
if (err && err.css('display') != 'none')
$('.try-again').click();
var alertModal = document.getElementById("alertModal");
if (alertModal === undefined) {
checkingModal = false;
return;
}
if (alertModal.className.match(/\sin/)) {
var op = $('.modal-operation').children();
if (op.length >= 2)
op[EnableAutoFillAnswer ? 0 : 1].click();
else {
var continueBtn = $('.btn-submit');
if (continueBtn.length > 0) {
continueBtn.each((k, v) => {
if ($(v).text() != '提交')
$(v).click();
});
}
}
if (EnableAutoFillAnswer)
ShowAndFillAnswer();
}
checkingModal = false;
}
function RemoveDuplicatedItem(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
arr.splice(j, 1);
j--;
}
}
}
return arr;
}
function Escape2Html(str) {
var arrEntities = {
'lt': '<',
'gt': '>',
'nbsp': ' ',
'amp': '&',
'quot': '"'
};
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
return arrEntities[t];
});
}
function DelHtmlTag(str) {
return str.replace(/(<[^>]+>|\\n|\\r)/g, " ");
}
function FillAnswers() {
if (!autoAnswering || !EnableAutoAnswerFills)
return;
var ansarr = [];
var idList = [];
var re = [];
var txtAreas = $('textarea, .blank-input');
$(txtAreas).each((k, v) => {
var reg = /question\d+/;
var fa = $(v).parent();
while (!reg.test($(fa).attr('id'))) {
fa = $(fa).parent();
}
var id = $(fa).attr('id').replace('question', '');
idList.push(id);
});
idList = RemoveDuplicatedItem(idList);
$(idList).each((k, id) => {
$.ajax({
url: 'https://api.ulearning.cn/questionAnswer/' + id,
type: "GET",
contentType: "application/json",
dataType: 'json',
async: false,
data: {
parentId: pageid,
},
success: function (result, status, xhr) {
re.push(result.correctAnswerList);
}
});
});
$(re).each((k1, v1) => {
if (v1.length == 1) {
ansarr.push(DelHtmlTag(Escape2Html(v1[0])));
} else {
$(v1).each(function (k2, v2) {
ansarr.push(DelHtmlTag(Escape2Html(v2)));
});
}
});
$(txtAreas).each((k, v) => {
$(v).val(ansarr.shift());
});
}
function ShowAndFillAnswer() {
if (autoAnswering | !EnableAutoFillAnswer)
return;
autoAnswering = true;
var sqList = [];
var qw = $('.question-wrapper');
var pages = $('.page-item');
var an = [];
qw.each(function (k, v) {
var id = $(v).attr('id');
sqList.push(id.replace('question', ''));
});
var flag = false;
pages.each(function (k, v) {
if (flag) return;
var sp = $(v).find('.page-name');
if (sp.length > 0 && sp[0].className.search('active') >= 0) {
var pd = $(v).attr('id');
pd = pd.slice(pd.search(/\d/g));
pageid = pd;
flag = true;
}
});
if (!flag) {
autoAnswering = false;
GotoNextPage();
return;
}
if (sqList.length <= 0) {
autoAnswering = false;
return;
}
$(sqList).each(function (k, id) {
var flag = false;
while (!flag)
$.ajax({
url: 'https://api.ulearning.cn/questionAnswer/' + id,
type: "GET",
contentType: "application/json",
dataType: 'json',
async: false,
data: {
parentId: pageid,
},
success: function (result, status, xhr) {
an.push(result.correctAnswerList);
flag = true;
}
});
});
//
if (EnableAutoShowAnswer) {
var t = qw.find('.question-title-html');
t.each(function (k, v) {
var ans = an.shift();
$(v).after('<span style="color:red;">答案:' + ans + '</span>');
an.push(ans);
});
}
//
var checkBox = qw.find('.checkbox');
var choiceBox = qw.find('.choice-btn');
var checkList = [];
var choiceList = [];
let lasOffsetP = '';
checkBox.each((k, cb) => {
if (lasOffsetP == $(cb).offsetParent().attr('id')) {
checkList[checkList.length - 1].push(cb);
} else {
var l = [];
l.push(cb);
checkList.push(l);
lasOffsetP = $(cb).offsetParent().attr('id');
}
});
lasOffsetP = '';
choiceBox.each((k, cb) => {
if (lasOffsetP == $(cb).offsetParent().attr('id')) {
choiceList[choiceList.length - 1].push(cb);
} else {
var l = [];
l.push(cb);
choiceList.push(l);
lasOffsetP = $(cb).offsetParent().attr('id');
}
});
an.forEach(a => {
if (a==null || a==undefined || a.length <= 0) {
return;
}
if (a[0].match(/[A-Z]/) && a[0].length == 1 && EnableAutoAnswerChoices) {
var cb = checkList.shift();
a.forEach(aa => {
var cccb = $(cb[aa.charCodeAt() - 65]);
if (cccb[0] === undefined || (cccb[0] != undefined && cccb[0].className.search('selected') < 0))
cccb.click();
});
} else if (a[0].match(/(([tT][rR][uU][eE])|([fF][aA][lL][sS][eE]))/) && EnableAutoAnswerJudges) {
var ccb = choiceList.shift();
a.forEach(aa => {
if (aa.match(/([tT][rR][uU][eE])/))
ccb[0].click();
else
ccb[1].click();
});
}
return;
});
if (EnableAutoAnswerFills) {
FillAnswers();
$.globalEval("$('textarea, .blank-input').trigger('change')");
}
if (EnableAutoPlay) {
$('textarea, .blank-input').trigger('change');
$('.btn-submit').click();
var A_tmp = $('video');
var A = [];
for (let d = 0; d < A_tmp.length; d++) {
if (A_tmp[d].src != "") {
A.push(A_tmp[d]);
}
}
if (A.length === 0) {
autoAnswering = false;
GotoNextPage();
return;
}
}
autoAnswering = false;
}
function LoadStyle(url) {
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
var head = document.getElementsByTagName('head')[0];
head.appendChild(link);
}
function DrawOptionPanel() {
LoadStyle('https://eliotzhang.club/CSS/ulearning.css');
var root = document.getElementsByTagName('body')[0];
var panel = document.createElement('div');
root.appendChild(panel);
panel.setAttribute('class', 'OptionPanel');
panel.innerHTML = "<div class='OptionPanel'><div class='DragBall'>UL</div><div class='MainPanel'><h2 class='OptionMainTitle'>優(yōu)學(xué)院輔助腳本</br>by EliotZhang、BrushJIM</h2><button id='MainBtn'>隱藏設(shè)置</button><h4>視頻播放</h4><ul class='OptionUL'><li>自動(dòng)翻頁(yè)赞厕、播放視頻?<input class='OptionInput'id='AutoPlay'type='checkbox'checked='checked'></li><li>自動(dòng)靜音?<input class='OptionInput'id='AutoMute'type='checkbox'checked='checked'></li><li>自動(dòng)調(diào)整速率(依賴自動(dòng)播放視頻功能)?<input class='OptionInput'id='AutoPlayRate'type='checkbox'checked='checked'></li><li>自動(dòng)的速率速度<input class='OptionInput'id='AutoPlayRateChange'type='number'value='1.50'step='0.25'min='0.25'max='15.00'></li></ul><h4>自動(dòng)作答</h4><ul class='OptionUL'><li>自動(dòng)作答(總開關(guān))?<input class='OptionInput'id='AutoAnswer'type='checkbox'checked='checked'></li><li>自動(dòng)顯示答案?<input class='OptionInput'id='AutoShowAnswer'type='checkbox'checked='checked'></li><li>自動(dòng)作答選擇題?<input class='OptionInput'id='AutoAnswerChoices'type='checkbox'checked='checked'></li><li>自動(dòng)作答判斷題?<input class='OptionInput'id='AutoAnswerJudges'type='checkbox'checked='checked'></li><li>自動(dòng)作答填空艳狐、簡(jiǎn)答題?<input class='OptionInput'id='AutoAnswerFills'type='checkbox'checked='checked'></li></ul><button id='SaveOpBtn'>保存設(shè)置并刷新腳本</button><p style='color:hotpink;'>若<strong>關(guān)閉自動(dòng)翻頁(yè)功能</strong>導(dǎo)致<strong>自動(dòng)作答系列功能失效</strong>請(qǐng)點(diǎn)擊<strong>保存設(shè)置并刷新腳本按鈕!</strong></p><p style='color:hotpink;'>若關(guān)閉自動(dòng)翻頁(yè)功能答完題后請(qǐng)<strong>手動(dòng)提交C笊!僵驰!</strong></p></div></div>";
}
function Init() {
mainBtn = document.getElementById('MainBtn');
dragBall = $('.DragBall');
saveOpBtn = document.getElementById('SaveOpBtn');
OptionPanel = $('.OptionPanel');
MainPanel = $('.MainPanel');
autoPlayOp = document.getElementById('AutoPlay');
autoMuteOp = document.getElementById('AutoMute');
autoPlayRateOp = document.getElementById('AutoPlayRate');
autoPlayRateChangeOp = document.getElementById('AutoPlayRateChange');
autoAnswerOp = document.getElementById('AutoAnswer');
autoShowAnswerOp = document.getElementById('AutoShowAnswer');
autoAnswerChoicesOp = document.getElementById('AutoAnswerChoices');
autoAnswerJudgesOp = document.getElementById('AutoAnswerJudges');
autoAnswerFillsOp = document.getElementById('AutoAnswerFills');
dragBall.draggable({
containment: ".page-scroller ps ps--theme_default",
start: function (event, ui) {
$(this).addClass('noclick');
}
});
dragBall.hide();
mainBtn.addEventListener('click', function () {
MainPanel.hide();
dragBall.show();
}, true);
dragBall.click(function (e) {
if ($(this).hasClass('noclick')) {
$(this).removeClass('noclick');
} else if (e.target == e.currentTarget) {
MainPanel.show();
$(this).hide();
}
});
autoPlayRateChangeOp.addEventListener('change', function () {
let val = autoPlayRateChangeOp.value;
if (val > 15.0)
autoPlayRateChangeOp.value = 15;
else if (val < 0.25)
autoPlayRateChangeOp.value = 0.25;
}, true);
autoPlayOp.addEventListener('change', function () {
if (autoPlayOp.checked === false)
autoMuteOp.checked = autoPlayRateOp.checked = false;
else
autoMuteOp.checked = autoPlayRateOp.checked = true;
});
autoAnswerOp.addEventListener('change', function () {
if (autoAnswerOp.checked === false)
autoAnswerChoicesOp.checked = autoAnswerJudgesOp.checked = autoAnswerFillsOp.checked = autoShowAnswerOp.checked = false;
else
autoAnswerChoicesOp.checked = autoAnswerJudgesOp.checked = autoAnswerFillsOp.checked = autoShowAnswerOp.checked = true;
});
saveOpBtn.addEventListener('click', function () {
EnableAutoMute = autoMuteOp.checked;
EnableAutoChangeRate = autoPlayRateOp.checked;
EnableAutoPlay = autoPlayOp.checked;
EnableAutoShowAnswer = autoShowAnswerOp.checked;
EnableAutoAnswerChoices = autoAnswerChoicesOp.checked;
EnableAutoAnswerJudges = autoAnswerJudgesOp.checked;
EnableAutoAnswerFills = autoAnswerFillsOp.checked;
if (!EnableAutoShowAnswer && !EnableAutoAnswerChoices && !EnableAutoAnswerJudges && !EnableAutoAnswerFills)
autoAnswerOp.checked = false;
EnableAutoFillAnswer = autoAnswerOp.checked;
N = autoPlayRateChangeOp.value;
// Save
window.localStorage.EZUL = 'EliotZhang、BrushJIM';
window.localStorage.EAM = EnableAutoMute ? 't' : 'f';
window.localStorage.EACR = EnableAutoChangeRate ? 't' : 'f';
window.localStorage.EAP = EnableAutoPlay ? 't' : 'f';
window.localStorage.EASA = EnableAutoShowAnswer ? 't' : 'f';
window.localStorage.EAAC = EnableAutoAnswerChoices ? 't' : 'f';
window.localStorage.EAAJ = EnableAutoAnswerJudges ? 't' : 'f';
window.localStorage.EAAF = EnableAutoAnswerFills ? 't' : 'f';
window.localStorage.EAFA = EnableAutoFillAnswer ? 't' : 'f';
window.localStorage.APRC = autoPlayRateChangeOp.value.toString();
Video({}, true);
CheckModal(true);
}, true);
// Load
if (window.localStorage.getItem('EZUL') === 'EliotZhang唁毒、BrushJIM') {
autoMuteOp.checked = EnableAutoMute = window.localStorage.EAM == 't';
autoPlayRateOp.checked = EnableAutoChangeRate = window.localStorage.EACR == 't';
autoPlayOp.checked = EnableAutoPlay = window.localStorage.EAP == 't';
autoShowAnswerOp.checked = EnableAutoShowAnswer = window.localStorage.EASA == 't';
autoAnswerChoicesOp.checked = EnableAutoAnswerChoices = window.localStorage.EAAC == 't';
autoAnswerJudgesOp.checked = EnableAutoAnswerJudges = window.localStorage.EAAJ == 't';
autoAnswerFillsOp.checked = EnableAutoAnswerFills = window.localStorage.EAAF == 't';
autoAnswerOp.checked = EnableAutoFillAnswer = window.localStorage.EAFA == 't';
autoPlayRateChangeOp.value = N = parseFloat(window.localStorage.APRC);
}
}
function Main() {
DrawOptionPanel();
Init();
Video();
CheckModal();
}
var autoAnswering = false;
var checkingModal = false;
var mainBtn;
var dragBall;
var saveOpBtn;
var OptionPanel;
var MainPanel;
var autoPlayOp;
var autoMuteOp;
var autoPlayRateOp;
var autoPlayRateChangeOp;
var autoAnswerOp;
var autoShowAnswerOp;
var autoAnswerChoicesOp;
var autoAnswerJudgesOp;
var autoAnswerFillsOp;
var pageid = '';
try {
unsafeWindow.document.__defineGetter__('hidden', function () {
return false
})
} catch (e) {
Object.defineProperty(unsafeWindow.document, 'hidden', {
get: function () {
return false
}
})
};
try {
unsafeWindow.document.__defineGetter__('visibilityState', function () {
return 'visible'
})
} catch (e) {
Object.defineProperty(unsafeWindow.document, 'visibilityState', {
get: function () {
return 'visible'
}
})
};
setTimeout(Main, '3000');
})();