forked from Xu22Web/tech-study-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtech-study.js
2388 lines (2381 loc) · 82.2 KB
/
tech-study.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name 不学习何以强国
// @namespace http://tampermonkey.net/
// @version 20221008
// @description 有趣的 `学习强国` 油猴插件。读文章,看视频,做习题。问题反馈: https://github.com/Xu22Web/tech-study-js/issues 。
// @author 原作者:techxuexi 荷包蛋。现作者:Xu22Web
// @match https://www.xuexi.cn/*
// @match https://pc.xuexi.cn/points/exam-practice.html
// @match https://pc.xuexi.cn/points/exam-weekly-detail.html?id=*
// @match https://pc.xuexi.cn/points/exam-paper-detail.html?id=*
// @require https://cdn.jsdelivr.net/npm/[email protected]
// @run-at document-start
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_openInTab
// @grant GM_addElement
// ==/UserScript==
/**
* @description url配置
*/
const URL_CONFIG = {
// 主页
home: /^https\:\/\/www\.xuexi\.cn(\/(index\.html)?)?$/,
// 每日答题页面
examPractice: 'https://pc.xuexi.cn/points/exam-practice.html',
// 每周答题页面
examWeekly: 'https://pc.xuexi.cn/points/exam-weekly-detail.html',
// 专项练习页面
examPaper: 'https://pc.xuexi.cn/points/exam-paper-detail.html',
// 登录界面
login: 'https://login.xuexi.cn/login/xuexiWeb?appid=dingoankubyrfkttorhpou&goto=https%3A%2F%2Foa.xuexi.cn&type=1&state=ffdea2ded23f45ab%2FKQreTlDFe1Id3B7BVdaaYcTMp6lsTBB%2Fs3gGevuMKfvpbABDEl9ymG3bbOgtpSN&check_login=https%3A%2F%2Fpc-api.xuexi.cn',
};
/**
* @description api配置
*/
const API_CONFIG = {
// 用户信息
userInfo: 'https://pc-api.xuexi.cn/open/api/user/info',
// 总分
totalScore: 'https://pc-api.xuexi.cn/open/api/score/get',
// 当天分数
todayScore: 'https://pc-api.xuexi.cn/open/api/score/today/query',
// 任务列表
taskList: 'https://pc-proxy-api.xuexi.cn/api/score/days/listScoreProgress?sence=score&deviceType=2',
// 新闻数据
todayNews: [
'https://www.xuexi.cn/lgdata/35il6fpn0ohq.json',
'https://www.xuexi.cn/lgdata/1ap1igfgdn2.json',
'https://www.xuexi.cn/lgdata/1novbsbi47k.json',
'https://www.xuexi.cn/lgdata/vdppiu92n1.json',
'https://www.xuexi.cn/lgdata/152mdtl3qn1.json',
],
// 视频数据
todayVideos: [
'https://www.xuexi.cn/lgdata/525pi8vcj24p.json',
'https://www.xuexi.cn/lgdata/11vku6vt6rgom.json',
'https://www.xuexi.cn/lgdata/2qfjjjrprmdh.json',
'https://www.xuexi.cn/lgdata/3o3ufqgl8rsn.json',
'https://www.xuexi.cn/lgdata/591ht3bc22pi.json',
'https://www.xuexi.cn/lgdata/1742g60067k.json',
'https://www.xuexi.cn/lgdata/1novbsbi47k.json',
],
// 每周答题列表
weeklyList: 'https://pc-proxy-api.xuexi.cn/api/exam/service/practice/pc/weekly/more',
// 专项练习列表
paperList: 'https://pc-proxy-api.xuexi.cn/api/exam/service/paper/pc/list',
// 文本服务器保存答案
answerSave: 'https://a6.qikekeji.com/txt/data/save',
// 文本服务器获取答案
answerSearch: 'https://api.answer.uu988.xyz:4545/answer/search',
};
/**
* @description 获取cookie
* @param name
* @returns
*/
function getCookie(name) {
// 获取当前所有cookie
const strCookies = document.cookie;
// 截取变成cookie数组
const cookieText = strCookies.split(';');
// 循环每个cookie
for (const i in cookieText) {
// 将cookie截取成两部分
const item = cookieText[i].split('=');
// 判断cookie的name 是否相等
if (item[0].trim() === name) {
return item[1].trim();
}
}
return null;
}
/**
* @description 防抖
* @param callback
* @param delay
* @returns
*/
function debounce(callback, delay) {
let timer = -1;
return function (...args) {
if (timer !== -1) {
clearTimeout(timer);
}
timer = setTimeout(() => {
callback.apply(this, args);
}, delay);
};
}
/**
* @description 选择器
* @param selector
* @returns
*/
function $$(selector) {
return Array.from(document.querySelectorAll(selector));
}
/**
* @description 关闭子窗口
*/
function closeWin() {
try {
window.opener = window;
const win = window.open('', '_self');
win?.close();
top?.close();
}
catch (e) { }
}
/**
* @description 等待窗口关闭
* @param newPage
* @returns
*/
function waitingClose(newPage) {
return new Promise((resolve) => {
const doing = setInterval(() => {
if (newPage.closed) {
clearInterval(doing); // 停止定时器
resolve('done');
}
}, 1000);
});
}
/**
* @description 等待时间
* @param time
* @returns
*/
function waitingTime(time) {
if (!Number.isInteger(time)) {
time = 1000;
}
return new Promise((resolve) => {
setTimeout(() => {
resolve('done');
}, time);
});
}
/**
* @description 判断是否为移动端
* @returns
*/
function hasMobile() {
let isMobile = false;
if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
console.log('移动端');
isMobile = true;
}
if (document.body.clientWidth < 800) {
console.log('小尺寸设备端');
isMobile = true;
}
return isMobile;
}
/**
* @description 创建元素节点
* @param eleName
* @param props
* @param attrs
* @param children
* @returns
*/
function creatElementNode(eleName, props, attrs, children) {
// 元素
let ele;
// 格式化元素名
const formatEleName = eleName.toLowerCase();
// 需要命名空间的svg元素
const specficSVGElement = [
'svg',
'use',
'circle',
'rect',
'line',
'marker',
'linearGradient',
'g',
'path',
];
// 需要命名空间的html元素
const specficHTMLElement = 'html';
if (formatEleName === specficHTMLElement) {
// html元素命名空间
const ns = 'http://www.w3.org/1999/xhtml';
// 创建普通元素
ele = document.createElementNS(ns, formatEleName);
}
else if (specficSVGElement.includes(formatEleName)) {
// svg元素命名空间
const ns = 'http://www.w3.org/2000/svg';
// 创建普通元素
ele = document.createElementNS(ns, formatEleName);
}
else {
// 创建普通元素
ele = document.createElement(formatEleName);
}
// props属性设置
for (const key in props) {
if (props[key] instanceof Object) {
for (const subkey in props[key]) {
ele[key][subkey] = props[key][subkey];
}
}
else {
ele[key] = props[key];
}
}
// attrs属性设置
for (const key in attrs) {
// 属性值
const value = attrs[key];
// 处理完的key
const formatKey = key.toLowerCase();
// xlink命名空间
if (formatKey.startsWith('xlink:')) {
// xlink属性命名空间
const attrNS = 'http://www.w3.org/1999/xlink';
if (value) {
ele.setAttributeNS(attrNS, key, value);
}
else {
ele.removeAttributeNS(attrNS, key);
}
}
else if (formatKey.startsWith('on')) {
// 事件监听
const [, eventType] = key.toLowerCase().split('on');
// 事件类型
if (eventType) {
// 回调函数
if (value instanceof Function) {
ele.addEventListener(eventType, value);
// 回调函数数组
}
else if (value instanceof Array) {
for (const i in value) {
// 回调函数
if (value[i] instanceof Function) {
ele.addEventListener(eventType, value[i]);
}
}
}
}
}
else {
// 特殊属性
const specificAttrs = ['checked', 'selected', 'disabled', 'enabled'];
if (specificAttrs.includes(key) && value) {
ele.setAttribute(key, '');
}
else {
if (value) {
ele.setAttribute(key, value);
}
else {
ele.removeAttribute(key);
}
}
}
}
// 子节点
if (children) {
if (children instanceof Array) {
if (children.length === 1) {
ele.append(children[0]);
}
else {
// 文档碎片
const fragment = document.createDocumentFragment();
for (const i in children) {
fragment.append(children[i]);
}
ele.append(fragment);
}
}
else {
ele.append(children);
}
}
return ele;
}
/**
* @description 创建文字节点
* @param text
* @returns
*/
function createTextNode(...text) {
if (text && text.length === 1) {
return document.createTextNode(text[0]);
}
const fragment = document.createDocumentFragment();
for (const i in text) {
const textEle = document.createTextNode(text[i]);
fragment.append(textEle);
}
return fragment;
}
const css = ':root {\n --themeColor: #fa3333;\n --scale: 1;\n font-size: calc(10px * var(--scale));\n}\n.icon {\n width: 1em;\n height: 1em;\n vertical-align: -0.15em;\n fill: currentColor;\n overflow: hidden;\n}\n.egg_btn {\n transition: 0.5s;\n outline: none;\n border: none;\n padding: 1.2rem 2rem;\n border-radius: 1.2rem;\n cursor: pointer;\n font-size: 1.8rem;\n font-weight: bold;\n text-align: center;\n color: rgb(255, 255, 255);\n background: #666777;\n}\n.egg_btn.manual {\n background: #e3484b;\n}\n.egg_setting_box {\n position: fixed;\n top: 7rem;\n left: 1rem;\n padding: 1.2rem 2rem;\n border-radius: 1rem;\n background: #fff;\n box-shadow: 0 0 0.4rem 0.1rem #ccc;\n transition: 80ms ease-out;\n z-index: 99999;\n font-family: Noto Sans SC;\n}\n.egg_setting_box hr {\n height: 0.1rem;\n border: none;\n background: #eee;\n position: relative;\n margin: 0.8rem 0;\n}\n.egg_setting_box hr:after {\n content: attr(data-category);\n position: absolute;\n transform: translate(calc(-50%), calc(-50%));\n left: 50%;\n top: 50%;\n font-size: 1.2rem;\n color: #999;\n background: white;\n padding: 0.1rem 0.6rem;\n}\n.egg_setting_item {\n margin-top: 0.5rem;\n min-height: 3rem;\n min-width: 20rem;\n font-size: 1.6rem;\n display: flex;\n align-items: center;\n justify-content: space-between;\n}\n.egg_info {\n flex-direction: column;\n align-items: stretch;\n}\n.egg_userinfo {\n display: flex;\n justify-content: space-between;\n align-items: center;\n}\n.egg_login_status {\n display: flex;\n justify-content: center;\n align-items: center;\n}\n.egg_login_status button {\n outline: none;\n padding: 0.4rem 0.8rem;\n background: #ccc;\n font-size: 1.4rem;\n border: none;\n border-radius: 1rem;\n color: white;\n cursor: pointer;\n}\n.egg_login_status.active {\n flex-grow: 1;\n}\n.egg_login_status.active button {\n background: var(--themeColor);\n padding: 0.8rem 2.4rem;\n}\n.egg_userinfo .egg_user {\n display: flex;\n justify-content: center;\n align-items: center;\n padding: 0.5rem 0;\n}\n.egg_userinfo .egg_user .egg_sub_nickname,\n.egg_userinfo .egg_user .egg_avatar_img {\n height: 5rem;\n width: 5rem;\n border-radius: 50%;\n background: var(--themeColor);\n display: flex;\n justify-content: center;\n align-items: center;\n text-overflow: ellipsis;\n overflow: hidden;\n white-space: nowrap;\n font-size: 2rem;\n color: white;\n}\n.egg_userinfo .egg_user .egg_name {\n padding-left: 0.5rem;\n text-overflow: ellipsis;\n overflow: hidden;\n white-space: nowrap;\n max-width: 10rem;\n}\n.egg_scoreinfo {\n display: flex;\n justify-content: space-between;\n align-items: center;\n padding-top: 0.8rem;\n}\n.egg_scoreinfo .egg_totalscore,\n.egg_scoreinfo .egg_todayscore {\n font-size: 1.2rem;\n}\n.egg_scoreinfo span {\n color: var(--themeColor);\n padding-left: 0.4rem;\n font-weight: bold;\n}\n.egg_setting_item label {\n flex-grow: 1;\n}\n.egg_progress {\n display: flex;\n justify-content: space-between;\n align-items: center;\n padding: 0.5rem 0;\n}\n.egg_progress .egg_track {\n background: #ccc;\n height: 0.5rem;\n border-radius: 1rem;\n flex: 1 1 auto;\n overflow: hidden;\n box-shadow: -0.1rem 0.1rem 0.1rem -0.1rem #999 inset,\n 0.1rem 0.1rem 0.1rem -0.1rem #999 inset;\n}\n.egg_progress .egg_track .egg_bar {\n height: 0.5rem;\n background: var(--themeColor);\n border-radius: 1rem;\n width: 0;\n transition: width 0.5s;\n}\n.egg_progress .egg_percent {\n font-size: 1.2rem;\n padding-left: 0.5rem;\n width: 3.5rem;\n}\ninput[type=\'checkbox\'].egg_setting_switch {\n cursor: pointer;\n margin: 0;\n outline: 0;\n appearance: none;\n -webkit-appearance: none;\n -moz-appearance: none;\n position: relative;\n width: 4.2rem;\n height: 2.2rem;\n background: #ccc;\n border-radius: 5rem;\n transition: background 0.3s;\n --border-padding: 0.5rem;\n box-shadow: -0.1rem 0 0.1rem -0.1rem #999 inset,\n 0.1rem 0 0.1rem -0.1rem #999 inset;\n}\ninput[type=\'checkbox\'].egg_setting_switch::after {\n content: \'\';\n display: inline-block;\n width: 1.4rem;\n height: 1.4rem;\n border-radius: 50%;\n background: #fff;\n box-shadow: 0 0 0.2rem #999;\n transition: 0.4s;\n position: absolute;\n top: calc(50% - (1.4rem / 2));\n position: absolute;\n left: var(--border-padding);\n}\ninput[type=\'checkbox\'].egg_setting_switch:checked {\n background: var(--themeColor);\n}\ninput[type=\'checkbox\'].egg_setting_switch:checked::after {\n left: calc(100% - var(--border-padding) - 1.4rem);\n}\n.tip {\n background: #ccc;\n color: white;\n border-radius: 10rem;\n font-size: 1.2rem;\n width: 1.6rem;\n height: 1.6rem;\n margin-left: 0.4rem;\n display: inline-block;\n text-align: center;\n line-height: 1.6rem;\n cursor: pointer;\n}\n.egg_start_btn {\n justify-content: center;\n}\n.egg_study_btn {\n outline: none;\n background: var(--themeColor);\n padding: 0.8rem 2.4rem;\n font-size: 1.4rem;\n border: none;\n border-radius: 1rem;\n color: white;\n cursor: pointer;\n transition: all 0.3s;\n}\n.egg_study_btn:hover {\n opacity: 0.8;\n}\n@keyframes fade {\n from {\n opacity: 0.8;\n }\n to {\n opacity: 0.4;\n background: #ccc;\n }\n}\n.egg_study_btn.loading {\n animation: fade 2s ease infinite alternate;\n}\n.egg_study_btn.disabled {\n background: #ccc;\n}\n.egg_tip {\n position: fixed;\n bottom: 2rem;\n left: 2rem;\n padding: 1.2rem 1.4rem;\n border: none;\n border-radius: 1rem;\n background: var(--themeColor);\n color: white;\n font-size: 1.4rem;\n transition: 0.3s ease;\n font-family: Noto Sans SC;\n z-index: 99999;\n}\n.egg_tip.inactive {\n opacity: 0;\n transform: scale(0.9) translateY(1rem);\n}\n.egg_tip.active {\n opacity: 1;\n transform: scale(1) translateY(0);\n}\n.egg_tip .egg_countdown {\n display: inline-block;\n color: var(--themeColor);\n background: white;\n border-radius: 0.5rem;\n padding: 0.2rem 0.4rem;\n font-weight: bold;\n margin-left: 0.4rem;\n font-size: 1.2rem;\n}\n.egg_frame {\n position: relative;\n box-sizing: border-box;\n margin: 0 auto;\n}\n.egg_frame.active {\n padding: 0.4rem;\n width: 21.8rem;\n height: 21.8rem;\n overflow: hidden;\n}\n.egg_frame .egg_frame_login {\n position: absolute;\n left: -6.9rem;\n top: -2.6rem;\n}\n.egg_frame iframe {\n width: 284px;\n height: 241px;\n border: none;\n transform: scale(var(--scale));\n transform-origin: top left;\n}\n';
// 嵌入样式
GM_addStyle(css);
GM_addElement(document.head, 'link', {
rel: 'preconnect',
href: 'https://fonts.googleapis.com',
});
GM_addElement(document.head, 'link', {
rel: 'preconnect',
href: 'https://fonts.gstatic.com',
crossorigin: 'crossorigin',
});
GM_addElement(document.head, 'link', {
rel: 'preconnect',
href: 'https://fonts.googleapis.com',
crossorigin: 'crossorigin',
});
GM_addElement(document.head, 'link', {
href: 'https://fonts.googleapis.com/css2?family=Noto+Sans+SC&display=swap',
crossorigin: 'crossorigin',
rel: 'stylesheet',
});
// <link rel="preconnect" href="https://fonts.googleapis.com">
// <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
// <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC&display=swap" rel="stylesheet">
/* Config·配置 */
// 每周答题开启逆序答题: false: 顺序答题; true: 逆序答题
const examWeeklyReverse = true;
// 专项答题开启逆序答题: false: 顺序答题; true: 逆序答题
const examPaperReverse = true;
// 答题请求速率限制
const ratelimitms = 3000;
// 单次最大新闻数
const maxNewsNum = 6;
// 单次最大视频数
const maxVideoNum = 6;
/* Config End·配置结束 */
/* Tools·工具函数 */
// 暂停锁
function pauseLock(callback) {
return new Promise((resolve) => {
// 学习暂停
const pauseStudy = (GM_getValue('pauseStudy') || false);
if (pauseStudy) {
pauseExam(pauseStudy);
}
if (pause) {
const doing = setInterval(() => {
if (!pause) {
// 停止定时器
clearInterval(doing);
console.log('答题等待结束!');
if (callback && callback instanceof Function) {
callback('done');
}
resolve('done');
return;
}
if (callback && callback instanceof Function) {
callback('pending');
}
console.log('答题等待...');
}, 500);
return;
}
resolve('done');
});
}
// 暂停学习锁
function pauseStudyLock(callback) {
return new Promise((resolve) => {
if (pauseStudy) {
const doing = setInterval(() => {
if (!pauseStudy) {
// 停止定时器
clearInterval(doing);
console.log('学习等待结束!');
if (callback && callback instanceof Function) {
callback('done');
}
resolve('done');
return;
}
if (callback && callback instanceof Function) {
callback('pending');
}
console.log('学习等待...');
}, 500);
return;
}
resolve('done');
});
}
/* Tools End·工具函数结束 */
/* API请求函数 */
// 获取用户信息
async function getUserInfo() {
try {
const res = await fetch(API_CONFIG.userInfo, {
method: 'GET',
credentials: 'include',
});
// 请求成功
if (res.ok) {
const { data } = await res.json();
return data;
}
}
catch (err) { }
}
// 获取总积分
async function getTotalScore() {
try {
const res = await fetch(API_CONFIG.totalScore, {
method: 'GET',
credentials: 'include',
});
// 请求成功
if (res.ok) {
const { data } = await res.json();
// 总分
const { score } = data;
return score;
}
}
catch (err) { }
}
// 获取当天总积分
async function getTodayScore() {
try {
const res = await fetch(API_CONFIG.todayScore, {
method: 'GET',
credentials: 'include',
});
// 请求成功
if (res.ok) {
const { data } = await res.json();
// 当天总分
const { score } = data;
return score;
}
}
catch (err) { }
}
// 获取任务列表
async function getTaskList() {
try {
const res = await fetch(API_CONFIG.taskList, {
method: 'GET',
credentials: 'include',
});
// 请求成功
if (res.ok) {
const { data } = await res.json();
// 进度和当天总分
const { taskProgress } = data;
return taskProgress;
}
}
catch (err) { }
}
// 获取新闻数据
async function getTodayNews() {
// 随机
const randNum = ~~(Math.random() * API_CONFIG.todayNews.length);
try {
// 获取重要新闻
const res = await fetch(API_CONFIG.todayNews[randNum], {
method: 'GET',
});
// 请求成功
if (res.ok) {
const data = await res.json();
return data;
}
}
catch (err) { }
}
// 获取视频数据
async function getTodayVideos() {
// 随机
const randNum = ~~(Math.random() * API_CONFIG.todayVideos.length);
try {
// 获取重要新闻
const res = await fetch(API_CONFIG.todayVideos[randNum], {
method: 'GET',
});
// 请求成功
if (res.ok) {
const data = await res.json();
return data;
}
}
catch (err) { }
}
// 专项练习数据
async function getExamPaper(pageNo) {
// 链接
const url = `${API_CONFIG.paperList}?pageSize=50&pageNo=${pageNo}`;
try {
// 获取专项练习
const res = await fetch(url, {
method: 'GET',
credentials: 'include',
});
// 请求成功
if (res.ok) {
const data = await res.json();
const paperJson = decodeURIComponent(escape(window.atob(data.data_str.replace(/-/g, '+').replace(/_/g, '/'))));
// JSON格式化
const paper = JSON.parse(paperJson);
return paper;
}
}
catch (err) {
return [];
}
return [];
}
// 每周答题数据
async function getExamWeekly(pageNo) {
// 链接
const url = `${API_CONFIG.weeklyList}?pageSize=50&pageNo=${pageNo}`;
try {
// 获取每周答题
const res = await fetch(url, {
method: 'GET',
credentials: 'include',
});
// 请求成功
if (res.ok) {
const data = await res.json();
const paperJson = decodeURIComponent(escape(window.atob(data.data_str.replace(/-/g, '+').replace(/_/g, '/'))));
// JSON格式化
const paper = JSON.parse(paperJson);
return paper;
}
}
catch (err) {
return [];
}
return [];
}
// 获取答案
async function getAnswer(question) {
console.log('获取网络答案');
// 数据
const data = {
question,
};
try {
// 请求
const res = await fetch(API_CONFIG.answerSearch, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
// 请求成功
if (res.ok) {
const data = await res.json();
// 状态
const { errno } = data;
if (errno !== -1) {
// 答案
const { answers } = data.data;
console.log('answers', answers);
return answers;
}
}
}
catch (error) { }
return [];
}
// 保存答案
async function saveAnswer(key, value) {
// 内容
const content = JSON.stringify([{ title: key, content: value }]);
// 数据
const data = {
txt_name: key,
txt_content: content,
password: '',
v_id: '',
};
// 请求体
const body = Object.keys(data)
.map((key) => {
return `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`;
})
.join('&');
// 请求
const res = await fetch(API_CONFIG.answerSave, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body,
});
// 请求成功
if (res.ok) {
try {
const data = await res.json();
return data;
}
catch (err) {
return null;
}
}
return null;
}
/* API请求函数结束 */
/* 变量 */
// 任务进度
const tasks = [];
// 获取 URL
const { href } = window.location;
// 设置
let settings = [true, true, true, true, true, false, false, false, false];
// 已经开始
let started = false;
// 是否暂停答题
let pause = false;
// 是否暂停学习
let pauseStudy = false;
// 初始化登录状态
let login = !!getCookie('token');
// 用户信息
let userInfo;
// 新闻
let news = [];
// 视频
let videos = [];
// load
window.addEventListener('load', () => {
console.log('加载脚本');
// 主页
if (URL_CONFIG.home.test(href)) {
console.log('进入主页面!');
let ready = setInterval(() => {
if ($$('.text-wrap')[0]) {
window.addEventListener('beforeunload', () => {
// 全局暂停
if (GM_getValue('pauseStudy') !== false) {
GM_setValue('pauseStudy', false);
}
});
// 停止定时器
clearInterval(ready);
// 设置字体
initFontSize();
// 初始化设置
initSetting();
// 渲染菜单
renderMenu();
}
}, 800);
}
else if (typeof GM_getValue('readingUrl') === 'string' &&
href === GM_getValue('readingUrl')) {
// 初始化设置
initSetting();
console.log('初始化设置');
console.log(settings);
reading(0);
}
else if (typeof GM_getValue('watchingUrl') === 'string' &&
href === GM_getValue('watchingUrl')) {
// 初始化设置
initSetting();
console.log('初始化设置');
console.table(settings);
let randNum = 0;
const checkVideoPlayingInterval = setInterval(() => {
let temp = getVideoTag();
if (temp.video) {
if (!temp.video.muted) {
temp.video.muted = true;
}
if (temp.video.paused) {
console.log('正在尝试播放视频');
if (randNum === 0) {
// 尝试使用js的方式播放
try {
temp.video.play(); // 尝试使用js的方式播放
}
catch (e) { }
randNum++;
}
else {
try {
temp.pauseButton?.click(); // 尝试点击播放按钮播放
}
catch (e) { }
randNum--;
}
}
else {
console.log('成功播放');
clearInterval(checkVideoPlayingInterval);
reading(1);
}
}
else {
console.log('等待加载');
}
}, 800);
}
else if (href.includes(URL_CONFIG.examPaper) ||
href.includes(URL_CONFIG.examPractice) ||
href.includes(URL_CONFIG.examWeekly)) {
// 初始化设置
initSetting();
console.log('初始化设置');
console.table(settings);
console.log('进入答题页面!');
// 答题页面
const ready = setInterval(() => {
if ($$('.title')[0]) {
clearInterval(ready); // 停止定时器
// 创建“手动答题”按钮
createManualButton();
// 开始答题
doingExam();
}
}, 500);
}
else {
console.log('此页面不支持加载学习脚本!');
}
});
// 获取video标签
function getVideoTag() {
let iframe = $$('iframe')[0];
let video;
let pauseButton;
const u = navigator.userAgent;
if (u.indexOf('Mac') > -1) {
// Mac
if (iframe && iframe.innerHTML) {
// 如果有iframe,说明外面的video标签是假的
video = iframe.contentWindow?.document.getElementsByTagName('video')[0];
pauseButton = (iframe.contentWindow?.document.getElementsByClassName('prism-play-btn')[0]);
}
else {
// 否则这个video标签是真的
video = $$('video')[0];
pauseButton = $$('.prism-play-btn')[0];
}
return {
video: video,
pauseButton: pauseButton,
};
}
else {
if (iframe) {
// 如果有iframe,说明外面的video标签是假的
video = (iframe.contentWindow?.document.getElementsByTagName('video')[0]);
pauseButton = (iframe.contentWindow?.document.getElementsByClassName('prism-play-btn')[0]);
}
else {
// 否则这个video标签是真的
video = $$('video')[0];
pauseButton = $$('.prism-play-btn')[0];
}
return {
video: video,
pauseButton: pauseButton,
};
}
}
// 读新闻或者看视频
// type:0为新闻,1为视频
async function reading(type) {
// 看文章或者视频
let time = 1;
if (type === 0) {
// 80-100秒后关闭页面,看文章
time = ~~(Math.random() * 20 + 80) + 1;
}
if (type === 1) {
// 100-150秒后关闭页面,看视频
time = ~~(Math.random() * 50 + 100) + 1;
}
let firstTime = time - 2;
let secendTime = 12;
// 滚动长度
const scrollLength = document.body.scrollHeight / 2;
await createTip('距离关闭页面还剩', time, (time) => {
if (time === firstTime) {
window.scrollTo(0, 394);
}
if (time === secendTime) {
window.scrollTo(0, scrollLength / 3);
}
if (time === 0) {
if (type === 0) {
GM_setValue('readingUrl', null);
}
else {
GM_setValue('watchingUrl', null);
}
// 关闭窗口
closeWin();
}
});
// 关闭文章或视频页面
}
// 创建学习提示
async function createTip(text, delay, callback) {
return new Promise((resolve) => {
// 提前去除
const studyTip = $$('#studyTip')[0];
if (studyTip) {
studyTip.destroy();
}
// 提示
const tipInfo = creatElementNode('div', undefined, {
id: 'studyTip',
class: 'egg_tip inactive',
});
let destroyed = false;
// 插入节点
document.body.append(tipInfo);
// 操作
const operate = {
destroy() {
if (!destroyed) {
// 隐藏
operate.hide();
destroyed = true;
setTimeout(() => {
tipInfo.remove();
}, 300);
}
},
hide() {
if (!destroyed) {
tipInfo.classList.add('inactive');
tipInfo.classList.remove('active');
}
},
show() {
if (!destroyed) {
setTimeout(() => {
tipInfo.classList.add('active');
tipInfo.classList.remove('inactive');
}, 300);
}
},
};
Object.assign(tipInfo, operate);
tipInfo.append(text ? text : '');
if (delay && delay >= 0) {
// 倒计时
const countdown = creatElementNode('span', {
innerText: `${delay}s`,
}, {
class: 'egg_countdown',
});
tipInfo.appendChild(countdown);
operate.show();
// 倒计时
const countDown = () => {
countdown.innerText = `${delay}s`;
if (typeof delay === 'number' && callback) {
callback(delay, operate);
}
// 倒计时结束
if (!delay) {
// 隐藏
operate.hide();
resolve(operate);
return;
}
delay--;
setTimeout(countDown, 1000);
};
countDown();
return;
}
operate.show();
resolve(operate);
});
}
// 获取新闻列表
function getNews() {
return new Promise(async (resolve) => {
// 需要学习的新闻数量
const need = tasks[0].need < maxNewsNum ? tasks[0].need : maxNewsNum;
console.log(`还需要看 ${need} 个新闻`);
// 获取重要新闻
const data = await getTodayNews();
if (data && data.length) {
// 数量补足需要数量
while (news.length < need) {
// 随便取
const randomIndex = ~~(Math.random() * data.length);
// 新闻
const item = data[randomIndex];
// 是否存在新闻
if (item.dataValid && item.type === 'tuwen') {
news.push(item);
}
}
}
else {
news = [];
}
resolve('done');
});
}
// 获取视频列表
function getVideos() {
return new Promise(async (resolve) => {
// 需要学习的视频数量
const need = tasks[1].need < maxVideoNum ? tasks[1].need : maxVideoNum;
console.log(`还需要看 ${need} 个视频`);
// 获取重要视频
const data = await getTodayVideos();
if (data && data.length) {
// 数量补足需要数量
while (videos.length < need) {
// 随便取
const randomIndex = ~~(Math.random() * data.length);
// 视频
const item = data[randomIndex];
// 是否存在视频
if (item.dataValid &&
(item.type === 'shipin' || item.type === 'juji')) {
videos.push(item);
}
}
}
else {
videos = [];
}
resolve('done');
});
}
// 阅读文章
async function readNews() {
await getNews();
for (const i in news) {
// 暂停
await pauseStudyLock();
// 链接
GM_setValue('readingUrl', news[i].url);
console.log(`正在看第${Number(i) + 1}个新闻`);
// 新页面
const newPage = GM_openInTab(news[i].url, {
active: true,
insert: true,
setParent: true,
});
// 等待窗口关闭
await waitingClose(newPage);
// 等待一段时间
await waitingTime(1500);
// 刷新菜单数据
await refreshMenu();
// 任务完成跳出循环
if (settings[0] && tasks[0].status) {
break;
}
}
// 任务完成状况
if (settings[0] && !tasks[0].status) {
console.log('任务未完成,继续看新闻!');