Skip to content
This repository was archived by the owner on Apr 25, 2022. It is now read-only.

Commit 11db75f

Browse files
committed
修改为老版本逻辑&优化日志
1 parent f9cf681 commit 11db75f

13 files changed

Lines changed: 374 additions & 259 deletions

File tree

src/internal/app/question.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { HttpUtils, removeHTML, randNumber } from "../utils/utils";
2-
import { SystemConfig } from "@App/config";
3-
import { Application } from "../application";
1+
import {HttpUtils, removeHTML, randNumber} from "../utils/utils";
2+
import {SystemConfig} from "@App/config";
3+
import {Application} from "../application";
44

55
export interface Topic {
66
topic: string
@@ -21,6 +21,7 @@ export interface Answer {
2121
answers?: Option[]
2222
correct: Option[]
2323
id?: string
24+
2425
Equal(content1: string, content2: string): boolean
2526
}
2627

@@ -31,20 +32,25 @@ export class PushAnswer implements Answer {
3132
public status: TopicStatus;
3233
public answers: Option[];
3334
public correct: Option[];
35+
3436
public Equal(content1: string, content2: string): boolean {
3537
return content1 == content2;
3638
}
3739
}
3840

3941
export interface Options {
4042
Random(): TopicStatus;
43+
4144
Fill(s: Answer): TopicStatus;
45+
4246
Correct(): Answer
4347
}
4448

4549
export interface Question extends Options {
4650
GetType(): TopicType;
51+
4752
GetTopic(): string;
53+
4854
SetStatus(status: TopicStatus): void;
4955
}
5056

@@ -54,11 +60,17 @@ export type FillType = 1;
5460
// 1随机答案 2不支持的随机答案类型 3无答案 4无符合答案
5561
export type TopicStatus = "ok" | "random" | "no_support_random" | "no_answer" | "no_match";
5662

57-
let statusMap = new Map<TopicStatus, string>();
58-
statusMap.set("ok", "搜索成功").set("random", "随机答案").set("no_support_random", "不支持的随机答案类型").
59-
set("no_answer", "题库中没有搜索到答案").set("no_match", "题库中没有符合的答案");
63+
let topicStatusMap = new Map<TopicStatus, string>();
64+
topicStatusMap.set("ok", "搜索成功").set("random", "随机答案").set("no_support_random", "不支持的随机答案类型").set("no_answer", "题库中没有搜索到答案").set("no_match", "题库中没有符合的答案");
65+
let questionStatusMap = new Map<QuestionStatus, string>();
66+
questionStatusMap.set("success", "搜索成功").set("network", "题库网络错误").set("incomplete", "题库不全").set("processing", "搜索中...");
67+
6068
export function TopicStatusString(status: TopicStatus): string {
61-
return statusMap.get(status);
69+
return topicStatusMap.get(status);
70+
}
71+
72+
export function QuestionStatusString(status: QuestionStatus): string {
73+
return questionStatusMap.get(status);
6274
}
6375

6476
export function SwitchTopicType(title: string): TopicType {
@@ -85,21 +97,26 @@ export type QuestionStatus = "success" | "network" | "incomplete" | "processing"
8597
export type QuestionCallback = (status: QuestionStatus) => void
8698

8799
export type QuestionBankCallback = (args: { status: QuestionStatus, answer: Answer[] }) => void
100+
88101
export interface QuestionBank {
89102
Answer(topic: Topic[], resolve: QuestionBankCallback): void;
103+
90104
Push(answer: Answer[]): Promise<QuestionStatus>;
105+
91106
SetInfo(info: QuestionInfo): void;
92107
}
93108

94109
export interface QuestionInfo {
95110
refer: string
96111
id: string
97112
}
113+
98114
// 小工具题库
99115
export class ToolsQuestionBank implements QuestionBank {
100116

101117
protected platform: string
102118
protected info: QuestionInfo;
119+
103120
constructor(platform: string, info?: QuestionInfo) {
104121
this.platform = platform;
105122
this.info = info;
@@ -158,15 +175,15 @@ export class ToolsQuestionBank implements QuestionBank {
158175
if (status != "success") {
159176
retStatus = status;
160177
}
161-
resolve({ status: "processing", answer: tmpResult });
178+
resolve({status: "processing", answer: tmpResult});
162179
if (t < topic.length) {
163180
next(t);
164181
} else {
165-
return resolve({ status: retStatus, answer: answer });
182+
return resolve({status: retStatus, answer: answer});
166183
}
167184
},
168185
error: () => {
169-
return resolve({ status: "network", answer: answer });
186+
return resolve({status: "network", answer: answer});
170187
}
171188
});
172189
}
@@ -194,18 +211,23 @@ export class ToolsQuestionBank implements QuestionBank {
194211
}
195212

196213
}
214+
197215
export interface QuestionBankFacade {
198216
ClearQuestion(): void
217+
199218
AddQuestion(q: Question): void
219+
200220
Answer(callback: (status: QuestionStatus) => void): void
221+
201222
Push(callback: (status: QuestionStatus) => void): void
202223
}
224+
203225
export class ToolsQuestionBankFacade implements QuestionBankFacade {
204226
protected bank: QuestionBank;
205227
protected question: Array<Question>;
206228

207-
constructor(bank: QuestionBank) {
208-
this.bank = bank;
229+
constructor(platform: string, info?: any) {
230+
this.bank = new ToolsQuestionBank(platform, info);
209231
this.question = new Array<Question>();
210232
}
211233

src/internal/app/topic.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { QuestionBankFacade, ToolsQuestionBankFacade, ToolsQuestionBank, QuestionStatus, Question } from "./question";
2-
import { Application } from "../application";
3-
import { SystemConfig } from "@App/config";
1+
import {
2+
QuestionBankFacade,
3+
QuestionStatus,
4+
Question,
5+
} from "./question";
6+
import {Application} from "../application";
7+
import {SystemConfig} from "@App/config";
48

59
export interface QueryQuestions {
610
QueryQuestions(): Question[];
@@ -31,31 +35,29 @@ export abstract class Topic {
3135
});
3236
}
3337

34-
public QueryAnswer(): Promise<any> {
35-
return new Promise<any>(resolve => {
38+
public QueryAnswer(): Promise<QuestionStatus> {
39+
return new Promise<QuestionStatus>(resolve => {
3640
if (this.lock) {
37-
return resolve("搜索中...");
41+
return resolve("processing");
3842
}
3943
this.lock = true;
4044
Application.App.log.Info("题目搜索中...");
4145
this.addQuestion();
4246
this.answer.Answer((status: QuestionStatus) => {
4347
this.lock = false;
48+
resolve(status);
4449
if (status == "network") {
45-
resolve("网络错误");
4650
return Application.App.log.Fatal("题库无法访问,请查看:" + SystemConfig.url);
4751
} else if (status == "incomplete") {
48-
resolve("答案不全");
4952
return Application.App.log.Warn("题库答案不全,请手动填写操作");
5053
}
51-
resolve();
5254
});
5355
});
5456
}
5557

5658
public CollectAnswer(): Promise<any> {
5759
return new Promise(resolve => {
58-
Application.App.log.Debug("收集考试题目答案", this.context);
60+
Application.App.log.Debug("收集题目答案", this.context);
5961
this.addQuestion();
6062
this.answer.Push((status: QuestionStatus) => {
6163
Application.App.log.Debug("采集答案返回", status);

src/internal/utils/config.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Client, NewExtensionClientMessage, NewChromeClientMessage } from "./message";
2-
import { randNumber } from "./utils";
3-
import { Application } from "../application";
1+
import {randNumber} from "./utils";
2+
import {Application} from "../application";
43

54
export interface ConfigItems extends GetConfig {
65
vtoken: string
@@ -17,13 +16,15 @@ export interface ConfigItems extends GetConfig {
1716
export class ChromeConfigItems implements ConfigItems {
1817

1918
protected getConfig: GetConfig;
19+
2020
constructor(getConfig: GetConfig) {
2121
this.getConfig = getConfig;
2222
}
2323

2424
public GetConfig(key: string) {
2525
return this.getConfig.GetConfig(key);
2626
}
27+
2728
public Watch(key: string | string[], callback: (key: string) => void): void {
2829
this.getConfig.Watch(key, callback);
2930
}
@@ -51,6 +52,10 @@ export class ChromeConfigItems implements ConfigItems {
5152
return this.bool(this.getConfig.GetConfig("auto"));
5253
}
5354

55+
public set auto(val: boolean) {
56+
localStorage["auto"] = val;
57+
}
58+
5459
public get video_mute() {
5560
return this.bool(this.getConfig.GetConfig("video_mute"));
5661
}
@@ -76,6 +81,7 @@ export class ChromeConfigItems implements ConfigItems {
7681

7782
export interface GetConfig {
7883
GetConfig(key: string): any
84+
7985
Watch(key: Array<string> | string, callback: (key: string) => void): void
8086
}
8187

@@ -109,8 +115,8 @@ class backendConfig implements GetConfig, SetConfig {
109115
let info: { [key: string]: number; } = {};
110116
info[key] = val;
111117
chrome.storage.sync.set(info, () => {
112-
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
113-
chrome.tabs.sendMessage(tabs[0].id, { type: "cxconfig", key: key, value: val });
118+
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
119+
chrome.tabs.sendMessage(tabs[0].id, {type: "cxconfig", key: key, value: val});
114120
});
115121
resolve();
116122
});

src/internal/utils/log.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,19 @@ export class PageLog implements Logger {
9393
this.el && (this.el.innerHTML = text);
9494
return this;
9595
}
96+
9697
public Warn(...args: any): Logger {
98+
console.warn("[warn", this.getNowTime(), "]", ...args);
9799
return this;
98100
}
101+
99102
public Error(...args: any): Logger {
103+
console.error("[error", this.getNowTime(), "]", ...args);
100104
return this;
101105
}
106+
102107
public Fatal(...args: any): Logger {
108+
console.error("[fatal", this.getNowTime(), "]", ...args);
103109
return this;
104110
}
105111
}

src/mooc/chaoxing/course.ts

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Mooc, MoocFactory } from "../factory";
2-
import { TaskFactory, Task } from "./task";
3-
import { Application } from "@App/internal/application";
4-
import { VideoFactory } from "./video";
5-
import { TopicFactory, HomeworkTopicFactory, ExamTopicFactory } from "./topic";
6-
import { substrex } from "@App/internal/utils/utils";
1+
import {Mooc} from "../factory";
2+
import {Application} from "@App/internal/application";
3+
import {substrex} from "@App/internal/utils/utils";
4+
import {Task} from "@App/mooc/chaoxing/task";
5+
import {TaskFactory} from "@App/mooc/chaoxing/factory";
76

87
//课程任务
98
export class CxCourse implements Mooc {
@@ -33,41 +32,31 @@ export class CxCourse implements Mooc {
3332
return;
3433
}
3534
let task: Task;
36-
let taskFactory: TaskFactory;
37-
switch (value.type) {
38-
case "video": {
39-
taskFactory = new VideoFactory();
40-
break;
41-
}
42-
case "workid": {
43-
taskFactory = new TopicFactory();
44-
break;
45-
}
46-
default:
47-
return;
35+
task = TaskFactory.CreateCourseTask(iframeWindow, value);
36+
if (!task) {
37+
return;
4838
}
49-
task = taskFactory.CreateTask(iframeWindow, value);
5039
task.jobIndex = index;
5140
this.taskList.push(task);
5241
let taskIndex = this.taskList.length - 1;
5342
task.Load(() => {
5443
if (++loadOverNum == this.taskList.length) {
55-
this.startTask(0);
44+
this.startTask(0, null);
5645
}
5746
});
58-
task.Complete(() => {
59-
this.startTask(taskIndex + 1);
47+
task.Complete(async () => {
48+
this.startTask(taskIndex + 1, task);
6049
});
6150
task.Init();
6251
});
6352
Application.App.log.Debug("任务点参数", this.attachments);
6453
if (this.taskList.length == 0) {
6554
//无任务点
66-
this.startTask(0);
55+
this.startTask(0, null);
6756
}
6857
}
6958

70-
protected startTask(index: number) {
59+
protected async startTask(index: number, nowtask: Task) {
7160
if (Application.App.config.auto) {
7261
//选择未完成的任务点开始
7362
let task: Task;
@@ -77,14 +66,15 @@ export class CxCourse implements Mooc {
7766
if (index == 0) {
7867
task.Start();
7968
} else {
80-
this.delay(() => {
69+
this.delay(async () => {
70+
nowtask && await nowtask.Submit();
8171
task.Start();
8272
});
8373
}
8474
return;
8575
}
8676
}
87-
this.nextPage();
77+
this.nextPage(null, nowtask);
8878
}
8979
}
9080

@@ -108,9 +98,12 @@ export class CxCourse implements Mooc {
10898
return null;
10999
}
110100

111-
protected nextPage(num?: number) {
112-
if (num == undefined) {
113-
return this.delay(() => { this.nextPage(0); });
101+
protected nextPage(num: number, task: Task) {
102+
if (num == null) {
103+
return this.delay(async () => {
104+
task && await task.Submit();
105+
this.nextPage(0, null);
106+
});
114107
}
115108
let el = <HTMLElement>document.querySelector("span.currents ~ span");
116109
if (el != undefined) {
@@ -129,7 +122,8 @@ export class CxCourse implements Mooc {
129122
Application.App.log.Fatal("被锁卡住了,请手动处理");
130123
return alert("被锁卡住了,请手动处理");
131124
}
132-
this.nextPage(num + 1);
125+
Application.App.log.Info("等待解锁");
126+
this.nextPage(num + 1, null);
133127
}, 5000);
134128
}
135129
(<any>el.parentElement.querySelector("a>span")).click();
@@ -140,8 +134,7 @@ export class CxCourse implements Mooc {
140134
export class CxExamTopic implements Mooc {
141135
public Start(): void {
142136
window.onload = () => {
143-
let topic = new ExamTopicFactory();
144-
let task = topic.CreateTask(window, {
137+
let task = TaskFactory.CreateExamTopicTask(window, {
145138
refer: document.URL,
146139
id: (<HTMLInputElement>document.querySelector("#paperId")).value,
147140
});
@@ -158,8 +151,7 @@ export class CxExamTopic implements Mooc {
158151
export class CxHomeWork implements Mooc {
159152
public Start(): void {
160153
window.onload = () => {
161-
let topic = new HomeworkTopicFactory();
162-
let task = topic.CreateTask(window, {
154+
let task = TaskFactory.CreateHomeworkTopicTask(window, {
163155
refer: document.URL,
164156
id: substrex(document.URL, "&workId=", "&"),
165157
info: (<HTMLInputElement>document.querySelector("#workLibraryId") || <HTMLInputElement>document.querySelector("#cid")).value
@@ -170,4 +162,4 @@ export class CxHomeWork implements Mooc {
170162
}
171163
}
172164
}
173-
}
165+
}

0 commit comments

Comments
 (0)