Skip to content

Commit

Permalink
fix: 修复当拓展页面无法加载时不自动安装脚本的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Nov 10, 2023
1 parent 34106a8 commit 9766c4a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
29 changes: 13 additions & 16 deletions packages/app/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ export class ScriptWorker {
/** 执行的自动化脚本列表 */
playwrightScripts: PS[] = [];
/** 可关闭的浏览器拓展主页 */
closeableExtensionHomepages: string[] = [];
store?: AppStore;

init({
store,
uid,
cachePath,
playwrightScripts,
closeableExtensionHomepages
playwrightScripts
}: {
store: AppStore;
uid: string;
cachePath: string;
playwrightScripts: PS[];
closeableExtensionHomepages: string[];
}) {
this.debug('正在初始化进程...');

Expand All @@ -49,7 +46,6 @@ export class ScriptWorker {

// 自动化脚本
this.playwrightScripts = playwrightScripts;
this.closeableExtensionHomepages = closeableExtensionHomepages;

// 初始化日志
this.logger = new LoggerCore(store.paths['logs-path'], false, 'script', path.basename(cachePath));
Expand Down Expand Up @@ -107,7 +103,7 @@ export class ScriptWorker {
? `http://localhost:${this.store?.server.port || 15319}/index.html#/bookmarks`
: undefined,
serverPort: this.store?.server.port || 15319,
closeableExtensionHomepages: this.closeableExtensionHomepages,
closeableExtensionHomepages: ['docs.scriptcat.org', 'tampermonkey.net/index.php'],
actionsKey: this.store?.server.actions_key || '',
...options
});
Expand Down Expand Up @@ -449,26 +445,27 @@ async function waitAndCloseExtensionHomepage(opts: { browser: BrowserContext; cl
clearInterval(interval);
reject(new Error('浏览器拓展加载超时,请尝试重启浏览器,或者查看网络情况。'));
}, 60 * 1000);
const interval = setInterval(() => {
const includes = [];
const interval = setInterval(async () => {
const includes: Page[] = [];
for (const page of opts.browser.pages()) {
if (opts.closeableExtensionHomepages.some((homepage) => page.url().includes(homepage))) {
// 当拓展主页无法访问时,会跳转到chrome-error://chromewebdata/,此时获取的url为chrome-error://chromewebdata/,而不是拓展主页的url,但是title是拓展主页的host
const title = page.url() === 'chrome-error://chromewebdata/' ? await page.title() : '';
if (
opts.closeableExtensionHomepages.some((homepage) =>
page.url() === 'chrome-error://chromewebdata/' ? homepage.includes(title) : page.url().includes(homepage)
)
) {
includes.push(page);
}
}
if (includes.length) {
clearInterval(interval);
clearTimeout(timeout);
Promise.all(
opts.browser
.pages()
.filter((page) => opts.closeableExtensionHomepages.some((homepage) => page.url().includes(homepage)))
.map((page) => page.close())
)
Promise.all(includes.map(async (page) => page.close()))
.then(resolve)
.catch(reject);
}
});
}, 1000);
});
}

Expand Down
24 changes: 10 additions & 14 deletions packages/web/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ export const config = reactive({
title: '浏览器列表'
}
},
{
name: 'dashboard',
path: 'dashboard',
component: shallowRef(dashboard),
meta: {
icon: 'image',
title: '监控列表'
}
},
{
name: 'user-scripts',
path: 'user-scripts',
Expand All @@ -65,6 +56,15 @@ export const config = reactive({
title: '应用中心'
}
},
{
name: 'dashboard',
path: 'dashboard',
component: shallowRef(dashboard),
meta: {
icon: 'image',
title: '监控列表'
}
},
{
name: 'setting',
path: 'setting',
Expand Down Expand Up @@ -209,9 +209,5 @@ export const config = reactive({
}));
}
}
] as ScriptSearchEngine[],
/**
* 可关闭的浏览器拓展主页,用于拓展加载时自动关闭主页,节省浏览器内存
*/
closeableExtensionHomepages: ['docs.scriptcat.org', 'tampermonkey.net/index.php']
] as ScriptSearchEngine[]
});
4 changes: 1 addition & 3 deletions packages/web/src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Message } from '@arco-design/web-vue';
import EventEmitter from 'events';
import { child_process } from './node';
import { notify } from './notify';
import { config } from '../config';

export type RemoteScriptWorker = <W extends keyof ScriptWorker = keyof ScriptWorker>(
event: W,
Expand Down Expand Up @@ -113,8 +112,7 @@ export class Process extends EventEmitter {
store,
cachePath: this.browser.cachePath,
uid: this.uid,
playwrightScripts: this.browser.playwrightScripts,
closeableExtensionHomepages: config.closeableExtensionHomepages
playwrightScripts: this.browser.playwrightScripts
});
}

Expand Down

0 comments on commit 9766c4a

Please # to comment.