Skip to content

Commit

Permalink
feat: 适配wetest
Browse files Browse the repository at this point in the history
  • Loading branch information
SSmJaE committed Mar 8, 2024
1 parent ec487e2 commit f83aaea
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 68 deletions.
3 changes: 2 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"welearn": {
"title": "随行课堂网课助手",
"name": "WELearn网课助手",
"version": "1.0.5",
"version": "1.1.0",
"matches": [
"*://course.sflep.com/*",
"*://welearn.sflep.com/*",
"*://wetest.sflep.com/*",
"*://courseappserver.sflep.com/*",
"*://centercourseware.sflep.com/*"
],
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const availableUrls = [
".sflep.com/student/course_info.aspx?", //基准页面
"centercourseware.sflep.com", //练习答题页面,子页面
".sflep.com/test/", //考试答题页面
"wetest.sflep.com/Test", // 新url,内容未变
];

function initialize() {
Expand Down
18 changes: 13 additions & 5 deletions src/projects/welearn/exam/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ import { hackPlaySound } from "./utils";
// 手机上(页面加载过慢),onLoad也不靠谱,还是手动判断一下好了
let isPageReady = false;
let elapsedTime = 0;
const MAX_WAIT_TIME = 10000;

function checkPageReady() {
const element = document.querySelector("#aSubmit");
const element = document.querySelector("#spTimer");
if (element) {
isPageReady = true;
}
}

async function watcher() {
while (!isPageReady || elapsedTime > 10000) {
while (elapsedTime < MAX_WAIT_TIME) {
// 哪怕一直没有判断成功,过了MAX_WAIT_TIME也会退出
if (isPageReady) {
break;
}

await sleep(200);
elapsedTime += 200;
checkPageReady();
Expand All @@ -45,11 +51,13 @@ function notify() {
(finished
? "检测到当前位于解析页面,点击本条消息右侧的上传按钮,以收录答案"
: "检测到当前位于测试页面,点击本条消息右侧的查询按钮,以开始查询") +
"<br />❗❗❗测试的每一个Part,都需要点击一次",
`<br />❗❗❗即使有多个part,也只需要点击一次,会自动按顺序${
finished ? "上传" : "查询"
}所有part的题目`,
extra: undefined,
action: [
{
children: `${finished ? "上传" : "查询"}当前Part`,
children: `${finished ? "上传" : "查询"}答案`,
disabled: 5000,
onClick() {
getAnswers();
Expand All @@ -64,7 +72,7 @@ function notify() {
}
}

if (location.href.includes(".sflep.com/test/")) {
if (location.href.includes(".sflep.com/test/") || location.href.includes("wetest.sflep.com/Test")) {
// iife不允许顶层await
(async () => {
await watcher();
Expand Down
40 changes: 24 additions & 16 deletions src/projects/welearn/exam/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import logger, { IDynamicButton } from "@utils/logger";

/** 判断当前页面是否是详情/解析页面,或是答题页面 */
export function isFinished() {
return (document.querySelector("#aSubmit") as HTMLElement).style.display == "none"
? true
: false;
return !document.querySelector("#spSubmit");

// return false;
}

function getTaskId() {
// https://welearn.sflep.com/test/schooltest.aspx?schooltestid=1330
let isSchoolTest = false;
let taskId: string;

if (location.href.includes("schooltest")) {
isSchoolTest = true;
taskId = /schooltestid=(\d*)/.exec(location.href)![1];
} else {
taskId = /taskid=(\d*)/.exec(location.href)![1];
let taskId: string | null = null;

try {
if (location.href.includes("schooltest")) {
isSchoolTest = true;
taskId = /schooltestid=(\d*)/.exec(location.href)![1];
} else {
taskId = /testId=(\d*)/.exec(location.href)![1];
}
} catch {
logger.debug("testId获取失败");
}

return {
Expand Down Expand Up @@ -77,14 +79,20 @@ async function querySingleQuestion(questionItemDiv: HTMLElement) {
const replayButton: IDynamicButton = {
children: "播放音频",
onClick: () => {
const mainAudio = <HTMLElement>(
questionItemDiv.querySelector('a[href^="javascript:PlaySound"]')
);
const mainAudioFile = /'(.*?)'/.exec(<string>mainAudio.getAttribute("href"))![1];
const mainAudio = <HTMLElement>questionItemDiv.querySelector('a[id*="btnPlay"]');
const questionId = /btnPlay_(.*)/.exec(mainAudio.id)![1];

let mainAudioFile: string | null = null;
const match = /"(.*?.mp3)"/.exec(<string>mainAudio.getAttribute("href"));
if (match) {
mainAudioFile = match[1];
} else {
mainAudioFile = mainAudio.getAttribute("data-soundsrc");
}

logger.debug(mainAudioFile);
logger.debug(mainAudioFile, questionId);

PlaySound(mainAudioFile);
PlaySound(mainAudioFile, questionId);
},
};

Expand Down
80 changes: 38 additions & 42 deletions src/projects/welearn/exam/utils.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
// PlaySound使用的全局变量
// var resPath = "https://courseres.sflep.com/Test/";
// var soundfile = "";
// var bufferingTimer: any;

import { store } from "@src/store";
// import { addMessage } from "@src/store/actions";
import logger from "@utils/logger";

/**
* 替换原生的playSound函数,以实现无限播放听力
*/
export function hackPlaySound() {
((unsafeWindow || window) as any).PlaySound = (src: string, id: string) => {
var count = $("#hdPlay_" + id).val();
if (count <= 0) return;

if (soundfile == "") {
soundfile = resPath + "ItemRes/sound/" + src;
createSoundPlayer();
} else {
soundfile = resPath + "ItemRes/sound/" + src;
jwplayer("soundplayer").load([{ file: soundfile }]);
//clearTimeout(bufferingTimer);

// var remainCount = 3;
// if (paperAnswer && paperAnswer[id]) {
// try {
// remainCount = parseInt(paperAnswer[id].value);
// } catch (e) {}
// }

// if (remainCount <= 0) return;

if (soundPlayer) {
soundPlayer.stop();
try {
jwplayer("soundplayer").remove();
} catch (e) {}
}
// jwplayer("soundplayer").onPlaylistComplete(function() {
// jwplayer("soundplayer").load([{ file: "" }]);
// });
jwplayer("soundplayer").onBufferFull(function () {
clearTimeout(bufferingTimer);

var sp = $("#btnPlay_" + id);

sp.html('<span class=" fa fa-play-circle play_symble">' + "无限" + "次播放机会</span>");
//以下为原生调用
// if (sp.length > 0) {
// var count = $("#hdPlay_" + id).val();
// if (count > 0) count--;
// SetCurrentSoundInfo();

// //sp.val('播放(' + count + '次机会)');
currentSoundId = id;
const soundfile = testEnv.resPath + "sound/" + src;

// $("#hdPlay_" + id).val(count);
// if (count == 0) {
// //$('#btnPlay_' + id).attr("disabled", "disabled");
// $("#btnPlay_" + id).attr("href", "javascript:void(0);");
// }
soundPlayer = jwplayer("soundplayer").setup({
flashplayer: "script/jwplayer.flash.swf?c=" + Math.random(),
file: soundfile,
height: 0,
width: 0,
primary: "html5",
});
soundPlayer.play();

// // SaveCurrentPart(false, true); //异步保存,实时更新听力次数
// }
// soundPlayer.onBufferFull(function () {
// SetCurrentSoundInfo();
// });

sp.removeClass("loading");
});
// $("#btnPlay_" + currentSoundId).html(
// '<span class=" fa fa-play-circle play_symble">正在加载</span>',
// );
// $("#btnPlay_" + currentSoundId).addClass("loading");

$("#btnPlay_" + id).val("正在加载");
bufferingTimer = setTimeout("PlayerExpireCheck('" + id + "', 0)", 1000);
$("#btnPlay_" + id).addClass("loading");
// remainCount--;
// autoSaveSound(currentSoundId, remainCount);

jwplayer("soundplayer").play();
//if (remainCount <= 0)
// sp.attr("href", "javascript:void(0);");
};
}
11 changes: 7 additions & 4 deletions src/types/external.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ declare global {
const $: any; //todo types/jQuery
const angular: any; //todo types/jQuery

const createSoundPlayer: Function;
const jwplayer: Function;
const NextSCO: Function;

//音视频播放相关
const jwplayer: Function;
let PlaySound: Function;
let soundfile: string;
let resPath: any;
let bufferingTimer: any;
let testEnv: any;
let soundPlayer: any;
let lastSoundId: any;
let currentSoundId: any;

const unsafeWindow: Window;
}

0 comments on commit f83aaea

Please sign in to comment.