-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |