Skip to content

Commit

Permalink
feat: 新增答题器,新增题库配置选项,新增zhs作业功能
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Mar 20, 2022
1 parent 8427936 commit 449c008
Show file tree
Hide file tree
Showing 22 changed files with 1,078 additions and 303 deletions.
38 changes: 0 additions & 38 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const del = require("del");
const { exec } = require("child_process");

function cleanOutput() {
return del(["./dist", "./packages/scripts/lib", "./lib", "./CHANGELOG.md"]);
return del(["./dist", "./packages/scripts/lib", "./lib"]);
}

function tsc() {
Expand Down
3 changes: 1 addition & 2 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
## 目录介绍

- `src/browser` : 浏览器 js 模块,使用 javascript 运行脚本,实现网课视频学习等功能。
- `src/tampermonkey` : 浏览器油猴模块,使用 [油猴-Tampermonkey](https://www.tampermonkey.net/) 运行脚本,搭配 browser 模块的脚本进行使用。
- `scr/nodejs` : nodejs 模块,使用 [playwright](https://github.com/microsoft/playwright) 进行浏览器启动关闭运行等操作,配合 browser 模块的脚本进行使用。

## 使用方法

https://github.com/enncy/online-course-script/tree/3.0#readme
https://github.com/enncy/online-course-script/#readme
2 changes: 2 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
},
"dependencies": {
"@playwright/test": "^1.18.1",
"@types/string-similarity": "^4.0.0",
"axios": "^0.25.0",
"chalk": "4.1.0",
"commander": "^9.0.0",
"dotenv": "^16.0.0",
"interactjs": "^1.10.11",
"lodash": "^4.17.21",
"playwright": "^1.18.1",
"string-similarity": "^4.0.4",
"vue": "^3.2.31",
"ws": "^8.5.0"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/scripts/src/browser/assets/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ocs-panel {
background: white;
z-index: 99999999;
border-radius: 4px;
text-align: left;
box-shadow: 0px 2px 6px #848484;
}
ocs-panel .ocs-panel-header {
Expand All @@ -22,6 +23,8 @@ ocs-panel .ocs-panel-header {
ocs-panel .ocs-panel-header .title {
cursor: pointer;
font-size: 14px;
margin: 0;
padding: 0;
}
ocs-panel .ocs-panel-header .title.active {
font-weight: bold;
Expand All @@ -30,6 +33,9 @@ ocs-panel .ocs-panel-container {
font-size: 12px;
padding: 4px;
}
ocs-panel .ocs-panel-container ul li {
list-style: inside;
}
.ocs-panel-footer {
font-size: 1px;
color: #c5c5c5;
Expand Down
8 changes: 7 additions & 1 deletion packages/scripts/src/browser/assets/less/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ocs-panel {
background: white;
z-index: 99999999;
border-radius: 4px;

text-align: left;
box-shadow: 0px 2px 6px #848484;

.ocs-panel-header {
Expand All @@ -23,6 +23,8 @@ ocs-panel {
.title {
cursor: pointer;
font-size: 14px;
margin: 0;
padding: 0;

&.active {
font-weight: bold;
Expand All @@ -33,6 +35,10 @@ ocs-panel {
.ocs-panel-container {
font-size: 12px;
padding: 4px;

ul li {
list-style: inside;
}
}
}

Expand Down
147 changes: 147 additions & 0 deletions packages/scripts/src/browser/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { createApp, defineComponent, ref, h, isVNode, App, watch } from "vue";
import { definedScripts } from ".";
import { getItem } from "../browser.entry";
import { createFooter, createNote, createHeaders, createContainers } from "./common/create.element";
import { DefineScript } from "./common/define.script";
import { dragEnable, getCurrentPanels, addEvent, getCurrentRoutes } from "./common/utils";

/** 面板元素 */
export let panel: HTMLElement | undefined | null;
/** vue app 元素 */
export let app: App;

export interface StartOptions {
/**
* 面板样式 url | string
*/
style?: string;
/**
* 是否开启面板拖拽
*/
draggable?: boolean;
/** 脚本列表 */
scripts?: DefineScript[];
}

/**
* 显示面板,检测是否存在需要运行的脚本,并执行
*/
export function start(options?: StartOptions) {
showPanels(options);
executeScripts(options?.scripts);
}

/**
* 显示面板
*/
export function showPanels(options?: StartOptions) {
const { style = "", draggable, scripts = definedScripts } = options || {};

app = createApp(createPanel());
panel = document.createElement("ocs-panel");
document.body.appendChild(panel);
app.mount(panel);

if (draggable) {
dragEnable(panel);
}

function createPanel() {
return defineComponent({
data() {
const panels = ref(getCurrentPanels(scripts));
const activeKey = ref(panels.value[0]?.name);

history.pushState = addEvent(history, "pushState");
history.replaceState = addEvent(history, "replaceState");

window.addEventListener("pushState", () => (panels.value = getCurrentPanels(scripts)));
window.addEventListener("replaceState", () => (panels.value = getCurrentPanels(scripts)));

watch(panels, () => {
activeKey.value = panels.value[0]?.name;
});

return { panels, activeKey };
},
render() {
const footer = createFooter();
const styleElement =
/** 添加样式 */
/[a-zA-z]+:\/\/[^\s]*/.test(style)
? h("link", {
href: style,
type: "text/css",
rel: "stylesheet",
})
: h("style", style);
const about = createNote("进入相应的学习,作业,或者考试页面即可", "此窗口可以使用鼠标拖拽");

const main =
this.panels.length === 0
? [createHeaders(h("div", { class: "title" }, "OCS助手")), createContainers(about), footer]
: [
/** 添加 tab 栏 */
createHeaders(
this.panels.map((panel) =>
h(
"div",
{
class: ["title", this.activeKey === panel.name ? "active" : ""].join(" "),
onClick: () => {
// 隐藏其他面板,显示点击的面板
this.activeKey = panel.name;
},
},
panel.name
)
)
),

/** 显示面板 */
createContainers(
this.panels.map((panel, i) => {
const el = panel.el();

let element;
if (isVNode(el)) {
element = defineComponent({ render: () => el });
} else if (typeof el === "string") {
element = defineComponent({ template: el });
} else {
element = el;
}

return h(element, {
panel: panel.name,

style: {
display: this.activeKey === panel.name ? "block" : "none",
},
});
})
),

footer,
];

return h("div", [styleElement, ...main]);
},
});
}
}

/**
* 执行脚本
*/
export function executeScripts(scripts: DefineScript[] = definedScripts) {
const routes = getCurrentRoutes(scripts);

for (const route of routes) {
let setting;
if (route.settingPath) {
setting = getItem(route.settingPath);
}
route.script(setting);
}
}
79 changes: 77 additions & 2 deletions packages/scripts/src/browser/common/create.element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defaults } from "lodash";
import { DefineComponent, defineComponent, defineCustomElement, h, VNode, VNodeArrayChildren, VNodeProps } from "vue";
import { setItem } from "./store";
import { getItem, setItem } from "./store";
/**
* 创建自定义元素
*/
Expand All @@ -21,7 +21,10 @@ export function createCustomElement(name: string, element: any) {
export function createNote(...notes: string[]) {
return h(
"div",
notes.map((note) => h("p", note))
h(
"ul",
notes.map((note) => h("li", note))
)
);
}

Expand Down Expand Up @@ -235,3 +238,75 @@ function createSettingItem(input: SettingSelect | SettingInput) {
]
);
}

/**
* 公共答题设置组件
* @param label 设置备注
* @param ref 本地设置路径
* @param defaultValue 默认值
*/
export function createWorkerSetting(
label: string,
ref: string,
defaultValue?: {
upload: string;
answererWrappers: string;
}
): [SettingSelect, SettingInput] {
return [
{
label,
ref,
type: "select",
attrs: {
title: "查题之后不管是否提交, 都会自动将选项进行保存。",
},
options: (
[
{
label: "关闭自动答题",
value: "close",
},
...[10, 20, 30, 40, 50, 60, 70, 80, 90].map((rate) => ({
label: `查到大于${rate}%的题目则自动提交`,
value: rate,
attrs: {
title: `例如: 100题, 搜索到大于 ${rate} 的题, 则会自动提交答案。`,
},
})),
{
label: "每个题目都查到答案才自动提交",
value: "100",
},
] as SettingSelect["options"]
)?.map((option) => {
if (option.value === defaultValue?.upload) {
option.attrs = option.attrs || {};
option.attrs.selected = true;
}
return option;
}),
},
{
label: "题库配置",
ref: "setting.zhs.work.answererWrappers",
type: "text",
icons: [
{
type: "bi bi-question-circle",
attrs: {
title: "点击查看题库配置教程",
},
onClick() {
window.open("https://ocs.enncy.cn/answerer-wrappers");
},
},
],
attrs: {
title: "自定义搜题构造器, 如需设置, 前往API查看 https://github.enncy/online-course-script/ ",
value: defaultValue?.answererWrappers || "",
required: true,
},
},
];
}
Loading

0 comments on commit 449c008

Please sign in to comment.