Skip to content

Commit 5a12ec9

Browse files
author
wuwei
committed
1、新增查看作业自动读取答案,答题模式自动或手动写入答案。
1 parent a1e546f commit 5a12ec9

7 files changed

Lines changed: 194 additions & 45 deletions

File tree

StudyChromeApp/1.png

-2.46 KB
Binary file not shown.
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11

2+
var test = parent.document.getElementById("w_code");
3+
if(test){
4+
5+
// 判断是不是左边的树
6+
if($("#divTree").length > 0){
7+
var model = 1;
8+
chrome.storage.local.get('mode',function(str){
9+
model = str.mode;
10+
if(model == 1){
11+
console.log("自动学习模式。。。");
12+
mode1();
13+
} else if(mode = 2) {
14+
console.log("周期学习模式。。。");
15+
mode2();
16+
}
17+
});
18+
}
19+
}
220

321

4-
var model = 1;
5-
6-
chrome.storage.local.get('mode',function(str){
7-
model = str.mode;
822

923

10-
if(model == 1){
11-
console.log("自动学习模式。。。");
12-
mode1();
13-
} else if(mode = 2) {
14-
console.log("周期学习模式。。。");
15-
mode2();
16-
}
17-
});
1824

1925

2026
/**
@@ -178,10 +184,14 @@ function mode2(){
178184
*/
179185
chrome.storage.onChanged.addListener(function callback(changes, name){
180186
if('local' == name){
181-
STUDY_PERIOD_TIME = changes.time.newValue;
187+
if(changes.time){
182188

183-
clearInterval(myInterval);
184-
myInterval = setInterval(study, STUDY_PERIOD_TIME * 1000);
189+
STUDY_PERIOD_TIME = changes.time.newValue;
190+
191+
clearInterval(myInterval);
192+
myInterval = setInterval(study, STUDY_PERIOD_TIME * 1000);
193+
194+
}
185195
}
186196
});
187197

StudyChromeApp/js/homework.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+

2+
$(function(){
3+
// 作业答案
4+
if($("#_block_exam_1").length > 0){
5+
homeworkAnswer();
6+
}
7+
8+
// 写入答案
9+
if($("#tblDataList").length > 0){
10+
11+
/**
12+
* 监听周期值变化
13+
*/
14+
chrome.storage.onChanged.addListener(function callback(changes, name){
15+
if('local' == name){
16+
if(changes.writeAnswert){
17+
if($("#_block_exam_1").text().indexOf("查看考卷") == -1) { // 不是查看考卷才写
18+
chrome.storage.local.get('answers',function(str){
19+
answers = str.answers;
20+
if(answers){
21+
// 做作业吧
22+
doHomework(answers);
23+
alert("答题完成!")
24+
}
25+
});
26+
}
27+
}
28+
}
29+
});
30+
}
31+
32+
});
33+
34+
35+
36+
function homeworkAnswer(){
37+
var listTable = $("#_block_content_exam_1>form>table>tbody")[0]
38+
var rows = listTable.rows;
39+
var results = [];
40+
for(var i=0; i<rows.length; i++){
41+
console.log("current index "+ i)
42+
var item = rows[i];
43+
44+
var tagName = item.children[1].children[1].children[0].children[1].children[0].children[0].tagName;
45+
var text = "";
46+
console.log("current tagName= "+ tagName)
47+
if("SPAN" == tagName){
48+
try{
49+
text = item.children[1].children[1].children[0].children[1].children[0].children[0].children[0].children[1].innerText;
50+
} catch(e) {}
51+
try{
52+
item.children[1].children[1].children[0].children[1].children[0].children[0].children[1].innerText;
53+
} catch(e) {}
54+
}else{
55+
text = item.children[1].children[1].children[0].children[1].children[0].children[1].innerText;
56+
}
57+
58+
59+
60+
// "[参考答案:B]  分值:5"
61+
62+
var index_a = text.indexOf(":")+1;
63+
var index_b = text.indexOf("]");
64+
var result = text.substring(index_a, index_b);
65+
66+
results.push(result);
67+
}
68+
console.log(results.join("|"));
69+
70+
71+
chrome.storage.local.set({'answers': results.join("|") },function(){
72+
alert("答案读取成功!");
73+
});
74+
75+
}
76+
77+
78+
79+
80+
81+
82+
/**
83+
* 自动做作业函数
84+
* @author marker
85+
* @param answerStr
86+
*/
87+
function doHomework(answerStr){
88+
var ans = answerStr.split("|");// 答案列表
89+
var questionNode = $("#tblDataList>tbody")[0];
90+
var rows = questionNode.rows;
91+
92+
93+
for(var i=0; i<rows.length; i++) {
94+
var item = rows[i];
95+
var right = ans[i];//正确答案 A =65
96+
if(!right){
97+
continue;// 不存在就继续走
98+
}
99+
console.log(right);
100+
if(right.length <= 1) {// 单项选择
101+
102+
console.log(i+".单选题:"+right);
103+
104+
var rightIndex = right.charCodeAt() - 65;// 0开始
105+
// 答案里列表
106+
var answerListNode = item.children[0].children[0].children[0].children[0].children[1].children[1].children[0].children[1].children[0].children[0].children[0].children[0]
107+
if(answerListNode.children.length == 1){
108+
// 选中答案
109+
try{// 尝试选择判断题/单选题
110+
if(rightIndex == 0){
111+
answerListNode.children[0].children[0].children[0].click();
112+
} else if(rightIndex == 1){
113+
answerListNode.children[0].children[3].children[0].click();
114+
} else if(rightIndex == 2){
115+
answerListNode.children[0].children[6].children[0].click();
116+
} else if(rightIndex == 3){
117+
answerListNode.children[0].children[9].children[0].click();
118+
}
119+
}catch(e){}
120+
121+
}else{//
122+
// 选中答案
123+
try{// 尝试选择判断题/单选题
124+
answerListNode.rows[rightIndex].children[0].children[0].click();
125+
}catch(e){}
126+
}
127+
128+
129+
}else{// 多选题
130+
131+
console.log(i+".多选题:"+right);
132+
133+
for(var j=0;j<right.length; j++){
134+
var crooet = right[j];
135+
var rightIndex = crooet.charCodeAt() - 65;
136+
// 答案里列表
137+
var answerListNode = item.children[0].children[0].children[0].children[0].children[1].children[1].children[0].children[1].children[0].children[0].children[0].children[0]
138+
139+
140+
141+
142+
143+
// 选中答案
144+
try{// 尝试选择判断题
145+
answerListNode.rows[rightIndex].children[0].children[0].click();
146+
}catch(e){console.log(e)}
147+
}
148+
149+
150+
}
151+
152+
153+
154+
155+
}
156+
157+
158+
}
159+

StudyChromeApp/js/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ $(function(){
7777
});
7878

7979

80+
$("#myModelWrite").click(function(){
81+
82+
chrome.storage.local.set({'writeAnswert': Math.random()*1000000});
83+
84+
});
8085

8186
});
8287

StudyChromeApp/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
},
1010
"content_scripts": [
1111
{
12-
"matches": ["*://*/*","http://learning.uestcedu.com/*"],
13-
"js": ["lib/jquery-1.4.3.min.js", "script.js"],
12+
"matches": ["http://learning.uestcedu.com/*"],
13+
"js": ["lib/jquery-1.4.3.min.js", "js/autostudy.js", "js/homework.js"],
1414
"run_at": "document_end"
1515
,
1616
"all_frames": true

StudyChromeApp/popup.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
<div id="inputTimeRoundEl">
6565
周期: <input id="inputTimeRound" type="text" value="80" />
6666
</div>
67-
67+
</div>
68+
<div class="taskItem" id="myModelWrite" >
69+
<label class="on"></label><span class="taskTitle">写入答案</span>
6870
</div>
6971

7072
<div class="taskItem" id="site">

StudyChromeApp/popup.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)