Skip to content

Commit

Permalink
fix: 恢复直接在页面上无限播放听力的能力 #54
Browse files Browse the repository at this point in the history
  • Loading branch information
SSmJaE committed Mar 31, 2023
1 parent 8be145b commit e468d42
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 129 deletions.
31 changes: 19 additions & 12 deletions src/projects/welearn/exam/initial.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { store } from "@/src/store";
import { sleep } from "@/src/utils";
import logger from "@/src/utils/logger";
import { WELearnAPI } from "@api/welearn";

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

// 确保页面已经加载完成,扩展有可能先于页面加载完成
// ~onLoad不一定靠谱,因为页面上本身也会执行js,这就有了额外的延迟~
Expand Down Expand Up @@ -37,14 +39,6 @@ function notify() {

const recordId = `${Math.random()}`;

const buttonInfo = {
children: `${finished ? "上传" : "查询"}当前Part`,
disabled: 5000,
onClick() {
getAnswers();
},
};

logger.info({
id: recordId,
content:
Expand All @@ -53,15 +47,28 @@ function notify() {
: "检测到当前位于测试页面,点击本条消息右侧的查询按钮,以开始查询") +
"<br />❗❗❗测试的每一个Part,都需要点击一次",
extra: undefined,
action: [buttonInfo],
action: [
{
children: `${finished ? "上传" : "查询"}当前Part`,
disabled: 5000,
onClick() {
getAnswers();
},
},
],
});

if (store.userSettings.infiniteListening) {
hackPlaySound();
logger.debug("已开启无限听力");
}
}

if (location.href.includes(".sflep.com/test/")) {
// iife不允许顶层await
(async () => {
await watcher();
})();
// (async () => {
await watcher();
// })();
}

if (location.href.includes(".sflep.com/student/course_info.aspx?")) {
Expand Down
24 changes: 12 additions & 12 deletions src/projects/welearn/exam/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ export interface IWELearnExamSettings {
}

export const WELearnExamSettings: SectionSetting<IWELearnExamSettings>[] = [
// {
// title: "考试",
// settings: [
// {
// id: "infiniteListening",
// name: "无限听力",
// default: true,
// valueType: "boolean",
// description: "允许无限次播放听力音频",
// },
// ],
// },
{
title: "考试",
settings: [
{
id: "infiniteListening",
name: "无限听力",
default: true,
valueType: "boolean",
description: "允许无限次播放听力音频",
},
],
},
];
153 changes: 48 additions & 105 deletions src/projects/welearn/exam/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//PlaySound使用的全局变量
// PlaySound使用的全局变量
// var resPath = "https://courseres.sflep.com/Test/";
// var soundfile = "";
// var bufferingTimer: any;
Expand All @@ -10,107 +10,50 @@ import logger from "@utils/logger";
/**
* 替换原生的playSound函数,以实现无限播放听力
*/
// export function hackPlaySound() {
// /*答卷过程中调用的方法*/
// (unsafeWindow 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 }]);
// }
// // 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--;

// // //sp.val('播放(' + count + '次机会)');

// // $("#hdPlay_" + id).val(count);
// // if (count == 0) {
// // //$('#btnPlay_' + id).attr("disabled", "disabled");
// // $("#btnPlay_" + id).attr("href", "javascript:void(0);");
// // }

// // // SaveCurrentPart(false, true); //异步保存,实时更新听力次数
// // }

// sp.removeClass("loading");
// });

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

// jwplayer("soundplayer").play();
// };
// }

// QuestionResponse
// export function parseResponse(json: any) {
// logger.debug(json);

// let status = "";
// switch (json.status) {
// case 0:
// status = "新增收录题目,未收录答案";
// break;
// case 1:
// status = "新增收录题目,且收录答案";
// // addMessage(status, "info");
// // addMessage(`用户${store.USER_SETTINGS.userAccount}积分+1`, "info");
// break;
// case 2:
// status = "服务器已有题目,新增答案";
// // addMessage(status, "info");
// // addMessage(`用户${store.USER_SETTINGS.userAccount}积分+1`, "info");
// break;
// case 3:
// status = "服务器已有答案,返回答案";
// break;
// case 4:
// status = "服务器已有题目,没有答案";
// break;
// case 5:
// status = "服务器没有题目,没有答案";
// break;
// case 6:
// status = "没有标答,返回最可能答案";
// break;
// }
// // addMessage(status, "info");

// let answer = json.answer;
// switch (json.status) {
// case 3:
// // addMessage(answer as string, "success");
// break;
// case 4:
// //fallthrough
// case 5:
// // addMessage("尚未收录答案", "error");
// break;
// case 6:
// for (let [option, possibility] of Object.entries(<string>answer)) {
// // addMessage(`${possibility} ${option}`, "success");
// }
// }

// // if (store.messages)
// // if (store.messages[store.messages.length - 1].info)
// //前一条消息为空不添加
// // addMessage("", "hr");
// }
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 }]);
}
// 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--;

// //sp.val('播放(' + count + '次机会)');

// $("#hdPlay_" + id).val(count);
// if (count == 0) {
// //$('#btnPlay_' + id).attr("disabled", "disabled");
// $("#btnPlay_" + id).attr("href", "javascript:void(0);");
// }

// // SaveCurrentPart(false, true); //异步保存,实时更新听力次数
// }

sp.removeClass("loading");
});

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

jwplayer("soundplayer").play();
};
}

0 comments on commit e468d42

Please sign in to comment.