From 049234c280835363d1e56581cbe782d8e98424e6 Mon Sep 17 00:00:00 2001 From: enncy <877526278@qq.com> Date: Wed, 25 Oct 2023 15:53:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=B7=BB=E5=8A=A0=E4=BC=98?= =?UTF-8?q?=E5=85=88=E7=BA=A7=E9=80=89=E9=A1=B9=EF=BC=8C=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E7=89=B9=E5=AE=9A=E8=84=9A=E6=9C=AC=E7=9A=84=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/interfaces/script.ts | 8 +++++++- packages/core/src/utils/start.ts | 2 +- packages/scripts/src/projects/common.ts | 20 +++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/core/src/interfaces/script.ts b/packages/core/src/interfaces/script.ts index a20b3f36..eeeb9d92 100644 --- a/packages/core/src/interfaces/script.ts +++ b/packages/core/src/interfaces/script.ts @@ -24,6 +24,8 @@ export interface ScriptOptions> { configs?: ScriptConfigsProvider; /** 不显示脚本页 */ hideInPanel?: boolean; + /** 运行优先级 */ + priority?: number; } export type ScriptConfigs = { @@ -103,6 +105,8 @@ export class Script< methods: M = Object.create({}); /** 自定义事件触发器,避免使用 script.emit , script.on 导致与原有的事件冲突,使用 script.event.emit 和 script.event.on */ event: EventEmitter = new EventEmitter(); + /** 运行优先级,默认0 */ + priority: number; get configs() { if (!this._resolvedConfigs) { @@ -128,7 +132,8 @@ export class Script< onbeforeunload, onrender, onhistorychange, - methods + methods, + priority }: ScriptOptions & { onstart?: (this: Script, ...args: any) => any; onactive?: (this: Script, ...args: any) => any; @@ -152,6 +157,7 @@ export class Script< this.onrender = this.errorHandler(onrender); this.onhistorychange = this.errorHandler(onhistorychange); this.methods = methods?.bind(this)() || Object.create({}); + this.priority = priority ?? 0; if (this.methods) { for (const key in methods) { diff --git a/packages/core/src/utils/start.ts b/packages/core/src/utils/start.ts index 260c08cc..7705f607 100644 --- a/packages/core/src/utils/start.ts +++ b/packages/core/src/utils/start.ts @@ -65,7 +65,7 @@ export async function start(startConfig: StartConfig) { return p; }); - const scripts = $.getMatchedScripts(startConfig.projects, [location.href]); + const scripts = $.getMatchedScripts(startConfig.projects, [location.href]).sort((a, b) => b.priority - a.priority); /** 执行脚本 */ scripts.forEach((script) => { diff --git a/packages/scripts/src/projects/common.ts b/packages/scripts/src/projects/common.ts index 94c88ace..bf8adc3c 100644 --- a/packages/scripts/src/projects/common.ts +++ b/packages/scripts/src/projects/common.ts @@ -920,15 +920,21 @@ export const CommonProject = Project.create({ name: '禁止弹窗', url: [['所有页面', /.*/]], hideInPanel: true, + priority: 1, onstart() { + function disableDialog(msg: string) { + $modal('alert', { + profile: '弹窗来自:' + location.origin, + content: msg + }); + } + try { - $gm.unsafeWindow.alert = (msg) => { - $modal('alert', { - profile: '弹窗来自:' + location.origin, - content: msg - }); - }; - } catch (e) {} + $gm.unsafeWindow.alert = disableDialog; + window.alert = disableDialog; + } catch (e) { + console.error(e); + } } }), apps: new Script({