Skip to content

Commit

Permalink
feat: 切换网络路线
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 9, 2022
1 parent 5e21f0a commit 0a0a5dd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 49 deletions.
1 change: 1 addition & 0 deletions packages/core/src/core/create.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function createSettingPanel(...settingItems: FormType[]): DefineComponent
class: "ocs-setting-items",
},
settingItems
.filter((item) => item)
.map((item) =>
isVNode(item) ? item : [h("label", item.label), createSettingItem.apply(this, [item])]
)
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/cx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createCXStudySettingPanel, createCXWorkSettingPanel } from "./panels";
import { study } from "./study";
import { rateHack } from "./utils";
import { workOrExam } from "./work";
import { useOCS } from "..";

/** 需切换版本的 url 页面 */
const updateURLs = [
Expand Down Expand Up @@ -50,7 +51,7 @@ export const CXScript = defineScript({
name: "任务切换脚本",
url: "**/mycourse/studentstudy**",
onload() {
const { restudy } = (top || window).OCS.setting.cx.video;
const { restudy } = useOCS().setting.cx.video;

const params = new URLSearchParams(window.location.href);
const mooc = params.get("mooc2");
Expand Down Expand Up @@ -93,7 +94,7 @@ export const CXScript = defineScript({
{
name: "学习脚本",
url: "**/knowledge/cards**",
async onload(setting = (top || window).OCS.setting.cx.video) {
async onload(setting = useOCS().setting.cx.video) {
await sleep(5000);
await study(setting);
},
Expand All @@ -114,7 +115,7 @@ export const CXScript = defineScript({
{
name: "作业脚本",
url: "**/mooc2/work/dowork**",
async onload(setting = (top || window).OCS.setting.cx.work) {
async onload(setting = useOCS().setting.cx.work) {
await sleep(5000);
await workOrExam(setting, false);
},
Expand All @@ -132,7 +133,7 @@ export const CXScript = defineScript({
{
name: "考试脚本",
url: "**/mooc2/exam/preview**",
async onload(setting = (top || window).OCS.setting.cx.exam) {
async onload(setting = useOCS().setting.cx.exam) {
await sleep(5000);
await workOrExam(setting, true);
},
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/cx/panels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DefineComponent, h } from "vue";
import { createSettingPanel, createWorkerSetting, CreateWorkerSettingConfig } from "../core/create.element";
import { domSearch } from "../core/utils";
import { switchPlayLine } from "./study";

/**
* 创建超星学习设置面板
Expand Down Expand Up @@ -41,13 +43,21 @@ export function createCXStudySettingPanel(): DefineComponent {
id: "video-line",
value: settings.line || "",
onchange: (e: any) => {
console.log("播放路线", e.target.value);
settings.line = e.target.value;
const { iframe } = domSearch({ iframe: "iframe" });
const win = (iframe as HTMLIFrameElement).contentWindow;
win?.location.reload();
},
},
options: (
[
settings.line ? { label: "指定-" + settings.line, value: settings.line } : [],
...[
Array.from(settings.playlines || [{ label: "公网1" }, { label: "公网2" }]).map((line: any) => ({
label: line.label,
value: line.label,
})),
],
{
label: "请指定路线(播放视频后才可选择, 无需保存)",
value: "",
Expand Down
72 changes: 28 additions & 44 deletions packages/core/src/cx/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,15 @@ function searchIFrame() {
}

/**
* 播放视频和音频
* 视频路线切换
*/
function mediaTask(setting: ScriptSettings["cx"]["video"], media: HTMLMediaElement, frame: HTMLIFrameElement) {
const { playbackRate = 1, mute = true } = setting;
/**
* 视频路线选择器
*/
// @ts-ignore
const { videojs } = domSearch({ videojs: "#video" }, frame.contentDocument || document);
const { lineSelect } = domSearch({ lineSelect: "#video-line" }, top?.document);

console.log({ videojs, lineSelect });
export function switchPlayLine(
setting: ScriptSettings["cx"]["video"],
videojs: HTMLElement,
media: HTMLMediaElement,
line: string
) {
const { playbackRate = 1 } = setting;

// @ts-ignore
if (videojs?.player) {
Expand All @@ -140,50 +137,37 @@ function mediaTask(setting: ScriptSettings["cx"]["video"], media: HTMLMediaEleme
// @ts-ignore
videojs.player.controlBar.videoJsPlayLine.querySelectorAll("ul li")
);
console.log({ playlines, menus });

const currentLine = setting.line;
if (currentLine) {
logger("info", "切换路线中: " + currentLine);
setTimeout(() => selectLine(currentLine), 3000);
}
setting.playlines = playlines;

/** 添加选项 */
if (lineSelect) {
for (const line of playlines) {
// @ts-ignore
createOption(line.label);
}
logger("info", "切换路线中: " + line);
selectLine(line);

lineSelect.onchange = function () {
const select = lineSelect as HTMLSelectElement;
selectLine(select.value);
};

function createOption(label: string) {
console.log({ label });

const option = document.createElement("option");
option.text = label;
option.value = label;
// @ts-ignore
lineSelect.appendChild(option);
console.log(lineSelect);
}
}

function selectLine(value: string) {
function selectLine(line: string) {
for (const menu of menus) {
if (menu.textContent?.includes(value)) {
if (menu.textContent?.includes(line)) {
menu.click();
setting.line = value;
setting;
setting.line = line;
setTimeout(() => (media.playbackRate = playbackRate), 3000);
break;
}
}
}
}
}

/**
* 播放视频和音频
*/
function mediaTask(setting: ScriptSettings["cx"]["video"], media: HTMLMediaElement, frame: HTMLIFrameElement) {
const { playbackRate = 1, mute = true } = setting;

// @ts-ignore
const { videojs } = domSearch({ videojs: "#video" }, frame.contentDocument || document);
if (videojs) {
// 切换路线
setTimeout(() => switchPlayLine(setting, videojs, media, setting.line), 3000);
}

/**
* 视频播放
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ export const setting: typeof defaultOCSSetting = defaultsDeep(localStorage.setti

// @ts-ignore
localStorage.setting = setting;

export function useOCS(): typeof OCS {
// @ts-ignore
return unsafeWindow?.OCS;
}

0 comments on commit 0a0a5dd

Please sign in to comment.