This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
catchHomework.js
539 lines (403 loc) · 13.9 KB
/
catchHomework.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
function debug(param) {
if (globalDebugMode) {
console.log(param)
}
}
/* function changeNext() {
if (0 < notMarkCount) {
var nextURL = document.URL.split("#")[0] + "#" + TWorkStr + "-id-" + hid.toString() + "-be_uid-" + nuid.toString()
document.querySelectorAll(".report-box-head > .pull-right > a").forEach(function (btn) {
btn.href = nextURL
}
)
}
} */
function getAttachment() {
/*
获取页面的附件信息
*/
for (let i = 0; i < questionCount; i++) {
if (!homeworkPara[i].answer_record.hasOwnProperty('attach_info')) {
continue
}
debug("捕捉到作业附件")
var attachment = homeworkPara[i].answer_record.attach_info
var attachCount = attachment.length
for (let j = 0; j < attachCount; j++) {
var attachInfo = []
var fName = attachment[j].filename //文件名
var tmpURL = attachment[j].path //文件实际下载路径
var fExt = attachment[j].ext //文件扩展名
attachInfo.push(fName)
attachInfo.push(fExt)
attachInfo.push(tmpURL)
attachInfo.push(attachElements[j])//附件所属<a>元素
/* 附件分类 */
if ('jpg|jpeg|png|gif'.indexOf(fExt.toLowerCase()) > -1) {
imgLinks.push(attachInfo)
} else {
fileLinks.push(attachInfo)
}
}
}
}
function img2canvas() {
/*
将img转换为canvas,以便应用旋转功能
*/
for (let imgIndex = 0; imgIndex < imgInserted.length; imgIndex++) {
var imgCanvas = document.createElement('canvas')
var img2 = document.createElement('img')
imgCanvas.className = "imgCanvas-0"
var imgctx = imgCanvas.getContext('2d')
imgs = imgInserted[imgIndex]
img2.src = imgs.src
img2.className = 'insertedImg'
imgCanvas.appendChild(img2)
imgs.parentNode.appendChild(imgCanvas)
imgs.parentNode.removeChild(imgs)
/* 在canvas绘制图片内容 */
imgctx2 = img2.parentElement.getContext('2d')
img2.parentElement.width = img2.parentElement.parentElement.parentElement.offsetWidth
img2.parentElement.height = img2.parentElement.parentElement.parentElement.offsetWidth * img2.height / img2.width
imgctx2.drawImage(img2, 0, 0, img2.width, img2.height, 0, 0, img2.parentElement.width, img2.parentElement.height)
imgCanvas.parentElement.insertBefore(document.createElement('br'), imgCanvas)
if (settings.enableImageRotation) {
setImgRotate(imgCanvas, "imgCanvas-")
}
/* 为每个图片下方添加评语框 */
if (settings.enableRateBox) {
addImgRateBox(imgCanvas)
}
}
}
function setImgRotate(imgCanvas, tagPreffix) {
/*
imgCanvas: 绘制图片的canvas
tagPreffix: 绘制图片canvas class的前缀格式
*/
imgCanvas.addEventListener('click', function () {
var currentDeg = parseInt((/[0-9]+/i).exec(this.className))
var newDeg = (currentDeg + 90) % 360
var ctx = this.getContext('2d')
var img = this.children[0]
switch (newDeg) {
case 90:
ctx.clearRect(0, 0, this.width, this.height)
this.height = this.parentElement.offsetWidth * img.width / img.height
ctx.rotate(1 / 2 * Math.PI)
ctx.translate(0, -this.width)
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, this.height, this.width)
ctx.translate(0, this.width)
ctx.rotate(-1 / 2 * Math.PI)
break;
case 180:
ctx.clearRect(0, 0, this.width, this.height)
this.height = this.parentElement.offsetWidth * img.height / img.width
ctx.rotate(Math.PI)
ctx.translate(-this.width, -this.height)
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, this.width, this.height)
ctx.translate(this.width, this.height)
ctx.rotate(-Math.PI)
break;
case 270:
ctx.clearRect(0, 0, this.width, this.height)
this.height = this.parentElement.offsetWidth * img.width / img.height
ctx.rotate(-1 / 2 * Math.PI)
ctx.translate(-this.height, 0)
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, this.height, this.width)
ctx.translate(this.height, 0)
ctx.rotate(1 / 2 * Math.PI)
break;
case 0:
this.height = this.parentElement.offsetWidth * img.height / img.width
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, this.width, this.height)
break;
default:
break;
}
this.className = tagPreffix + newDeg.toString()
})
}
function addImgRateBox(imgCanvas) {
/*
imgCanvas: 希望在下方增加输入框的canvas
*/
var inbox2 = document.createElement('textarea')
inbox2.className = 'ratebox'
inbox2.placeholder = '点击可以在这里记录本题评语,评语会最终汇总到总评里'
inbox2.rows = 2
inbox2.style.width = "100%"
// inbox2.addEventListener('change', function () {
// rateboxes = document.getElementsByClassName('ratebox')
// gatherRate = []
// for (let boxid = 0; boxid < rateboxes.length; boxid++) {
// gatherRate.push(rateboxes[boxid].value)
// }
// })
imgCanvas.parentNode.insertBefore(inbox2, imgCanvas.nextSibling)
imgCanvas.parentElement.insertBefore(document.createElement('br'), imgCanvas.nextSibling)
}
function showPreviewImg(link) {
/*
link: 预览图片的附件元素
*/
var br1 = document.createElement('br')
var br2 = document.createElement('br')
var canvas = document.createElement('canvas')
var img = document.createElement('img')
img.src = link[2]
img.className = "previewImg"
canvas.className = "previewCanvas-0"
canvas.appendChild(img)
img.onload = function () {
var ctx = this.parentElement.getContext('2d')
this.parentElement.width = link[3].parentElement.offsetWidth
this.parentElement.height = link[3].parentElement.offsetWidth * this.height / this.width
ctx.drawImage(img, 0, 0, this.width, this.height, 0, 0, this.parentElement.width, this.parentElement.height)
}
link[3].parentNode.insertBefore(br1, link[3].nextSibling)
link[3].parentNode.insertBefore(canvas, link[3].nextSibling)
link[3].parentNode.insertBefore(br2, link[3].nextSibling)
if (settings.enableImageRotation) {
setImgRotate(canvas, "previewImg-")
}
if (settings.enableRateBox) {
addImgRateBox(canvas)
}
}
function addFloatRatebox() {
var insertNode = document.querySelector(".caozuo a")
var inbox2 = document.createElement('textarea')
inbox2.className = 'ratebox'
inbox2.id = "float-ratebox"
inbox2.placeholder = '这里是浮动评语框,点击可以在这里随时记录想写的评语,同样会最终汇总到总评'
inbox2.rows = 2
inbox2.style.width = "90%"
inbox2.style.float = "left"
inbox2.style.border = "black 2px solid"
insertNode.parentNode.appendChild(inbox2)
// inbox2.addEventListener('change', function () {
// rateboxes = document.getElementsByClassName('ratebox')
// gatherRate = []
// for (let boxid = 0; boxid < rateboxes.length; boxid++) {
// gatherRate.push(rateboxes[boxid].value)
// }
// })
}
function moveRateButton() {
var rateStyle = document.createElement('style')
var insertNode = document.querySelector(".caozuo a")
rateStyle.innerHTML =
".caozuo {\
background:rgba(255,255,255,0.6);\
position:fixed!important;\
left:0px!important;\
right:unset!important;\
top:unset!important;\
bottom:0px!important;\
text-align:right!important;\
height:fit-content;\
margin:0px 20% 0px 20%;\
width:70%;\
z-index:100;\
padding:10px;\
}"
insertNode.parentNode.insertBefore(rateStyle, insertNode)
}
function hideSrvBtn() {
var kfButton = document.querySelector('[id*=support-btn]')
kfButton.style.display = "none"
}
function fillRate() {
rateboxes = document.getElementsByClassName('ratebox')
gatherRate = []
for (let boxid = 0; boxid < rateboxes.length; boxid++) {
gatherRate.push(rateboxes[boxid].value)
}
var combinedRates = settings.enableSignature ? gatherRate.join(' ') + signature : gatherRate.join(' ')
if ("" == document.querySelector('.teacherRating-content > span > textarea').value.replace(/\s*/g, "")) {
document.querySelector('.teacherRating-content > span > textarea').value = combinedRates
}
}
function collectRates() {
document.querySelector('.caozuo').addEventListener('click',fillRate )
}
function signOnly() {
if ("" == document.querySelector('.teacherRating-content > span > textarea').value.replace(/\s*/g, "")) {
document.querySelector('.teacherRating-content > span > textarea').value = signature
}
}
function sign() {
document.querySelector('.caozuo').addEventListener('click', signOnly)
}
function gatherNoteLinks() {
document.getElementById("cpyInput").value = noteLinks.join("\n")
document.getElementById("cpyInput").select()
debug("已选中")
document.execCommand("copy")
alert("已复制至剪贴板,可以粘贴到批量下载工具如迅雷使用!")
}
function showCpyAll() {
var tp = document.getElementById("table_points")
var copyAllLinks = document.createElement("button")
var copyArea = document.createElement("textArea")
tp.appendChild(copyAllLinks)
tp.appendChild(copyArea)
copyArea.id = "cpyInput"
copyArea.style.width = "100%"
copyArea.style.zIndex = "-100"
copyArea.style.position = "fixed"
copyAllLinks.id = "copyBtn"
copyAllLinks.style.margin = "10px"
copyAllLinks.style.zIndex = "100"
copyAllLinks.style.position = "fixed"
copyAllLinks.style.bottom = "0"
copyAllLinks.style.right = "0"
copyAllLinks.innerText = "点击复制本页下载链接"
copyAllLinks.title = "点击后可以将所有链接复制到粘贴板,粘贴至各类下载工具可以实现批量下载,无需另装插件"
copyAllLinks.onclick = gatherNoteLinks
}
function showDlBtn() {
noteLinks = [] //清空收集
var lesson_items = document.querySelectorAll("tbody > tr")
for (let ri = 0; ri < resource.length; ri++) {
resource[ri].can_download = 1
if ("dir" == resource[ri].ext){
continue
}
if (settings.enableDownload){
// lesson_url = lesson_items[ri].lastElementChild.lastElementChild
// lesson_url.href = resource[ri].path
noteLinks.push(resource[ri].path)
}
}
debug("捕捉到课件文件")
}
function checkPage() {
if (0 < document.querySelectorAll(".pagination").length){
if (oldPage != parseInt(document.querySelector(".pagination .active").textContent)) {
showDlBtn()
if (settings.enableDownload){
showCpyAll()
}
oldPage = parseInt(document.querySelector(".pagination .active").textContent)
}
}
}
function mainPart() {
/* 根据当前页面选择执行不同功能 */
if (currentURL.includes(TWorkStr)) {
var signature = settings.signatureText
// var gatherRate = []
nuid = homework.next_uid
hid = homework.hid
notMarkCount = homework.not_mark_uids_arr.length
homeworkPara = homework.question_list
questionCount = homeworkPara.length
/* var rateboxes = []
var gatherRate = []
var fileLinks = []
var imgLinks = [] */
attachElements = document.querySelectorAll('div.records-exam-section.s-work-section > div > div > div:not(.change) > a') //捕捉附件
imgInserted = document.querySelectorAll('div.records-exam-section.s-work-section > div > div img:not(.previewImg)') //捕捉插入图片
/* 捕捉作业附件 */
getAttachment()
/* 为所有插入的图片套上一层canvas */
img2canvas()
/* 处理上传附件的列表 */
imgLinks.forEach(function (link) {
/* var inbox = document.createElement('textarea')
inbox.className = 'ratebox'
inbox.placeholder = '点击可以在这里记录本题评语!'
inbox.rows = 2
inbox.style.width = "100%"
inbox.addEventListener('change',function () {
rateboxes = document.getElementsByClassName('ratebox')
gatherRate = []
for (let boxid = 0; boxid < rateboxes.length; boxid++) {
gatherRate.push(rateboxes[boxid].value)
}
})
link[3].parentNode.insertBefore(inbox, link[3].nextSibling) */
/* 创建canvas并在其中绘制图片 */
if (settings.enableImagePreview) {
showPreviewImg(link)
}
})
if (settings.enableRateBox && settings.enableMoveRate) {
addFloatRatebox()
}
/* 为非图片替换原先的下载链接 */
// fileLinks.forEach(function (link) {
// try{
// link[3].href = "javascript:window.open(" + link[2] +")"
// } catch(errFile){
// debug("[Info][injected] Maybe no file.")
// }
// })
/* 评阅按钮移动到底部 */
if (settings.enableMoveRate) {
moveRateButton()
}
/* 收集评语 */
if (settings.enableRateBox) {
collectRates(gatherRate)
}
if (settings.enableSignature) {
sign()
}
//changeNext()
if (settings.enableHideSrv) {
hideSrvBtn()
}
}
/* 学生功能,显示课件的下载按键 */
if (currentURL.includes(lessonStr)) {
resource = lessonindex.lesson_list
if (true) {
showDlBtn()
if (settings.enableDownload){
showCpyAll()
}
var interval = 100;
if (0 < document.querySelectorAll(".pagination").length){
oldPage = parseInt(document.querySelector(".pagination .active").textContent)//获取当前列表页
}
window.setInterval(checkPage, interval)//间隔判断当期页面是否发生改变
}
if (settings.enableHideSrv) {
hideSrvBtn()
}
}
}
var globalDebugMode = true
var settings = JSON.parse(document.getElementById('opt_data').textContent)
var currentURL = window.location.href;
var lessonStr = "S-Lesson-index"
var TWorkStr = "T-Work-treviewinfo"
var signature = settings.signatureText
var gatherRate = []
var homeworkPara
var questionCount
var nuid
var hid
var notMarkCount
var rateboxes = []
var gatherRate = []
var fileLinks = []
var imgLinks = []
var attachElements
var imgInserted
var resource
var oldPage
var noteLinks = []
try {
mainPart()
debug("[Info] Done.")
} catch (error) {
debug(error)
debug("[Info] Wait.")
window.setTimeout(mainPart, 1500)
debug("[Info] Done.")
}