Skip to content

Commit

Permalink
fix(script): 优化智慧树刷课逻辑,增加流畅度
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Sep 22, 2023
1 parent 43b60ac commit 3a93157
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
19 changes: 10 additions & 9 deletions packages/scripts/src/projects/zhs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ export const ZHSProject = Project.create({

hideDialog();

setInterval(() => {
closeTestDialog();
setInterval(async () => {
await closeTestDialog();
fixProcessBar();
// 删除遮罩层
$$el('.v-modal,.mask').forEach((modal) => {
Expand Down Expand Up @@ -377,15 +377,20 @@ export const ZHSProject = Project.create({

$message('info', { content: '3秒后开始学习', duration: 3 });

const init = await $app_actions.init();
if (!init) {
$app_actions.showError();
return;
}

const study = async (opts: { next: boolean }) => {
if (stop === false) {
const item = findVideoItem(opts);
console.log('item', item);

if (item) {
await $.sleep(3000);
$app_actions.mouseClick(item);
await $.sleep(5000);
await $app_actions.mouseClick(item);

watch(
{ volume: this.cfg.volume, playbackRate: this.cfg.playbackRate, definition: this.cfg.definition },
({ next }) => {
Expand Down Expand Up @@ -574,10 +579,8 @@ async function watch(
// 设置清晰度
await switchLine(options.definition);
await $.sleep(1000);

// 设置播放速度
await switchPlaybackRate(options.playbackRate);
await $.sleep(1000);

// 上面操作会导致元素刷新,这里重新获取视频
const video = await waitForMedia();
Expand Down Expand Up @@ -658,7 +661,6 @@ async function switchLine(definition: 'line1bq' | 'line1gq' = 'line1bq') {
const controls = $el('.controlsBar');
controls && (controls.style.display = 'block');
await $app_actions.mouseClick('.definiBox > span');
await $.sleep(1000);
await $app_actions.mouseClick(`.definiLines .${definition}`);
}

Expand All @@ -670,7 +672,6 @@ async function switchPlaybackRate(playbackRate: number) {
const controls = $el('.controlsBar');
controls && (controls.style.display = 'block');
await $app_actions.mouseClick('.speedBox > span');
await $.sleep(1000);
await $app_actions.mouseClick(`.speedList [rate="${playbackRate === 1 ? '1.0' : playbackRate}"]`);
}

Expand Down
48 changes: 28 additions & 20 deletions packages/scripts/src/utils/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { $console } from '../projects/background';
let actions_key = '';

export const $app_actions = {
showError: () => {
$message('error', {
duration: 60,
content: el('div', [
'软件辅助启动失败,请检查网络,或者使用OCS桌面软件运行浏览器,教程/疑问:',
$creator.button('软件辅助开启教程', {
onclick: () => {
window.open('https://docs.ocsjs.com/docs/script-helper');
}
})
])
});
},
init: async () => {
/**
* OCS桌面端后端无法拦截 GM_xmlhttpRequest ,所以这里使用 fetch 请求动作执行,然后后端根据key判断是否允许执行
Expand All @@ -15,20 +28,13 @@ export const $app_actions = {
method: 'get',
responseType: 'text'
});
return true;
} catch (e) {
console.log(e);
$message('error', {
duration: 0,
content: el('div', [
'软件辅助启动失败,请检查网络,或者使用OCS桌面软件运行浏览器,教程/疑问:',
$creator.button('软件辅助开启教程', {
onclick: () => {
window.open('https://docs.ocsjs.com/docs/script-helper');
}
})
])
});
return false;
}
} else {
return true;
}
},
waitForResponse: async (
Expand All @@ -39,7 +45,7 @@ export const $app_actions = {
responseType?: 'text' | 'json';
}
) => {
await await appActionRequest(
await appActionRequest(
{
url,
action: 'listen-request'
Expand All @@ -52,7 +58,7 @@ export const $app_actions = {
return new Promise((resolve, reject) => {
const interval = setInterval(async () => {
try {
const res = await await appActionRequest(
const res = await appActionRequest(
{
url,
action: 'get-listened-request'
Expand All @@ -74,7 +80,7 @@ export const $app_actions = {
});
},
mouseMove: async (x: number, y: number) => {
await appActionRequest({
return await appActionRequest({
x: x.toString(),
y: y.toString(),
action: 'mouse-move'
Expand All @@ -89,14 +95,14 @@ export const $app_actions = {
elementOrSelector.scrollIntoView();
await $.sleep(1000);
const rect = elementOrSelector.getBoundingClientRect();
await appActionRequest({
return await appActionRequest({
x: rect.x.toString(),
y: rect.y.toString(),
count: count.toString(),
action: 'mouse-click'
});
} else {
await appActionRequest({
return await appActionRequest({
selector: elementOrSelector,
action: 'mouse-click'
});
Expand All @@ -110,15 +116,15 @@ async function appActionRequest(
baseUrl?: string;
responseType?: 'text' | 'json';
}
) {
): Promise<string> {
await $app_actions.init();
if (!actions_key) {
return;
return '';
}
data = Object.assign({ page: window.location.href }, data);

try {
return await request(
const res = await request(
(options?.baseUrl || 'http://localhost:15319') + '/ocs-script-actions?' + new URLSearchParams(data).toString(),
{
type: 'fetch',
Expand All @@ -129,8 +135,10 @@ async function appActionRequest(
}
}
);

return String(res);
} catch (e) {
$console.error('软件辅助错误', String(e));
return undefined;
return '';
}
}

0 comments on commit 3a93157

Please sign in to comment.