Skip to content

Commit

Permalink
fix(script): 优化没有题库配置的提示
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed May 9, 2023
1 parent 30b303f commit dc55244
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export async function defaultAnswerWrapperHandler(
): Promise<SearchInformation[]> {
const searchInfos: SearchInformation[] = [];
const temp: AnswererWrapper[] = JSON.parse(JSON.stringify(answererWrappers));
if (temp.length === 0) {
throw new Error('题库配置不能为空,请配置后重新开始自动答题。');
}
// 多线程请求
await Promise.all(
temp.map(async (wrapper) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConfigElement } from '../elements/config';
import { Script } from '../interfaces';
import { Config } from '../interfaces/config';
import { $ } from './common';
import { ElementChildren, ElementHandler, el } from './dom';
import { CustomElementStyleAttrs, ElementChildren, ElementHandler, el } from './dom';
import { $elements } from './elements';
import { $gm } from './tampermonkey';

Expand Down Expand Up @@ -117,7 +117,7 @@ export const $creator = {
},
button(
text?: string,
attrs?: Omit<Partial<HTMLInputElement>, 'type'> | undefined,
attrs?: CustomElementStyleAttrs<Omit<Partial<HTMLInputElement>, 'type'>> | undefined,
handler?: ElementHandler<'input'> | undefined
) {
return el('input', { type: 'button', ...attrs }, function (btn) {
Expand Down
10 changes: 9 additions & 1 deletion packages/scripts/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ export interface CommonWorkOptions {
export function workPreCheckMessage(
options: CommonWorkOptions & {
onrun: (opts: CommonWorkOptions) => void;
/**
* 当没有题库配置时的回调
*/
onNoAnswererWrappers?: (opts: CommonWorkOptions) => void;
/**
* 手动关闭时的回调
*/
onclose?: (opts: CommonWorkOptions, closedMessage: MessageElement) => void;
}
) {
const { onrun, onclose, ...opts } = options;
const { onrun, onNoAnswererWrappers, onclose, ...opts } = options;

if (opts.answererWrappers.length === 0) {
onNoAnswererWrappers?.(opts);
return $message('warn', { content: '检测到题库配置为空,无法自动答题,请前往全局设置页面进行配置。' });
} else {
return $message('info', {
Expand Down
32 changes: 31 additions & 1 deletion packages/scripts/src/utils/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ export function commonWork(
CommonProject.scripts.render.methods.pin(script);

let worker: OCSWorker<any> | undefined;
/**
* 是否已经按下了开始按钮
*/
let startBtnPressed = false;
/**
* 是否检查失败
*/
let checkFailed = false;

/** 显示答题控制按钮 */
const createControls = () => {
Expand Down Expand Up @@ -53,12 +60,35 @@ export function commonWork(
const workResultPanel = () => CommonProject.scripts.workResults.methods.createWorkResultsPanel();

script.on('render', () => {
script.panel?.body?.replaceChildren(createControls().container, workResultPanel());
let gotoSettingsBtnContainer: string | HTMLElement = '';
if (checkFailed) {
const gotoSettingsBtn = $creator.button('👉 前往设置题库配置', {
className: 'base-style-button',
style: { flex: '1', padding: '4px' }
});
gotoSettingsBtn.style.flex = '1';
gotoSettingsBtn.style.padding = '4px';
gotoSettingsBtn.onclick = () => {
CommonProject.scripts.render.methods.pin(CommonProject.scripts.settings);
};
gotoSettingsBtnContainer = el('div', { style: { display: 'flex' } }, [gotoSettingsBtn]);
}

script.panel?.body?.replaceChildren(
el('div', { style: { marginTop: '12px' } }, [
gotoSettingsBtnContainer,
createControls().container,
workResultPanel()
])
);
});

let checkMessage = workPreCheckMessage({
onrun: () => startBtnPressed === false && start(),
onclose: (_, closedMsg) => (checkMessage = closedMsg),
onNoAnswererWrappers: () => {
checkFailed = true;
},
...CommonProject.scripts.settings.cfg
});

Expand Down

0 comments on commit dc55244

Please sign in to comment.