mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 11:09:19 +08:00
refactor: 修改结构 功能动态从后端获取
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import type { FeatureHandle, FeatureModule, FeatureMountContext } from '@/features/runtime/types';
|
||||
import {
|
||||
isFloatingVisible,
|
||||
setFloatingVisible,
|
||||
toggleFloatingPanel,
|
||||
} from '@/content/floating/mount';
|
||||
|
||||
/**
|
||||
* 嵌入式实现:与远程发布的 pdd-ad-fee 同 id。
|
||||
* 壳内直接 mount;远程 JS 用于版本对齐与后续 main-world 执行。
|
||||
*/
|
||||
export const pddAdFeeEmbeddedFeature: FeatureModule = {
|
||||
id: 'pdd-ad-fee',
|
||||
async mount(_ctx: FeatureMountContext): Promise<FeatureHandle> {
|
||||
setFloatingVisible(true);
|
||||
return {
|
||||
unmount: () => {
|
||||
setFloatingVisible(false);
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export function isPddAdFeePanelVisible(): boolean {
|
||||
return isFloatingVisible();
|
||||
}
|
||||
|
||||
export function togglePddAdFeePanel(): boolean {
|
||||
return toggleFloatingPanel();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 远程功能包入口(IIFE 构建)。
|
||||
* 当前阶段导出元数据;真正 UI 仍由壳内 embedded 模块挂载。
|
||||
* 后续可将 FloatingPanel 整包打入此入口并在 main world 执行。
|
||||
*/
|
||||
const FEATURE_ID = 'pdd-ad-fee';
|
||||
|
||||
export interface RemoteFeatureExport {
|
||||
id: string;
|
||||
version: string;
|
||||
executionWorld: 'extension';
|
||||
}
|
||||
|
||||
const meta: RemoteFeatureExport = {
|
||||
id: FEATURE_ID,
|
||||
version: '0.1.0',
|
||||
executionWorld: 'extension',
|
||||
};
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
__ECBI_FEATURES__?: Record<string, RemoteFeatureExport>;
|
||||
}
|
||||
}
|
||||
|
||||
const g = globalThis as typeof globalThis & {
|
||||
__ECBI_FEATURES__?: Record<string, RemoteFeatureExport>;
|
||||
};
|
||||
g.__ECBI_FEATURES__ = g.__ECBI_FEATURES__ ?? {};
|
||||
g.__ECBI_FEATURES__[FEATURE_ID] = meta;
|
||||
|
||||
export default meta;
|
||||
@@ -0,0 +1,40 @@
|
||||
import { pddAdFeeEmbeddedFeature } from '@/features/pdd-ad-fee/embedded';
|
||||
import type { FeatureHandle, FeatureModule, FeatureMountContext } from '@/features/runtime/types';
|
||||
|
||||
const embeddedModules: Record<string, FeatureModule> = {
|
||||
[pddAdFeeEmbeddedFeature.id]: pddAdFeeEmbeddedFeature,
|
||||
};
|
||||
|
||||
let active: { id: string; handle: FeatureHandle } | null = null;
|
||||
|
||||
export function getActiveFeatureId(): string | null {
|
||||
return active?.id ?? null;
|
||||
}
|
||||
|
||||
export async function mountFeature(
|
||||
featureId: string,
|
||||
ctx: FeatureMountContext = { featureId },
|
||||
): Promise<void> {
|
||||
if (active?.id === featureId) {
|
||||
return;
|
||||
}
|
||||
await unmountActiveFeature();
|
||||
|
||||
const mod = embeddedModules[featureId];
|
||||
if (!mod) {
|
||||
throw new Error(`功能「${featureId}」未在壳内注册,请更新扩展或检查远程清单`);
|
||||
}
|
||||
const handle = await mod.mount({ ...ctx, featureId });
|
||||
active = { id: featureId, handle };
|
||||
}
|
||||
|
||||
export async function unmountActiveFeature(): Promise<void> {
|
||||
if (!active) return;
|
||||
const current = active;
|
||||
active = null;
|
||||
await current.handle.unmount();
|
||||
}
|
||||
|
||||
export function listEmbeddedFeatureIds(): string[] {
|
||||
return Object.keys(embeddedModules);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/** 功能清单与模块约定(壳 / 远程脚本共用) */
|
||||
|
||||
export interface ExtensionFeatureItem {
|
||||
id: string;
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
/** 匹配当前页才可打开,chrome match pattern 风格简化:含 host 即可 */
|
||||
matchUrlPatterns: string[];
|
||||
/** 相对静态目录的入口文件,如 pdd-ad-fee.js */
|
||||
entry: string;
|
||||
/** 脚本 sha256(hex),可选;有则下载后校验 */
|
||||
sha256?: string;
|
||||
/** extension=壳内嵌入模块;main=注入页面(远期) */
|
||||
executionWorld?: 'extension' | 'main';
|
||||
}
|
||||
|
||||
export interface ExtensionFeatureManifest {
|
||||
schemaVersion: number;
|
||||
updatedAt?: string;
|
||||
features: ExtensionFeatureItem[];
|
||||
}
|
||||
|
||||
export interface FeatureHandle {
|
||||
unmount: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface FeatureModule {
|
||||
id: string;
|
||||
mount: (ctx: FeatureMountContext) => FeatureHandle | Promise<FeatureHandle>;
|
||||
}
|
||||
|
||||
export interface FeatureMountContext {
|
||||
featureId: string;
|
||||
/** 打开/关闭由壳控制时可回调 */
|
||||
onClose?: () => void;
|
||||
}
|
||||
|
||||
export function urlMatchesPatterns(url: string, patterns: string[] | null | undefined): boolean {
|
||||
if (!patterns?.length) return true;
|
||||
try {
|
||||
return patterns.some((pattern) => matchPattern(url, pattern));
|
||||
} catch {
|
||||
// 非法 pattern 时不拖垮 Popup
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 将 chrome 风格 * 通配转为安全正则(先按 * 切开再转义各段) */
|
||||
function matchPattern(url: string, pattern: string): boolean {
|
||||
if (!pattern) return false;
|
||||
if (!pattern.includes('*')) {
|
||||
return url.includes(pattern);
|
||||
}
|
||||
const parts = pattern.split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&'));
|
||||
return new RegExp(`^${parts.join('.*')}$`).test(url);
|
||||
}
|
||||
Reference in New Issue
Block a user