refacor: 重构目录架构

This commit is contained in:
2026-09-16 22:43:21 +08:00
parent b37cdb0d54
commit 2754ed1e45
2762 changed files with 0 additions and 1529 deletions
+258
View File
@@ -0,0 +1,258 @@
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import {
downloadAndVerifyFeature,
fetchFeatureManifest,
} from '@/api/open/extensionFeatures';
import type { ExtensionFeatureItem } from '@/features/runtime/types';
import { urlMatchesPatterns } from '@/features/runtime/types';
import { EXTENSION_NAME } from '@/shared/constants';
import { toUserMessage } from '@/shared/errors';
import { getSettings, saveSettings } from '@/storage/settings';
import {
getFloatingPanelState,
mountFeatureOnPage,
unmountFeatureOnPage,
} from './messaging';
const apiBaseUrl = ref('');
const apiKey = ref('');
const features = ref<ExtensionFeatureItem[]>([]);
const featuresLoading = ref(false);
const featuresUpdatedAt = ref('');
const activeFeatureId = ref<string | null>(null);
const currentTabUrl = ref('');
const statusText = ref('');
const errorText = ref('');
const busy = ref(false);
const checking = ref(false);
const configOpen = ref(false);
const hasConfig = computed(() => Boolean(apiBaseUrl.value.trim() && apiKey.value.trim()));
const matchedFeatures = computed(() => {
const list = Array.isArray(features.value) ? features.value : [];
if (!currentTabUrl.value) return list;
return list.filter((f) => urlMatchesPatterns(currentTabUrl.value, f?.matchUrlPatterns ?? []));
});
const configSummary = computed(() => {
if (!hasConfig.value) return '未配置';
try {
const host = new URL(apiBaseUrl.value.trim()).host;
return host || '已配置';
} catch {
return '已配置';
}
});
async function loadLocalSettings() {
const settings = await getSettings();
apiBaseUrl.value = settings.apiBaseUrl;
apiKey.value = settings.apiKey;
// 未配置时默认展开
configOpen.value = !(settings.apiBaseUrl && settings.apiKey);
}
async function saveConfig() {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
}
async function persistAndCheck() {
checking.value = true;
errorText.value = '';
statusText.value = '';
try {
await saveConfig();
const manifest = await fetchFeatureManifest();
features.value = manifest.features ?? [];
featuresUpdatedAt.value = manifest.updatedAt ?? '';
const count = features.value.length;
statusText.value = `已连接 · 清单 ${count} 项`;
configOpen.value = false;
} catch (error) {
errorText.value = toUserMessage(error);
configOpen.value = true;
} finally {
checking.value = false;
}
}
async function refreshTabUrl() {
try {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
currentTabUrl.value = tabs[0]?.url ?? '';
} catch {
currentTabUrl.value = '';
}
}
async function loadFeatures() {
featuresLoading.value = true;
errorText.value = '';
try {
await saveConfig();
const manifest = await fetchFeatureManifest();
features.value = manifest.features ?? [];
featuresUpdatedAt.value = manifest.updatedAt ?? '';
statusText.value = `清单已刷新(${features.value.length})`;
} catch (error) {
errorText.value = toUserMessage(error);
configOpen.value = true;
} finally {
featuresLoading.value = false;
}
}
async function refreshPanelState() {
try {
const state = await getFloatingPanelState();
activeFeatureId.value = state.visible ? 'pdd-ad-fee' : null;
} catch {
activeFeatureId.value = null;
}
}
async function handleOpenFeature(item: ExtensionFeatureItem) {
busy.value = true;
errorText.value = '';
statusText.value = '';
try {
await saveConfig();
const remote = await downloadAndVerifyFeature(item);
await mountFeatureOnPage(item.id, remote.code);
activeFeatureId.value = item.id;
statusText.value = `已打开 ${item.name}`;
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
busy.value = false;
}
}
async function handleCloseFeature() {
busy.value = true;
errorText.value = '';
try {
await unmountFeatureOnPage();
activeFeatureId.value = null;
statusText.value = '已关闭';
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
busy.value = false;
}
}
onMounted(async () => {
await loadLocalSettings();
await refreshTabUrl();
await refreshPanelState();
if (hasConfig.value) {
await loadFeatures();
}
});
</script>
<template>
<div class="popup-root">
<header class="popup-header">
<div class="min-w-0 flex-1">
<h1>{{ EXTENSION_NAME }}</h1>
<p class="popup-sub">{{ configSummary }}</p>
</div>
<button
class="popup-link"
type="button"
:disabled="busy || checking"
@click="configOpen = !configOpen"
>
{{ configOpen ? '收起配置' : '配置' }}
</button>
</header>
<section v-show="configOpen" class="popup-card space-y-2">
<label class="popup-label">
Base URL
<input
v-model="apiBaseUrl"
class="popup-input"
placeholder="https://host/api"
autocomplete="off"
/>
</label>
<label class="popup-label">
API Key
<input
v-model="apiKey"
class="popup-input"
type="password"
placeholder="sk_live_..."
autocomplete="off"
/>
</label>
<button
class="popup-btn popup-btn-primary w-full"
type="button"
:disabled="busy || checking || !apiBaseUrl.trim() || !apiKey.trim()"
@click="persistAndCheck"
>
{{ checking ? '连接中…' : '保存并连接' }}
</button>
</section>
<section class="popup-card space-y-2">
<div class="flex items-center justify-between gap-2">
<h2 class="popup-section-title">功能</h2>
<button
class="popup-link"
type="button"
:disabled="featuresLoading || busy || checking || !hasConfig"
:title="featuresUpdatedAt ? `更新于 ${featuresUpdatedAt}` : ''"
@click="loadFeatures"
>
{{ featuresLoading ? '…' : '刷新' }}
</button>
</div>
<p v-if="!hasConfig" class="popup-hint">先填写上方连接配置</p>
<p v-else-if="!matchedFeatures.length && !featuresLoading" class="popup-hint">
{{ features.length ? '当前页无匹配功能,请打开对应后台页' : '暂无清单,点「刷新」或「保存并连接」' }}
</p>
<div v-for="item in matchedFeatures" :key="item.id" class="popup-feature">
<div class="min-w-0 flex-1">
<div class="truncate text-sm font-medium text-antd-text">{{ item.name }}</div>
<div class="text-[11px] text-antd-text-tertiary">v{{ item.version }}</div>
</div>
<div class="flex shrink-0 gap-1.5">
<button
v-if="activeFeatureId === item.id"
class="popup-btn popup-btn-ghost"
type="button"
:disabled="busy || checking"
@click="handleCloseFeature"
>
关闭
</button>
<button
class="popup-btn popup-btn-primary"
type="button"
:disabled="busy || checking"
@click="handleOpenFeature(item)"
>
{{ activeFeatureId === item.id ? '重新打开' : '打开' }}
</button>
</div>
</div>
</section>
<p v-if="statusText" class="popup-status">{{ statusText }}</p>
<p v-if="errorText" class="popup-error">{{ errorText }}</p>
</div>
</template>
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ECBI 数据同步</title>
</head>
<body class="bg-slate-50 text-slate-900">
<div id="app"></div>
<script type="module" src="./main.ts"></script>
</body>
</html>
+6
View File
@@ -0,0 +1,6 @@
import { createApp } from 'vue';
import App from './App.vue';
import './styles.css';
createApp(App).mount('#app');
@@ -0,0 +1,64 @@
import type { ExtensionMessage, ExtensionResponse } from '@/shared/messages';
import { ExtensionError } from '@/shared/errors';
async function getActiveTabId(): Promise<number> {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const tabId = tabs[0]?.id;
if (tabId == null) {
throw new ExtensionError('NO_ACTIVE_TAB', '未找到当前标签页');
}
return tabId;
}
async function sendToActiveTab<T>(message: ExtensionMessage): Promise<T> {
const tabId = await getActiveTabId();
let response: ExtensionResponse | undefined;
try {
response = (await chrome.tabs.sendMessage(tabId, message)) as ExtensionResponse;
} catch {
throw new ExtensionError(
'PAGE_NOT_READY',
'无法连接页面脚本。请刷新目标页面后再打开功能。',
);
}
if (!response) {
throw new ExtensionError('PAGE_NOT_READY', '页面无响应');
}
if (!response.ok) {
throw new ExtensionError(
(response.code as never) || 'UNKNOWN',
response.message || '页面处理失败',
);
}
return response.data as T;
}
export async function openFloatingPanel(): Promise<{ visible: boolean }> {
return sendToActiveTab<{ visible: boolean }>({
type: 'SET_FLOATING_PANEL',
payload: { visible: true },
});
}
export async function toggleFloatingPanel(): Promise<{ visible: boolean }> {
return sendToActiveTab<{ visible: boolean }>({ type: 'TOGGLE_FLOATING_PANEL' });
}
export async function getFloatingPanelState(): Promise<{ visible: boolean }> {
return sendToActiveTab<{ visible: boolean }>({ type: 'GET_FLOATING_PANEL_STATE' });
}
export async function mountFeatureOnPage(
featureId: string,
code: string,
): Promise<{ featureId: string | null }> {
return sendToActiveTab<{ featureId: string | null }>({
type: 'MOUNT_FEATURE',
payload: { featureId, code },
});
}
export async function unmountFeatureOnPage(): Promise<{ featureId: string | null }> {
return sendToActiveTab<{ featureId: string | null }>({ type: 'UNMOUNT_FEATURE' });
}
+265
View File
@@ -0,0 +1,265 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
html,
body {
margin: 0;
min-width: 320px;
color: rgba(0, 0, 0, 0.88);
background: #f5f5f5;
font-family:
-apple-system,
BlinkMacSystemFont,
'Segoe UI',
Roboto,
'Helvetica Neue',
Arial,
'Noto Sans',
sans-serif;
scrollbar-width: thin;
scrollbar-color: rgb(0 0 0 / 0.28) transparent;
}
html::-webkit-scrollbar,
body::-webkit-scrollbar,
.popup-root::-webkit-scrollbar {
width: 6px;
height: 6px;
}
html::-webkit-scrollbar-thumb,
body::-webkit-scrollbar-thumb,
.popup-root::-webkit-scrollbar-thumb {
background: rgb(0 0 0 / 0.28);
border-radius: 3px;
}
.popup-root {
box-sizing: border-box;
width: 320px;
display: flex;
flex-direction: column;
gap: 10px;
padding: 12px;
background: #f5f5f5;
color: rgba(0, 0, 0, 0.88);
}
.popup-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 8px;
padding: 2px 2px 0;
}
.popup-header h1 {
margin: 0;
font-size: 15px;
font-weight: 600;
line-height: 1.3;
}
.popup-sub {
margin: 2px 0 0;
font-size: 12px;
color: rgba(0, 0, 0, 0.45);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 220px;
}
.popup-card {
border: 1px solid #f0f0f0;
border-radius: 12px;
background: #fff;
padding: 12px;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.03);
}
.popup-section-title {
margin: 0;
font-size: 12px;
font-weight: 600;
color: rgba(0, 0, 0, 0.45);
}
.popup-label {
display: block;
font-size: 12px;
color: rgba(0, 0, 0, 0.45);
}
.popup-input {
box-sizing: border-box;
display: block;
width: 100%;
margin-top: 4px;
border: 1px solid #d9d9d9;
border-radius: 8px;
padding: 6px 10px;
font-size: 13px;
outline: none;
background: #fff;
color: rgba(0, 0, 0, 0.88);
}
.popup-input:focus {
border-color: #1677ff;
box-shadow: 0 0 0 2px rgb(22 119 255 / 0.2);
}
.popup-btn {
border-radius: 8px;
border: 1px solid #d9d9d9;
background: #fff;
color: rgba(0, 0, 0, 0.88);
padding: 5px 10px;
font-size: 12px;
line-height: 1.4;
cursor: pointer;
transition:
color 0.15s,
border-color 0.15s,
background 0.15s;
}
.popup-btn:disabled {
opacity: 0.45;
cursor: not-allowed;
}
.popup-btn-primary {
border-color: #1677ff;
background: #1677ff;
color: #fff;
}
.popup-btn-primary:hover:not(:disabled) {
border-color: #4096ff;
background: #4096ff;
}
.popup-btn-ghost:hover:not(:disabled) {
color: #1677ff;
border-color: #1677ff;
}
.popup-link {
border: 0;
background: transparent;
color: #1677ff;
font-size: 12px;
padding: 0;
cursor: pointer;
white-space: nowrap;
}
.popup-link:hover:not(:disabled) {
text-decoration: underline;
}
.popup-link:disabled {
opacity: 0.45;
cursor: not-allowed;
}
.popup-feature {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
border: 1px solid #f0f0f0;
border-radius: 8px;
background: #fafafa;
padding: 8px 10px;
}
.popup-hint {
margin: 0;
font-size: 12px;
color: rgba(0, 0, 0, 0.25);
line-height: 1.4;
}
.popup-status {
margin: 0;
font-size: 12px;
color: #52c41a;
line-height: 1.4;
}
.popup-error {
margin: 0;
font-size: 12px;
color: #ff4d4f;
line-height: 1.4;
}
.w-full {
width: 100%;
}
.space-y-2 > * + * {
margin-top: 8px;
}
.flex {
display: flex;
}
.items-center {
align-items: center;
}
.justify-between {
justify-content: space-between;
}
.gap-1\.5 {
gap: 6px;
}
.gap-2 {
gap: 8px;
}
.min-w-0 {
min-width: 0;
}
.flex-1 {
flex: 1;
}
.shrink-0 {
flex-shrink: 0;
}
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.text-sm {
font-size: 13px;
}
.font-medium {
font-weight: 500;
}
.text-antd-text {
color: rgba(0, 0, 0, 0.88);
}
.text-antd-text-tertiary {
color: rgba(0, 0, 0, 0.25);
}
.text-\[11px\] {
font-size: 11px;
}