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

Commit ec287df

Browse files
committed
task & default config
1 parent 5c179e2 commit ec287df

File tree

16 files changed

+548
-219
lines changed

16 files changed

+548
-219
lines changed

build/cxmooc-tools/manifest.json

+23-23
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@
2424
"src/background.js"
2525
]
2626
},
27-
"content_scripts": [
28-
{
29-
"matches": [
30-
"*://*/mycourse/studentstudy?*",
31-
"*://*/work/doHomeWorkNew?*",
32-
"*://*/work/selectWorkQuestionYiPiYue?*",
33-
"*://*/exam/test/reVersionTestStartNew?*",
34-
"*://*/ztnodedetailcontroller/visitnodedetail?*",
35-
"*://*/antispiderShowVerify.ac*",
36-
"*://*/html/processVerify.ac?*",
37-
"*://study.zhihuishu.com/learning/videoList*",
38-
"*://*/exam/test/reVersionPaperMarkContentNew?*",
39-
"*://examh5.zhihuishu.com/stuExamWeb.html*",
40-
"*://onlineexamh5new.zhihuishu.com/stuExamWeb.html*",
41-
"*://study.zhihuishu.com/learningNew/videoList*"
42-
],
43-
"js": [
44-
"src/start.js"
45-
],
46-
"run_at": "document_start"
47-
}
48-
],
27+
"content_scripts": [{
28+
"matches": [
29+
"*://*/mycourse/studentstudy?*",
30+
"*://*/work/doHomeWorkNew?*",
31+
"*://*/work/selectWorkQuestionYiPiYue?*",
32+
"*://*/exam/test/reVersionTestStartNew?*",
33+
"*://*/ztnodedetailcontroller/visitnodedetail?*",
34+
"*://*/antispiderShowVerify.ac*",
35+
"*://*/html/processVerify.ac?*",
36+
"*://study.zhihuishu.com/learning/videoList*",
37+
"*://*/exam/test/reVersionPaperMarkContentNew?*",
38+
"*://examh5.zhihuishu.com/stuExamWeb.html*",
39+
"*://onlineexamh5new.zhihuishu.com/stuExamWeb.html*",
40+
"*://study.zhihuishu.com/learningNew/videoList*",
41+
"*://*/knowledge/cards?*"
42+
],
43+
"js": [
44+
"src/start.js"
45+
],
46+
"run_at": "document_start",
47+
"all_frames": true
48+
}],
4949
"permissions": [
5050
"contextMenus",
5151
"tabs",
@@ -60,4 +60,4 @@
6060
"src/mooc.js"
6161
],
6262
"homepage_url": "https://blog.icodef.com"
63-
}
63+
}

src/background.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NewExtensionServerMessage } from "./internal/utils/message";
22
import { HttpUtils } from "./internal/utils/utils";
33
import { Application, Backend, Launcher } from "./internal/application";
4+
import { ConsoleLog } from "./internal/utils/log";
45

56

67
class background implements Launcher {
@@ -25,8 +26,28 @@ class background implements Launcher {
2526
});
2627
}
2728
});
29+
30+
let configKeyList: string[] = new Array();
31+
for (let key in Application.App.config) {
32+
configKeyList.push(key);
33+
}
34+
let configDefaultValue = new Map<string, any>().
35+
set("vtoken", "").set("rand_answer", false).set("auto", true).
36+
set("video_mute", true).set("answer_ignore", false).set("video_cdn", "").
37+
set("video_multiple", 1).set("interval", 2).set("danmu", false);
38+
39+
chrome.storage.sync.get(configKeyList, function (items) {
40+
for (let key in items) {
41+
if (items[key] == undefined) {
42+
chrome.storage.sync.set(key, configDefaultValue.get(key));
43+
}
44+
}
45+
});
2846
}
2947
}
3048

31-
let application = new Application(Backend, new background());
49+
let component = new Map<string, any>().
50+
set("logger", new ConsoleLog());
51+
52+
let application = new Application(Backend, new background(), component);
3253
application.run();

src/internal/application.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Client, NewChromeClientMessage, NewExtensionClientMessage } from "./utils/message";
22
import { HttpUtils } from "./utils/utils";
33
import { SystemConfig, ConfigItems } from "./utils/config";
4+
import { Logger } from "./utils/log";
45

56
export const Backend = "backend";
67
export const Frontend = "frontend";
@@ -35,6 +36,10 @@ export class Application {
3536
return this.component.get("config") as ConfigItems;
3637
}
3738

39+
public get log(): Logger {
40+
return this.component.get("logger") as Logger;
41+
}
42+
3843
public run(): void {
3944
this.launcher.start();
4045
}

src/internal/utils/config.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { Client } from "./message";
2-
import { Application } from "../application";
3-
import { randNumber } from "./utils";
42

53
export interface ConfigItems {
64
vtoken: string
@@ -11,6 +9,7 @@ export interface ConfigItems {
119
video_cdn: string
1210
video_multiple: number
1311
interval: number
12+
danmu: boolean
1413
get(key: string): any
1514
set(key: string, val: any): void
1615
}
@@ -31,6 +30,13 @@ export class ChromeConfigItems implements ConfigItems {
3130
return this.setConfig.SetConfig(key, val);
3231
}
3332

33+
public get danmu() {
34+
return this.getConfig.GetConfig("danmu");
35+
}
36+
public set danmu(val) {
37+
this.setConfig.SetConfig("danmu", val);
38+
}
39+
3440
public get vtoken() {
3541
return this.getConfig.GetConfig("vtoken");
3642
}
@@ -126,13 +132,8 @@ export function NewFrontendGetConfig(): GetConfig {
126132
}
127133

128134
class frontendGetConfig implements GetConfig {
129-
public async GetConfig(key: string): Promise<any> {
130-
let client = Application.App.Client;
131-
let p = new Promise<any>(resolve => ((<Client>client).Recv((data) => {
132-
resolve(data.val);
133-
})));
134-
(<Client>client).Send({ type: "config", key: key });
135-
return p;
135+
public GetConfig(key: string): any {
136+
return localStorage[key];
136137
}
137138
}
138139

src/internal/utils/hook.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ export interface Context {
44
}
55

66
export class Hook {
7-
public func: Function;
7+
public func: Function | string;
88
public stack: Array<Context>;
99
public context: any;
1010

11-
public constructor(func: Function, context?: any) {
11+
public constructor(func: Function | string, context?: any) {
1212
this.context = context || window;
1313
this.func = func;
1414
}
1515

1616
public Middleware(call: Context) {
17-
let old = this.context[this.func.name];
18-
this.context[this.func.name] = function () {
17+
let name: string;
18+
if (typeof this.func == "string") {
19+
name = this.func;
20+
} else {
21+
name = this.func.name;
22+
}
23+
let old = this.context[name];
24+
this.context[name] = function () {
1925
var args = [old];
2026
for (var _i = 0; _i < arguments.length; _i++) {
2127
args[_i + 1] = arguments[_i];

src/internal/utils/log.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
export interface Logger {
2+
Debug(...args: any): Logger;
3+
Info(...args: any): Logger;
4+
Warn(...args: any): Logger;
5+
Error(...args: any): Logger;
6+
Fatal(...args: any): Logger;
7+
}
8+
9+
export class ConsoleLog implements Logger {
10+
11+
protected getNowTime(): string {
12+
let time = new Date();
13+
return time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
14+
}
15+
16+
public Debug(...args: any): Logger {
17+
console.info("[debug", this.getNowTime(), "]", ...args);
18+
return this;
19+
}
20+
21+
public Info(...args: any): Logger {
22+
console.info("[info", this.getNowTime(), "]", ...args);
23+
return this;
24+
}
25+
26+
public Warn(...args: any): Logger {
27+
console.warn("[warn", this.getNowTime(), "]", ...args);
28+
return this;
29+
}
30+
31+
public Error(...args: any): Logger {
32+
console.error("[error", this.getNowTime(), "]", ...args);
33+
return this;
34+
}
35+
36+
public Fatal(...args: any): Logger {
37+
console.error("[fatal", this.getNowTime(), "]", ...args);
38+
return this;
39+
}
40+
41+
}
42+
43+
export class EmptyLog implements Logger {
44+
45+
public Debug(...args: any): Logger {
46+
return this;
47+
}
48+
49+
public Info(...args: any): Logger {
50+
return this;
51+
}
52+
53+
public Warn(...args: any): Logger {
54+
return this;
55+
}
56+
57+
public Error(...args: any): Logger {
58+
return this;
59+
}
60+
61+
public Fatal(...args: any): Logger {
62+
return this;
63+
}
64+
65+
}

src/internal/utils/utils.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,20 @@ export function randNumber(minNum: number, maxNum: number): number {
131131
default:
132132
return 0;
133133
}
134-
}
134+
}
135+
136+
137+
/**
138+
* 创建一个按钮
139+
* @param title
140+
* @param description
141+
* @param id
142+
*/
143+
export function createBtn(title: string, description: string = "", className: string = "", id: string = ""): HTMLElement {
144+
let btn = document.createElement('button');
145+
btn.innerText = title;
146+
btn.id = id;
147+
btn.title = description;
148+
btn.className = className;
149+
return btn;
150+
}

src/mooc.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
import { RemoveInjected } from "./internal/utils/utils";
22
import { Application, Frontend, Launcher } from "./internal/application";
33
import { ChromeConfigItems, NewFrontendGetConfig } from "./internal/utils/config";
4-
import { Mooc, MoocFactory } from "./mooc/factory";
4+
import { Mooc, MoocFactory, CreateMooc } from "./mooc/factory";
55
import { CxCourseFactory } from "./mooc/chaoxing/course";
6+
import { ConsoleLog } from "./internal/utils/log";
67

78
RemoveInjected(document);
89

910
class mooc implements Launcher {
1011
async start(): Promise<void> {
11-
let url = document.URL;
12-
let factory: MoocFactory;
13-
let mooc: Mooc;
14-
if (url.indexOf("mycourse/studentstudy?") > 0) {
15-
factory = new CxCourseFactory();
12+
let mooc = CreateMooc();
13+
if (mooc != null) {
14+
mooc.Start();
1615
}
17-
mooc = factory.CreateMooc();
18-
mooc.Start();
1916
}
2017
}
2118

2219
let component = new Map<string, any>().
23-
set("config", new ChromeConfigItems(NewFrontendGetConfig()));
20+
set("config", new ChromeConfigItems(NewFrontendGetConfig())).
21+
set("logger", new ConsoleLog());
2422

2523
let app = new Application(Frontend, new mooc(), component);
2624
app.run()

src/mooc/chaoxing/course.ts

+59-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
11
import { Mooc, MoocFactory } from "../factory";
2-
import "../../internal/utils/hook"
3-
2+
import { Task, VideoFactory, TaskFactory, TopicFactory } from "./task";
3+
import { Application } from "@App/internal/application";
44
export class CxCourseFactory implements MoocFactory {
55
public CreateMooc(): Mooc {
6-
return new Course();
6+
return new CxCourse();
77
}
88
}
9-
export class Course implements Mooc {
10-
9+
export class CxCourse implements Mooc {
1110
public Start(): void {
11+
document.addEventListener(
12+
"load",
13+
ev => {
14+
var el = <HTMLIFrameElement>(ev.srcElement || ev.target);
15+
if (el.id == "iframe") {
16+
Application.App.log.Info("超星新窗口加载");
17+
this.OperateCard(el);
18+
}
19+
},
20+
true
21+
);
22+
}
23+
24+
public OperateCard(iframe: HTMLIFrameElement) { }
25+
}
1226

27+
export class CxCourseCardFactory implements MoocFactory {
28+
CreateMooc(): Mooc {
29+
return new CxCourseCard();
1330
}
31+
}
1432

33+
export class CxCourseCard implements Mooc {
34+
protected attachments: Array<any>;
35+
Start(): void {
36+
let self = this;
37+
document.addEventListener("readystatechange", function () {
38+
if (document.readyState != "interactive") {
39+
return;
40+
}
41+
Application.App.log.Info("超星任务点启动", document.readyState);
42+
let iframeWindow: any = window;
43+
self.attachments = <Array<any>>iframeWindow.mArg.attachments;
44+
self.attachments.forEach((value: any, index: number) => {
45+
let task: Task;
46+
let taskFactory: TaskFactory;
47+
switch (value.type) {
48+
case "video": {
49+
taskFactory = new VideoFactory();
50+
break;
51+
}
52+
case "workid": {
53+
taskFactory = new TopicFactory();
54+
break;
55+
}
56+
default:
57+
break;
58+
}
59+
task = taskFactory.CreateTask(iframeWindow, value);
60+
task.Complete(function () {
61+
self.taskComplete(task, index);
62+
});
63+
});
64+
Application.App.log.Debug("任务点参数", self.attachments);
65+
});
66+
}
1567

16-
}
68+
protected taskComplete(task: Task, index: number) { }
69+
}

0 commit comments

Comments
 (0)