diff --git a/packages/common/src/interface.ts b/packages/common/src/interface.ts new file mode 100644 index 00000000..55e6fe13 --- /dev/null +++ b/packages/common/src/interface.ts @@ -0,0 +1,4 @@ +export interface ValidBrowser { + name: string; + path: string | undefined; +} diff --git a/packages/common/src/utils.ts b/packages/common/src/utils.ts index 6483ac2e..b82f726b 100644 --- a/packages/common/src/utils.ts +++ b/packages/common/src/utils.ts @@ -1,15 +1,26 @@ import { existsSync } from 'fs'; import { join } from 'path'; +import { ValidBrowser } from './interface'; // 获取 chrome 路径 -export function getValidBrowserPaths () { - return { - '谷歌浏览器(chrome)': resolveBrowserPath('/Google/Chrome/Application/chrome.exe'), - '微软浏览器(Microsoft Edge)': resolveBrowserPath('/Microsoft/Edge/Application/msedge.exe') - }; +export function getValidBrowsers(): ValidBrowser[] { + return [ + { + name: '微软浏览器(Microsoft Edge)', + path: resolveBrowserPath('Microsoft\\Edge\\Application\\msedge.exe') + }, + { + name: '谷歌浏览器(chrome)', + path: resolveBrowserPath('Google\\Chrome\\Application\\chrome.exe') + }, + { + name: '火狐浏览器(Firefox)', + path: resolveBrowserPath('Mozilla Firefox\\firefox.exe') + } + ]; } -function resolveBrowserPath (commonPath: string) { +function resolveBrowserPath(commonPath: string) { return [ // @ts-ignore join(process.env.ProgramFiles, commonPath), diff --git a/packages/web/src/pages/setting/index.vue b/packages/web/src/pages/setting/index.vue index fac422ad..c91e1129 100644 --- a/packages/web/src/pages/setting/index.vue +++ b/packages/web/src/pages/setting/index.vue @@ -23,32 +23,38 @@ - - 自定义浏览器 - + + 自定义浏览器 + - - {{ launchOptions.executablePath }} - import { LaunchOptions } from '@ocsjs/scripts'; -import { ref } from 'vue'; +import { message } from 'ant-design-vue'; +import { nextTick, onMounted, ref } from 'vue'; import Card from '../../components/Card.vue'; import Description from '../../components/Description.vue'; +import { fs } from '../../components/file/File'; import Path from '../../components/Path.vue'; import { store } from '../../store'; import { remote } from '../../utils/remote'; @@ -168,39 +170,44 @@ const { shell } = require('electron'); const launchOptions = store.script.launchOptions as LaunchOptions; -const keys = Reflect.ownKeys(store.validBrowserPaths); +console.log('可用浏览器', store.validBrowsers); +// 题库配置 const answererWrapper = ref( store.script.localStorage?.setting?.answererWrappers ? JSON.stringify(store.script.localStorage?.setting?.answererWrappers) : '' ); -const browserType = ref( - keys.find( - (key) => (store.validBrowserPaths as any)[key] === launchOptions.executablePath - ) || 'diy' -); - -function reset() { - store.version = undefined; +// 浏览器类型 +const selectedBrowser = ref(store.validBrowsers.find( + (browser) => browser.path === launchOptions.executablePath +) || { + name: '自定义浏览器', + path: 'diy' +}); - remote.app.call('relaunch'); - remote.app.call('exit', 0); -} +console.log(selectedBrowser); +/** + * 监听浏览器类型变化 + */ function onBrowserTypeChange(val: string) { launchOptions.executablePath = val === 'diy' ? '' : val; - browserType.value = - keys.find( - (key) => (store.validBrowserPaths as any)[key] === launchOptions.executablePath - ) || 'diy'; } -function link(url: string) { - shell.openExternal(url); +/** + * 监听自定义浏览器编辑 + */ +function onDiy() { + if (launchOptions.executablePath && !fs.existsSync(launchOptions.executablePath)) { + message.error('自定义的浏览器路径不存在, 请点击右侧按钮查看教程。'); + } } +/** + * 监听题库配置的变化 + */ function onAWChange(e: Event) { // @ts-ignore const val = e.target.value; @@ -209,6 +216,27 @@ function onAWChange(e: Event) { answererWrapper.value = val; } + +/** 如果尚未选择,并且没有自定义路径的话,自动选择第一个 */ +onMounted(() => { + nextTick(() => { + if (store.validBrowsers.length !== 0 && launchOptions.executablePath === '' && selectedBrowser.value.path === 'diy') { + launchOptions.executablePath = store.validBrowsers[0].path; + } + }); +}); + +function link(url: string) { + shell.openExternal(url); +} + +/** 重置设置 */ +function reset() { + store.version = undefined; + remote.app.call('relaunch'); + remote.app.call('exit', 0); +} +