-
Notifications
You must be signed in to change notification settings - Fork 0
/
MOOC-Helper.js
355 lines (325 loc) · 14 KB
/
MOOC-Helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// ==UserScript==
// @name MOOC Helper
// @version 0.5
// @description 中国大学MOOC慕课辅助脚本
// @author Harry Huang
// @license MIT
// @match *://www.icourse163.org/*
// @run-at document-start
// @grant GM_addStyle
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @source https://github.com/isHarryh/USTB-OES-JS
// @namespace https://www.icourse163.org/
// ==/UserScript==
(function() {
'use strict';
// Optimize webpage style
GM_addStyle(`
.m-learnhead {
height: 100px !important;
}
.m-learnhead-right div {
align-self: baseline !important;
}
.learnPageContent {
border-radius: 10px !important;
}
.u-quizHwListItem .detail {
padding: 10px 0 !important;
}
.u-quizHwInfoItem .infoItem {
margin: 10px 0 !important;
}
.analysisMode .analysisInfo {
padding: 6px 12px !important;
margin: 12px 0 6px 0 !important;
}
.analysisMode .anserror {
background: #f3f5ff !important;
border: 1px solid #bbc0f6 !important;
}
.m-quizScore .totalScore {
margin: 10px 0 0 0 !important;
}
`);
class QuizBean {
static info = {};
static paper = {};
static updateInfo(data) {
QuizBean.info[data.tid] = {
quizId: data.tid,
quizName: data.name,
papers: data.ansformInfoList && data.ansformInfoList.map(e => ({
answerId: e.aid,
scoreFinal: e.finalScore,
scoreTotal: e.totalScore,
})),
targetAnswerId: data.targetAnswerform && data.targetAnswerform.aid,
tryTimeLimit: data.totalTryCount,
tryTimeUsed: data.usedTryCount
};
}
static updatePaper(data) {
QuizBean.paper = {
answerId: data.aid,
quizId: data.tid,
quizName: data.tname,
timeOpen: data.startTime,
timeClose: data.deadline,
timeSubmit: data.submitTime,
tryTimeLimit: data.tryTime,
tryTimeUsed: data.usedTryCount,
scoreO: data.objectiveScore,
scoreS: data.subjectiveScore,
answers: data.answers && data.answers.map(e => ({
questionId: e.qid,
questionType: e.type,
optionIdList: e.optIds,
score: e.score
})),
questionListO: data.objectiveQList && data.objectiveQList.map(e => ({
questionId: e.id,
questionType: e.type,
scoreMax: e.score,
optionList: e.optionDtos && e.optionDtos.map(o => ({
optionId: o.id,
isAnswer: o.answer
}))
}))
};
console.log(QuizBean.paper);
}
static showPaperAnswer() {
let successCount = 0;
// Step 1. Attach embedded answer display to each answer
const wrapper = $('.j-quizPool .j-data-list');
if (QuizBean.paper.answers && wrapper.length) {
// If answers exists and wrapper exists:
let curIdxO = 0;
wrapper.children().each((_, e) => {
// For each children in wrapper:
e = $(e);
if (e.hasClass('m-choiceQuestion') && e.find('.j-choicebox').length &&
curIdxO < QuizBean.paper.questionListO.length) {
// If this is an objective question element
const q = QuizBean.paper.questionListO[curIdxO++];
const rightAnsIdx = QuizBean.getRightOptionIdxFromQuestionO(q);
const rightAnsStr = QuizBean.convertOptionIdxToLetter(rightAnsIdx);
const a = QuizBean.getAnswerOById(q.questionId);
let box = $(`<div></div>`);
if (a && q.questionType == 1) {
const myAnsIdx = QuizBean.getMyOptionIdxFromAnswerO(a)
const myAnsStr = QuizBean.convertOptionIdxToLetter(myAnsIdx);
// Attach embedded answer display
box = $(`
<div class="analysisInfo mghAnswerO" style="display:none">
<div>
<span class="f-f0 tt1">MOOC Helper 获取的答案:</span>
<span class="f-f0 tt2">${rightAnsStr}</span>
</div>
</div>
`);
if (myAnsStr !== rightAnsStr) {
box.addClass('answrong');
box.find('div').append(`<span class="tt3">你错选为${myAnsStr}</span>`);
}
successCount++;
} else {
box = $(`
<div class="analysisInfo mghAnswerO anserror" style="display:none">
<div>
<span class="f-f0 tt1">MOOC Helper 无法获取本题答案,可能是题型不支持或您未作答。</span>
</div>
</div>
`);
}
const oldBox = e.find('.mghAnswerO');
if (!oldBox.length || oldBox.html() !== box.html()) {
if (oldBox.length) {
oldBox.remove();
}
e.find('.j-choicebox').append(box);
box.slideDown();
}
}
}); // End foreach
} // End if
// Step 2. Update status display
if (wrapper.length) {
const insertAfter = $('.j-scoreInfo');
if (insertAfter.length) {
const statusBox = $(`
<div id="mchPaperStatus" class="totalScore f-f0" style="display:none">
<p><b>MOOC Helper:</b></p>
</div>`
);
if (successCount > 0) {
statusBox.append("已显示参考答案。");
statusBox.append(`本次一共成功获取了 <b>${successCount}</b> 道客观题答案。`);
const regex = /(.+#\/learn\/)quizscore\?id=(\d+)&aid=\d+/;
if (regex.test(document.URL)) {
const href = document.URL.replace(regex, '$1quiz?id=$2');
statusBox.append('<br>');
statusBox.append(`请注意,每次测验的题目或选项顺序可能改变。`)
statusBox.append(`你可以选择 <a href="${href}" target="_blank">在新标签页中再做一次</a> 。`);
}
} else {
statusBox.append("未成功获取到参考答案。可能是该测验记录尚未提交,或者题型不兼容。");
}
const oldStatusBox = $('#mchPaperStatus');
if (!oldStatusBox.length || oldStatusBox.html() !== statusBox.html()) {
if (oldStatusBox.length) {
oldStatusBox.remove();
}
statusBox.insertAfter(insertAfter);
statusBox.slideDown();
}
}
} // End if
} // End method
static showQuizInfo() {
// Step 1. Unlock info of each quiz
const quizItems = $('.m-chapterQuizHwItem');
if (quizItems.length && Object.keys(QuizBean.info).length) {
if (quizItems.length === Object.keys(QuizBean.info).length) {
quizItems.each((i, e) => {
const curQuiz = QuizBean.info[Object.keys(QuizBean.info)[i]];
QuizBean.showQuizInfoSingle(curQuiz, $(e));
});
} else {
console.warn("Inconsistent quiz item length");
}
}
}
static showQuizInfoSingle(curQuiz, curQuizElem) {
let count = 0;
const ansUl = curQuizElem.find('.j-ansul');
if (ansUl.length) {
const ansLis = ansUl.find('.f-cb').not('.hd');
if (ansLis.length && curQuiz.papers) {
if (ansLis.length === curQuiz.papers.length) {
ansLis.each((i, e) => {
const curPaper = curQuiz.papers[Object.keys(curQuiz.papers)[i]];
const trick = $(e).find('.j-triggerLockScore');
if (trick.length) {
trick.replaceWith(`<span>${curPaper.scoreFinal.toFixed(2)}(已解锁)</span>`);
count += 1;
}
});
} else {
console.warn("Inconsistent answer list length");
}
}
}
if (count) {
const tip = $(`
<p class="infoItem">
<span class="f-icon u-icon-warning2"></span>
<span class="gray">MOOC Helper 已自动解锁 ${count} 个不可查看的分数</span>
</p>
`);
tip.insertAfter(ansUl);
}
}
static getQuestionOById(questionId) {
for (let i = 0; i < QuizBean.paper.questionListO.length; i++) {
if (QuizBean.paper.questionListO[i].questionId === questionId) {
return QuizBean.paper.questionListO[i];
}
}
return null;
}
static getAnswerOById(questionId) {
for (let i = 0; i < QuizBean.paper.answers.length; i++) {
if (QuizBean.paper.answers[i].questionId === questionId) {
return QuizBean.paper.answers[i];
}
}
return null;
}
static getRightOptionIdxFromQuestionO(question) {
const rst = [];
for (let i = 0; i < question.optionList.length; i++) {
if (question.optionList[i].isAnswer) {
rst.push(i);
}
}
return rst;
}
static getMyOptionIdxFromAnswerO(answer) {
const q = QuizBean.getQuestionOById(answer.questionId);
const rst = [];
answer.optionIdList.forEach((e) => {
for (let i = 0; i < q.optionList.length; i++) {
if (q.optionList[i].optionId === e) {
rst.push(i);
}
}
});
return rst;
}
static convertOptionIdxToLetter(optionIdx) {
if (Number.isInteger(optionIdx)) {
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[optionIdx];
} else {
const rst = [];
optionIdx.sort();
optionIdx.forEach((e) => {
rst.push("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e]);
});
return rst.join('');
}
}
}
class XHRSpy {
static listeners = [];
static originalSend = XMLHttpRequest.prototype.send;
static replacedSend = XMLHttpRequest.prototype.send = function(...args) {
const xhr = this;
const originCallback = xhr.onreadystatechange;
xhr.onreadystatechange = function() {
// Invoke our listeners before the original one
XHRSpy.listeners.forEach((l) => l(xhr));
originCallback();
};
return XHRSpy.originalSend.apply(xhr, args);
};
static add(pathNamePrefix, handler) {
XHRSpy.listeners.push(function(xhr) {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
let url = new URL(xhr.responseURL);
if (url.pathname.startsWith(pathNamePrefix)) {
let json;
try {
json = JSON.parse(xhr.responseText);
} catch (err) {
console.error("Response parsing failed", err);
}
// JSON contents is { code: int, msg: str, result: obj }
if (json.code !== 0 || json.result === undefined) {
console.error(`Response not success\ncode=${json.code}\nmessage=${json.msg}`)
} else {
try {
handler(json.result, url);
} catch (err) {
console.error("Response handling failed", err);
}
}
}
}
});
}
}
// Listen on quiz bean responses
XHRSpy.add('/web/j/mocQuizRpcBean.getOpenQuizPaperDto.rpc', (data, url) => {
QuizBean.updatePaper(data);
});
XHRSpy.add('/web/j/mocQuizRpcBean.getOpenQuizInfo.rpc', (data, url) => {
QuizBean.updateInfo(data);
});
setInterval(() => {
QuizBean.showPaperAnswer();
QuizBean.showQuizInfo();
}, 500);
console.log("MOOC Helper Start");
})();