Skip to content

Commit

Permalink
fix(script): 修复智慧树无法使用软件辅助,无法自动答题看视频的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Feb 28, 2024
1 parent 5771d26 commit a3cdb6f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
23 changes: 18 additions & 5 deletions packages/core/src/utils/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Page } from 'playwright-core';
import { request } from '../core/utils';
import { $ } from './common';

export type Base64 = string;

export interface RemotePage {
click: (
selectorOrElement: string | Element,
Expand Down Expand Up @@ -82,9 +84,19 @@ export interface RemotePage {
setInputFiles: Page['setInputFiles'];
tap: Page['tap'];
press: Page['press'];
reload: Page['reload'];
waitForRequest(...args: Parameters<Page['waitForRequest']>): Promise<string>;
waitForResponse(...args: Parameters<Page['waitForResponse']>): Promise<string>;
reload: (...args: Parameters<Page['reload']>) => Promise<Base64>;
waitForRequest(...args: Parameters<Page['waitForRequest']>): Promise<{
url: string;
method: string;
headers: Record<string, string>;
postData: string;
}>;
waitForResponse(...args: Parameters<Page['waitForResponse']>): Promise<{
text: string;
headers: Record<string, string>;
status: number;
url: string;
}>;
waitForSelector(...args: Parameters<Page['waitForSelector']>): Promise<void>;
}

Expand Down Expand Up @@ -169,10 +181,11 @@ export class RemotePlaywright {
logger?.('[RP]: ', JSON.stringify(data));

try {
const res = await request('http://localhost:15319/ocs-script-actions', {
// 这里为什么不写前缀 http://localhost:15319,因为有 Content-Security-Policy , 这里我们借用后台的URL代理去进行处理,只要包含 ocs-script-actions 即可轻松绕过 Content-Security-Policy 限制
const res = await request('/ocs-script-actions', {
type: 'fetch',
method: 'post',
responseType: 'text',
responseType: ['waitForRequest', 'waitForResponse', 'reload'].includes(property) ? 'json' : 'text',
headers: {
'auth-token': authToken
},
Expand Down
36 changes: 25 additions & 11 deletions packages/scripts/src/projects/zhs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,11 @@ export const ZHSProject = Project.create({
} else {
url = '/studentExam/gateway/t/v1/student/doHomework';
}
return JSON.parse(await remotePage.waitForResponse(url));
return JSON.parse((await remotePage.waitForResponse(url)).text);
}

return {
getWorkInfo: getWorkInfo,
work: async () => {
// 检查是否为软件环境
const remotePage = await RemotePlaywright.getCurrentPage();
Expand All @@ -466,18 +467,21 @@ export const ZHSProject = Project.create({

if (isExam || isWork) {
const workInfo = await getWorkInfo(remotePage);
$message('info', { content: `开始${isExam ? '考试' : '作业'}` });
commonWork(this, {
workerProvider: (opts) => gxkWorkAndExam(workInfo, opts)
});
setTimeout(() => {
$message('info', { content: `开始${isExam ? '考试' : '作业'}` });
commonWork(this, {
workerProvider: (opts) => gxkWorkAndExam(workInfo, opts)
});
}, 1000);
} else {
$message('info', { content: '📢 请手动进入作业/考试,如果未开始答题,请尝试刷新页面。', duration: 0 });

CommonProject.scripts.render.methods.pin(this);
}
}
};
},
async oncomplete() {
async onactive() {
this.methods.work();
/**
* 当页面从作业考试列表跳转到作业考试页面时,触发的是onhistorychange事件,而不是oncomplete事件。
Expand Down Expand Up @@ -775,7 +779,15 @@ function getPopupCaptcha() {
*/
function gxkWorkAndExam(
workInfo: any,
{ answererWrappers, period, thread, stopSecondWhenFinish, redundanceWordsText, answer_separators }: CommonWorkOptions
{
answererWrappers,
period,
thread,
stopSecondWhenFinish,
redundanceWordsText,
answerSeparators,
answerMatchMode
}: CommonWorkOptions
) {
CommonProject.scripts.workResults.methods.init({
questionPositionSyncHandlerType: 'zhs-gxk'
Expand Down Expand Up @@ -825,7 +837,8 @@ function gxkWorkAndExam(
requestPeriod: period ?? 3,
resolvePeriod: 1,
thread: thread ?? 1,
separators: answer_separators.split(',').map((s) => s.trim()),
answerSeparators: answerSeparators.split(',').map((s) => s.trim()),
answerMatchMode: answerMatchMode,
/** 默认搜题方法构造器 */
answerer: (elements, ctx) => {
const title = titleTransform(undefined, index++);
Expand Down Expand Up @@ -902,7 +915,7 @@ function gxkWorkAndExam(
*/
for (let index = 0; index < worker.totalQuestionCount; index++) {
const modal = $modal('alert', {
content: '正在保存题目中(必须保存,否则填写的答案无效),请勿操作...',
content: '正在保存题目中(必须保存,否则填写的答案无效),<br>请勿操作...',
confirmButton: null
});
await waitForCaptcha();
Expand Down Expand Up @@ -932,7 +945,7 @@ function gxkWorkAndExam(
/**
* 校内学分课的作业
*/
function xnkWork({ answererWrappers, period, thread, answer_separators }: CommonWorkOptions) {
function xnkWork({ answererWrappers, period, thread, answerSeparators, answerMatchMode }: CommonWorkOptions) {
$message('info', { content: '开始作业' });

CommonProject.scripts.workResults.methods.init();
Expand Down Expand Up @@ -960,7 +973,8 @@ function xnkWork({ answererWrappers, period, thread, answer_separators }: Common
requestPeriod: period ?? 3,
resolvePeriod: 1,
thread: thread ?? 1,
separators: answer_separators.split(',').map((s) => s.trim()),
answerSeparators: answerSeparators.split(',').map((s) => s.trim()),
answerMatchMode: answerMatchMode,
/** 默认搜题方法构造器 */
answerer: (elements, ctx) => {
const title = titleTransform(elements.title);
Expand Down

0 comments on commit a3cdb6f

Please sign in to comment.