Skip to content

Commit

Permalink
fix: 测试页面的检测问题,onLoad不靠谱
Browse files Browse the repository at this point in the history
  • Loading branch information
SSmJaE committed Mar 23, 2023
1 parent c5876a1 commit 01c9c04
Showing 1 changed file with 46 additions and 52 deletions.
98 changes: 46 additions & 52 deletions src/projects/welearn/exam/initial.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,68 @@
import logger from "@/src/utils/logger";
import { sleep } from "@/src/utils";
import { WELearnAPI } from "@api/welearn";
import { store } from "@src/store";

import { getAnswers, isFinished } from "./parser";
import { hackPlaySound } from "./utils";

if (location.href.includes(".sflep.com/test/")) {
// 确保页面已经加载完成,扩展有可能先于页面加载完成
// ~onLoad不一定靠谱,因为页面上本身也会执行js,这就有了额外的延迟~
// https://welearn.sflep.com/test/Test.aspx 返回的页面中,直接就可以获取到#aSubmit,所以onLoad很靠谱
window.addEventListener("load", () => {
logger.debug("页面加载完成,开始检测");

const finished = isFinished();
// 确保页面已经加载完成,扩展有可能先于页面加载完成
// ~onLoad不一定靠谱,因为页面上本身也会执行js,这就有了额外的延迟~
// https://welearn.sflep.com/test/Test.aspx 返回的页面中,直接就可以获取到#aSubmit,所以onLoad很靠谱
// 手机上(页面加载过慢),onLoad也不靠谱,还是手动判断一下好了
let isPageReady = false;
let elapsedTime = 0;

const recordId = `${Math.random()}`;
function checkPageReady() {
const element = document.querySelector("#aSubmit");
if (element) {
isPageReady = true;
}
}

const buttonInfo = {
children: `${finished ? "上传" : "查询"}当前Part`,
disabled: 5000,
onClick() {
getAnswers();
async function watcher() {
while (!isPageReady || elapsedTime > 10000) {
await sleep(200);
elapsedTime += 200;
checkPageReady();
}

// const currentRecord = store.getRecordById(recordId);
logger.debug(`页面加载完成,耗时${elapsedTime}ms`);

// logger.debug(currentRecord);
notify();
}

// if (currentRecord?.action) {
// store.updateRecord({
// id: recordId,
// action: [
// {
// ...buttonInfo,
// disabled: true,
// },
// ],
// });
function notify() {
logger.debug("页面加载完成,开始检测完成情况");

// logger.debug(store.logs);
const finished = isFinished();

// setTimeout(() => {
// logger.debug("in timeout", store.logs);
const recordId = `${Math.random()}`;

// store.updateRecord({
// id: recordId,
// action: [
// {
// ...buttonInfo,
// disabled: false,
// },
// ],
// });
// }, 5000);
// }
},
};
const buttonInfo = {
children: `${finished ? "上传" : "查询"}当前Part`,
disabled: 5000,
onClick() {
getAnswers();
},
};

logger.info({
id: recordId,
content:
(finished
? "检测到当前位于解析页面,点击本条消息右侧的上传按钮,以收录答案"
: "检测到当前位于测试页面,点击本条消息右侧的查询按钮,以开始查询") +
"<br />❗❗❗测试的每一个Part,都需要点击一次",
extra: undefined,
action: [buttonInfo],
});
logger.info({
id: recordId,
content:
(finished
? "检测到当前位于解析页面,点击本条消息右侧的上传按钮,以收录答案"
: "检测到当前位于测试页面,点击本条消息右侧的查询按钮,以开始查询") +
"<br />❗❗❗测试的每一个Part,都需要点击一次",
extra: undefined,
action: [buttonInfo],
});
}

if (location.href.includes(".sflep.com/test/")) {
await watcher();
}

if (location.href.includes(".sflep.com/student/course_info.aspx?")) {
WELearnAPI.upload();
}

0 comments on commit 01c9c04

Please sign in to comment.