Skip to content

Commit

Permalink
feat(core): 添加 $creator 元素创建工具类变量,并且重写登录脚本,使用动态添加元素的方式重写。
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Dec 31, 2022
1 parent 9438a1e commit ba94b16
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 148 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/elements/container.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { el, tooltip } from '../utils/dom';
import { $creator } from '../utils/creator';
import { el } from '../utils/dom';
import { HeaderElement } from './header';
import { IElement } from './interface';

export class ContainerElement extends IElement {
header: HeaderElement = tooltip(el('header-element', { className: 'header', title: '菜单栏-可拖动区域' }));
header: HeaderElement = $creator.tooltip(el('header-element', { className: 'header', title: '菜单栏-可拖动区域' }));
body: HTMLDivElement = el('div', { className: 'body', clientHeight: window.innerHeight / 2 });
footer: HTMLDivElement = el('div', { className: 'footer' });

Expand Down
48 changes: 17 additions & 31 deletions packages/core/src/projects/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { definedCustomElements } from '../elements';
import { MessageElement } from '../elements/message';
import { ModelElement } from '../elements/model';

import { Config } from '../interfaces/config';
import { cors } from '../interfaces/cors';
import { Project } from '../interfaces/project';
import { Script } from '../interfaces/script';
import { getMatchedScripts, namespaceKey } from '../utils/common';
import { el, enableElementDraggable, tooltip } from '../utils/dom';
import { getMatchedScripts } from '../utils/common';
import { $creator } from '../utils/creator';
import { el, enableElementDraggable } from '../utils/dom';
import { StartConfig } from '../utils/start';
import { humpToTarget } from '../utils/string';
import { addConfigChangeListener, getInfos, getValue, notification } from '../utils/tampermonkey';
Expand Down Expand Up @@ -68,7 +68,7 @@ const InitPanelScript = new Script({
const initHeader = (urls: string[] = getValue('_urls_', [location.href])) => {
container.header.replaceChildren();
/** 图标 */
container.header.logo = tooltip(
container.header.logo = $creator.tooltip(
el('img', {
src: getInfos().script.icon || '',
width: 18,
Expand Down Expand Up @@ -119,7 +119,7 @@ const InitPanelScript = new Script({
updatePanelProjectName(name);
});
});
const projectSelectorDiv = tooltip(
const projectSelectorDiv = $creator.tooltip(
el(
'div',
{
Expand All @@ -138,7 +138,7 @@ const InitPanelScript = new Script({
/** 是否展开所有脚本 */
const isExpandAll = () => this.cfg.expandAll === true;
/** 脚本切换按钮 */
const expandSwitcher = tooltip(
const expandSwitcher = $creator.tooltip(
el('div', {
className: 'panel-switch',
title: isExpandAll() ? '收缩脚本' : '展开脚本',
Expand All @@ -158,7 +158,7 @@ const InitPanelScript = new Script({
/** 窗口是否最小化 */
const isMinimize = () => this.cfg.visual === 'minimize';
/** 窗口状态切换按钮 */
const visualSwitcher = tooltip(
const visualSwitcher = $creator.tooltip(
el('div', {
className: 'switch ',
title: isMinimize() ? '点击展开窗口' : '点击最小化窗口',
Expand All @@ -173,7 +173,7 @@ const InitPanelScript = new Script({
container.header.visualSwitcher = visualSwitcher;

/** 窗口关闭按钮 */
container.header.closeButton = tooltip(
container.header.closeButton = $creator.tooltip(
el('div', {
className: 'close ',
innerText: 'x',
Expand Down Expand Up @@ -206,7 +206,15 @@ const InitPanelScript = new Script({
script.panel = scriptPanel;

scriptPanel.notesContainer.innerHTML = script.cfg.notes || '';
scriptPanel.configsBody.append(...createConfigs(script.namespace, script.configs || {}));
const els = $creator.configs(script.namespace, script.configs || {});
const elList = [];
for (const key in els) {
if (Object.prototype.hasOwnProperty.call(els, key)) {
elList.push(els[key]);
}
}

scriptPanel.configsBody.append(...elList);
scriptPanel.configsContainer.append(scriptPanel.configsBody);

return scriptPanel;
Expand Down Expand Up @@ -297,28 +305,6 @@ const InitPanelScript = new Script({
};
/** 创建设置区域 */

const createConfigs = (namespace: string | undefined, configs: Record<string, Config<any>>) => {
const elements = [];
for (const key in configs) {
if (Object.prototype.hasOwnProperty.call(configs, key)) {
const cfg = configs[key];
if (cfg.label !== undefined) {
const element = el('config-element', {
key: namespaceKey(namespace, key),
tag: cfg.tag,
sync: cfg.sync,
attrs: cfg.attrs,
_onload: cfg.onload
});
element.label.textContent = cfg.label;
elements.push(element);
}
}
}

return elements;
};

/** 初始化模态框系统 */
const initModelSystem = () => {
// 添加 models 监听队列
Expand Down
106 changes: 54 additions & 52 deletions packages/core/src/projects/zhs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AnswererWrapper, defaultAnswerWrapperHandler } from '../core/worker/answer.wrapper.handler';
import { OCSWorker } from '../core/worker/worker';
import { ConfigElement } from '../elements/config';
import { MessageElement } from '../elements/message';
import { Config } from '../interfaces/config';
import { Project } from '../interfaces/project';
import { Script } from '../interfaces/script';
import { sleep } from '../utils/common';
import { $creator } from '../utils/creator';
import { $$el, $el, el } from '../utils/dom';
import { $createSelectOptions } from '../utils/script';
import { StringUtils } from '../utils/string';
import { addConfigChangeListener, getValue } from '../utils/tampermonkey';
import { getValue } from '../utils/tampermonkey';
import { $message, $model } from './init';

const volume: Config = {
Expand All @@ -34,7 +35,7 @@ const definition: Config = {
defaultValue: 'line1bq',
onload() {
this.append(
...$createSelectOptions(this.getAttribute('value'), [
...$creator.selectOptions(this.getAttribute('value'), [
['line1bq', '流畅'],
['line1gq', '高清']
])
Expand All @@ -54,7 +55,7 @@ const workConfigs: Record<'upload' | 'notes', Config> = {
attrs: { title: '答题设置, 鼠标悬浮在选项上可以查看每个选项的具体解释。' },
onload() {
this.append(
...$createSelectOptions(this.getAttribute('value'), [
...$creator.selectOptions(this.getAttribute('value'), [
['close', '关闭自动答题', '关闭自动答题后, 脚本将忽略答题, 自动进入下一节。'],
['save', '完成后自动保存', '完成后自动保存答案, 注意如果你开启了随机作答, 有可能分辨不出答案是否正确。'],
[
Expand Down Expand Up @@ -105,10 +106,10 @@ export const ZHSProject: Project = {
label: '定时停止',
tag: 'select',
attrs: { title: '到时间后自动暂停脚本' },
defaultValue: 0,
defaultValue: '0',
onload() {
this.append(
...$createSelectOptions(this.getAttribute('value'), [
...$creator.selectOptions(this.getAttribute('value'), [
[0, '关闭'],
[0.5, '半小时后'],
[1, '一小时后'],
Expand All @@ -126,7 +127,7 @@ export const ZHSProject: Project = {
defaultValue: 1,
onload() {
this.append(
...$createSelectOptions(
...$creator.selectOptions(
this.getAttribute('value'),
[1, 1.25, 1.5].map((rate) => [rate, rate + 'x'])
)
Expand Down Expand Up @@ -181,7 +182,7 @@ export const ZHSProject: Project = {

// 监听速度
this.onConfigChange('playbackRate', (curr) => {
switchPlaybackRate(parseFloat(curr));
switchPlaybackRate(parseFloat(curr.toString()));
});

// 监听清晰度
Expand Down Expand Up @@ -393,7 +394,7 @@ export const ZHSProject: Project = {
attrs: { title: '答题设置, 鼠标悬浮在选项上可以查看每个选项的具体解释。' },
onload() {
this.append(
...$createSelectOptions(this.getAttribute('value'), [
...$creator.selectOptions(this.getAttribute('value'), [
['close', '关闭自动答题', '关闭自动答题后, 脚本将忽略答题, 自动进入下一节。'],
['nomove', '开启自动答题']
])
Expand Down Expand Up @@ -496,51 +497,54 @@ export const ZHSProject: Project = {
type: {
label: '登录类型',
tag: 'select',
defaultValue: 'phone',
defaultValue: 'phone' as 'phone' | 'id',
onload() {
this.append(
...$createSelectOptions(this.getAttribute('value') || '', [
...$creator.selectOptions(this.getAttribute('value') || '', [
['phone', '手机号登录'],
['id', '学号登录']
])
);
}
},
phone: {
label: '手机',
defaultValue: '',
onload() {
const style = (type: string) =>
(this.parentElement!.parentElement!.style.display = type === 'id' ? 'none' : 'table-row');
style(getValue('zhs.login.type'));
addConfigChangeListener('zhs.login.type', (pre, curr) => style(curr));
}
},
onrender({ panel }) {
let els: Record<string, ConfigElement<any>>;
/** 监听更改 */
this.onConfigChange('type', () => {
for (const key in els) {
if (Object.prototype.hasOwnProperty.call(els, key)) {
els[key].remove();
}
}
},
school: {
label: '学校',
defaultValue: '',
onload() {
const style = (type: string) =>
(this.parentElement!.parentElement!.style.display = type === 'id' ? 'table-row' : 'none');
style(getValue('zhs.login.type'));
addConfigChangeListener('zhs.login.type', (pre, curr) => style(curr));
// 删除后重新渲染
render();
});

const render = () => {
/** 动态创建设置 */
const passwordConfig: Config = { label: '密码', defaultValue: '', attrs: { type: 'password' } };
if (this.cfg.type === 'phone') {
els = $creator.configs('zhs.login', {
phone: { label: '手机', defaultValue: '' },
password: passwordConfig
});
} else {
els = $creator.configs('zhs.login', {
school: { label: '学校', defaultValue: '' },
id: { label: '学号', defaultValue: '' },
password: passwordConfig
});
}
},
id: {
label: '学号',
defaultValue: '',
onload() {
const style = (type: string) =>
(this.parentElement!.parentElement!.style.display = type === 'id' ? 'table-row' : 'none');
style(getValue('zhs.login.type'));
addConfigChangeListener('zhs.login.type', (pre, curr) => style(curr));

for (const key in els) {
if (Object.prototype.hasOwnProperty.call(els, key)) {
panel.configsBody.append(els[key]);
}
}
},
password: {
label: '密码',
defaultValue: '',
attrs: { type: 'password' }
}
};

render();
},
oncomplete() {
if (!this.cfg.disable) {
Expand All @@ -549,22 +553,20 @@ export const ZHSProject: Project = {
const idLogin = $el('#qStudentID');
if (this.cfg.type === 'phone') {
phoneLogin.click();
$el('#lUsername').value = this.cfg.phone;
$el('#lPassword').value = this.cfg.password;

await sleep(2000);
$el('#lUsername').value = getValue('zhs.login.phone');
$el('#lPassword').value = getValue('zhs.login.password');
} else {
idLogin.click();
const search = $el('#quickSearch');
search.onfocus?.(new FocusEvent('focus'));
search.value = this.cfg.school;
search.value = getValue('zhs.login.school');
search.onclick?.(new MouseEvent('click'));

// 等待搜索
await sleep(2000);

$el('#schoolListCode > li').click();
$el('#clCode').value = this.cfg.id;
$el('#clPassword').value = this.cfg.password;
$el('#clCode').value = getValue('zhs.login.id');
$el('#clPassword').value = getValue('zhs.login.password');
}

// 点击登录
Expand Down
Loading

0 comments on commit ba94b16

Please sign in to comment.