Skip to content

Commit

Permalink
fix(app): 修复软件浏览器选择BUG,并停止火狐浏览器使用。
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed May 18, 2022
1 parent 51e26de commit 5e2559b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
5 changes: 3 additions & 2 deletions packages/app/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ process.on('message', async (message) => {

/** 加载油猴 */
const pathToExtension = path.join(__dirname, './extensions/Tampermonkey');
launchOptions.args = [`--disable-extensions-except=${pathToExtension}`, `--load-extension=${pathToExtension}`];
launchOptions.args =
[`--disable-extensions-except=${pathToExtension}`, `--load-extension="${pathToExtension}"`];

const { browser: _browser, page: _page } = await ocs.launchScripts({
userDataDir,
Expand Down Expand Up @@ -103,7 +104,7 @@ process.on('message', async (message) => {
} else if (action === 'close') {
console.log(bgBlueBright(loggerPrefix('info')), '任务已关闭!');
logger.info('任务已关闭!');
await browser.close();
await browser?.close();
browser = undefined;
page = undefined;
} else if (action === 'call') {
Expand Down
14 changes: 12 additions & 2 deletions packages/scripts/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OCSApi } from '@ocsjs/common';
import { Browser, BrowserContext, chromium, LaunchOptions, Page } from 'playwright';
import { Browser, BrowserContext, BrowserType, chromium, LaunchOptions, Page, webkit } from 'playwright';
import { CX, ZHS } from '.';
import { ScriptFunction, ScriptOptions } from './types';

Expand Down Expand Up @@ -58,9 +58,19 @@ process.on('uncaughtException', (e) => {
*/
export async function launchScripts({ userDataDir, launchOptions, scripts, sync = true, init }: LaunchScriptsOptions) {
let browser: BrowserContext;
let target: BrowserType;

/** 确定浏览器类型 */
if (launchOptions.executablePath?.includes('Firefox')) {
throw new Error('暂不支持 firefox 浏览器,请切换其他浏览器重试。');
} else if (launchOptions.executablePath?.includes('Safari')) {
target = webkit;
} else {
target = chromium;
}

if (userDataDir) {
browser = await chromium.launchPersistentContext(userDataDir, {
browser = await target.launchPersistentContext(userDataDir, {
viewport: null,
ignoreHTTPSErrors: true,
...launchOptions
Expand Down
30 changes: 13 additions & 17 deletions packages/web/src/pages/setting/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
<Card title="默认设置">
<Description label="指定浏览器">
<a-select
v-model:value="selectedBrowser.path"
v-model:value="selectedPath"
:title="launchOptions.executablePath"
size="small"
class="w-100"
@change="onBrowserTypeChange"
>
<template
v-for="(browser, index) in store.validBrowsers"
Expand All @@ -47,7 +47,7 @@
</a-select>
</Description>
<Description
v-if="selectedBrowser.path === 'diy'"
v-if="selectedPath === 'diy'"
label="自定义浏览器路径"
>
<a-input
Expand Down Expand Up @@ -159,7 +159,7 @@
<script setup lang="ts">
import { LaunchOptions } from '@ocsjs/scripts';
import { message } from 'ant-design-vue';
import { nextTick, onMounted, ref } from 'vue';
import { nextTick, onMounted, ref, watch } from 'vue';
import Card from '../../components/Card.vue';
import Description from '../../components/Description.vue';
import { fs } from '../../components/file/File';
Expand All @@ -179,22 +179,14 @@ const answererWrapper = ref(
: ''
);
// 浏览器类型
const selectedBrowser = ref(store.validBrowsers.find(
(browser) => browser.path === launchOptions.executablePath
) || {
name: '自定义浏览器',
path: 'diy'
});
console.log(selectedBrowser);
const selectedPath = ref('diy');
/**
* 监听浏览器类型变化
*/
function onBrowserTypeChange(val: string) {
launchOptions.executablePath = val === 'diy' ? '' : val;
}
watch(selectedPath, () => {
launchOptions.executablePath = selectedPath.value === 'diy' ? '' : selectedPath.value;
});
/**
* 监听自定义浏览器编辑
Expand All @@ -220,9 +212,13 @@ function onAWChange(e: Event) {
/** 如果尚未选择,并且没有自定义路径的话,自动选择第一个 */
onMounted(() => {
nextTick(() => {
if (store.validBrowsers.length !== 0 && launchOptions.executablePath === '' && selectedBrowser.value.path === 'diy') {
if (store.validBrowsers.length !== 0 && launchOptions.executablePath === '') {
launchOptions.executablePath = store.validBrowsers[0].path;
}
selectedPath.value = store.validBrowsers.find(
(browser) => browser.path === launchOptions.executablePath
)?.path || 'diy';
});
});
Expand Down

0 comments on commit 5e2559b

Please sign in to comment.