Skip to content

Commit

Permalink
修复答题页面报错
Browse files Browse the repository at this point in the history
  • Loading branch information
ttglad committed May 31, 2022
1 parent 884726a commit ca690a3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 73 deletions.
Binary file modified release.crx
Binary file not shown.
4 changes: 4 additions & 0 deletions release/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function startStudy() {
chrome.windows.update(result.studyWindowId, { "focused": true });
}
});
return true;
}

// 停止学习,需要关闭
Expand All @@ -267,6 +268,7 @@ function stopStudy() {
chrome.action.setBadgeText({ text: "" });
}
});
return true;
}

// 通知消息
Expand Down Expand Up @@ -330,6 +332,7 @@ chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
logMessage("chrome tab remove success.");
}
});
return true;
});

// 窗口移除监听事件
Expand All @@ -342,6 +345,7 @@ chrome.windows.onRemoved.addListener(function (windowId) {
logMessage("chrome windows remove success.");
}
});
return true;
});

// 后台监听事件消息
Expand Down
163 changes: 91 additions & 72 deletions release/js/xuexi/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,69 @@ chrome.runtime.sendMessage({ type: "checkRunning" }, {}, function (response) {
var WaitingTime = 15, setTimeoutFunc = null, ManageType = 'auto', isManual = false;

function getAnswers() {
var answerChoseNum = 0, answerArray = [], match_num = {}, max = 0, timeAfter = 0;
let answerChoseNum = 0, answerArray = [], match_num = {}, max = 0, timeAfter = 0;
isManual = false;
if (document.querySelector(".q-header") == null) {
if (document.querySelector(".ant-btn.action.ant-btn-primary") != null) {
// 获取答题标题,单选题、多选题、填空题
let questionTitle = $(".q-header");
if (!questionTitle.length) {
// 如果答题已完成
if ($(".ant-btn.action.ant-btn-primary").length) {
setTimeout(function () {
chrome.runtime.sendMessage({ type: "studyComplete" }, {}, function (res) {
if (res.complete) {
window.close();
}
});
}, 5000 + Math.floor(Math.random() * 5000));
return;
} else {
setTimeoutFunc = setTimeout(getAnswers, parseInt(Math.random() * 2000 + 2000));
return;
}
}
var questionTitle = document.querySelector(".q-header");
if (questionTitle == null) {
return;
}
var questionType = questionTitle.innerText.substr(0, 3);
if (document.querySelector(".q-footer .tips") != null) {
document.querySelector(".q-footer .tips").click();
} else {
// 提交答案
if (!$(".q-footer .tips").length) {
answerSubmit(1);
return;
}
document.querySelectorAll('.line-feed [color=red]').forEach(function (a, b, c) {
let i = a.innerText;
if (i != "") answerArray.push(i);
// 获取答案
$(".q-footer .tips").click();
$('.line-feed [color=red]').each(function () {
let i = $(this).text();
if (i != "") {
answerArray.push(i);
}
});

// 如果 答案为空,则找到全部提示内容
// 如果答案为空,则找到全部提示内容
if (answerArray.length == 0) {
document.querySelectorAll('.line-feed > font').forEach(function (a, b, c) {
let i = a.innerText;
if (i != "") answerArray.push(i);
$('.line-feed > font').each(function () {
let i = $(this).text();
if (i != "") {
answerArray.push(i);
}
});

if (answerArray.length == 0) {
document.querySelectorAll('.line-feed').forEach(function (a, b, c) {
let i = a.innerText;
$('.line-feed').each(function () {
let i = $(this).text();
if (i != "" && i != "请观看视频") {
answerArray.push(i);
}
});
}
}

// 获取题目
let questionType = questionTitle.text().substr(0, 3);
switch (questionType) {
case "单选题":
timeAfter = 1;
case "多选题":
answerChoseNum = document.querySelectorAll('.q-answers .chosen').length;
answerChoseNum = $('.q-answers .chosen').length;
if (answerChoseNum <= 0) {
document.querySelectorAll('.q-answer').forEach(function (a, b, c) {
var answerSelect = a.innerHTML.split('. ').slice(-1)[0];
$('.q-answer').each(function () {
let that = $(this);
var answerSelect = that.text().split('. ').slice(-1)[0];
var answerIsRight = false;
var answerMatches = 0;
var isChosen = false;
Expand All @@ -71,20 +77,21 @@ chrome.runtime.sendMessage({ type: "checkRunning" }, {}, function (response) {
answerSelect = answerSelect.replace(/\(/g, "(").replace(/\)/g, ")");
answerJoinString = answerJoinString.replace(/\(/g, "(").replace(/\)/g, ")");

isChosen = Boolean(a.className.indexOf("chosen") != -1);
isChosen = Boolean(that.attr('class').indexOf("chosen") != -1);
answerIsRight = (answerSelect.indexOf(answerJoinString) != -1 || answerJoinString.indexOf(answerSelect) != -1) && answerJoinString != "";
if (answerIsRight && questionType == '单选题') {
answerIsRight = (answerJoinString.length == answerSelect.length ? true : false);
}
if (answerIsRight && !isChosen) {
a.click();
that.click();
answerChoseNum++;
}
if (!answerIsRight) {
answerMatches += getAnswerMatches(answerJoinString, a.innerHTML);
match_num[answerMatches] = a
answerMatches += getAnswerMatches(answerJoinString, that.text());
match_num[answerMatches] = that;
}
})
});

if (answerChoseNum == 0) {
for (let i in match_num) {
max = Number(max) >= Number(i) ? Number(max) : Number(i);
Expand Down Expand Up @@ -112,10 +119,11 @@ chrome.runtime.sendMessage({ type: "checkRunning" }, {}, function (response) {
answerArray[0] = a.substr(i, n);
}
var value = "";
if (c.length == 1)
if (c.length == 1) {
value = answerArray.join('');
else
} else {
value = b < answerArray.length ? answerArray[b] : answerArray[0];
}
if (a.value == "") {
a.setAttribute("value", value);
a.dispatchEvent(ev);
Expand All @@ -133,8 +141,17 @@ chrome.runtime.sendMessage({ type: "checkRunning" }, {}, function (response) {
}

function answerSubmit(answerChoseNum = 0) {
// 提交答案
if (answerChoseNum > 0 && ManageType == 'auto') {
!document.querySelector(".next-btn").disabled ? document.querySelector(".next-btn").click() : document.querySelector(".submit-btn").click();
//!document.querySelector(".next-btn").disabled ? document.querySelector(".next-btn").click() : document.querySelector(".submit-btn").click();
// 有提交按钮,提交数据
if ($(".submit-btn").length) {
$(".submit-btn").click();
} else {
if ($(".next-btn").length) {
$(".next-btn").click();
}
}
setTimeoutFunc = setTimeout(getAnswers, parseInt(Math.random() * 1000 + 2000));
}
}
Expand All @@ -150,60 +167,62 @@ chrome.runtime.sendMessage({ type: "checkRunning" }, {}, function (response) {
}

function manualManage() {
if (document.querySelector("#my_ms") != null || !isManual) return;

let myId = "my_ms";
if ($('#' + myId).length || !isManual) {
return;
}

// 浏览器提醒
chrome.runtime.sendMessage({ type: "answerError" });

let ds_c = 0;
let ds_t = null;
// 设置类型等待
ManageType = "wait";
let e = document.createElement("div");
e.id = "my_ms";
e.innerHTML = "此题无完全匹配答案,已填写(选择)一个相对最匹配的答案(可能是错误的)。你可以点击下面按钮切换到手动做题并修正答案后再次点击按钮切换到自动做题。<br><span id='my_ds'>若 <span id='my_ds_c'>" + WaitingTime + "</span> 秒无操作则继续自动做题</span><br>";
e.style.color = 'red';
e.style.fontSize = '20px';
e.style.textAlign = 'center';
document.querySelector(".header-row").appendChild(e);
let b = document.createElement("button");
b.style.color = 'green';
b.style.fontSize = '24px';
b.style.textAlign = 'center';
b.style.marginTop = '10px';
b.value = 'auto';
b.innerText = '切换到手动做题';
b.onclick = function () {
document.querySelector("#my_ds").innerHTML = '';
if (ds_t != null) {
clearInterval(ds_t);
ds_t = null;

let timerId = "my_ds_c";
let buttonId = "my_bt_c";
let html = '<div id="' + myId + '" style="color: red; font-size: 20px; text-align: center;">此题无完全匹配答案,已填写(选择)一个相对最匹配的答案(可能是错误的)。你可以点击下面按钮切换到手动做题并修正答案后再次点击按钮切换到自动做题。<br>';
html += '<span>若 <span id="' + timerId + '">' + WaitingTime + '</span> 秒无操作则继续自动做题</span><br>';
html += '<button id="' + buttonId + '" value="auto" style="color: green; font-size: 24px; text-align: center; margin-top: 10px;">切换到手动做题</button>';
html += '</div>';

$(".header-row").append(html);

let timeLeftSeconds = 0;
let timeLeftEvenv = null;
// button点击事件
$('#' + buttonId).off('click').on('click', function () {
if (timeLeftEvenv != null) {
clearInterval(timeLeftEvenv);
timeLeftEvenv = null;
}
if (this.value == 'auto') {
this.value = 'manual';
if ($(this).val() == 'auto') {
$(this).val('manual');
$(this).text('切换到自动做题');
$(this).css('color', 'red');
ManageType = "manual";
b.innerText = '切换到自动做题';
this.style.color = 'red';
} else {
this.value = 'auto';
$(this).val('auto');
$(this).text('切换到手动做题');
$(this).css('color', 'green');
ManageType = 'auto';
b.innerText = '切换到手动做题';
this.style.color = 'green';
document.querySelector("#my_ms").remove();
$('#' + myId).remove();
getAnswers();
}
}
e.appendChild(b);
ds_t = setInterval(function () {
ds_c++;
document.querySelector("#my_ds_c").innerText = WaitingTime - ds_c;
if (ds_c >= WaitingTime) {
document.querySelector("#my_ms").remove();
clearInterval(ds_t);
ds_t = null;
});

// 定时事件
timeLeftEvenv = setInterval(function () {
timeLeftSeconds++;
$('#' + timerId).text(WaitingTime - timeLeftSeconds);
if (timeLeftSeconds >= WaitingTime) {
$('#' + myId).remove();
clearInterval(timeLeftEvenv);
timeLeftEvenv = null;
ManageType = 'auto';
answerSubmit(1);
}
}, 1000);

}

setTimeoutFunc = setTimeout(getAnswers, parseInt(Math.random() * 3000 + 3000));
Expand Down
3 changes: 2 additions & 1 deletion release/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "__MSG_extName__",
"default_locale": "zh_CN",
"description": "__MSG_extDescription__",
"version": "3.0.3",
"version": "3.0.4",
"manifest_version": 3,
"icons": {
"16": "img/icon/icon_16x16.png",
Expand Down Expand Up @@ -53,6 +53,7 @@
},
{
"js": [
"libs/jquery.js",
"js/xuexi/answer.js"
],
"matches": [
Expand Down

0 comments on commit ca690a3

Please sign in to comment.