feat: 优化浏览器扩展popup界面布局

This commit is contained in:
2026-08-16 23:03:38 +08:00
parent 12a5f4039f
commit 50ce131d60
2 changed files with 341 additions and 112 deletions
+94 -109
View File
@@ -7,7 +7,7 @@ import {
} from '@/api/open/extensionFeatures';
import type { ExtensionFeatureItem } from '@/features/runtime/types';
import { urlMatchesPatterns } from '@/features/runtime/types';
import { EXTENSION_NAME, OPEN_SCOPES } from '@/shared/constants';
import { EXTENSION_NAME } from '@/shared/constants';
import { toUserMessage } from '@/shared/errors';
import { getSettings, saveSettings } from '@/storage/settings';
@@ -28,6 +28,9 @@ 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 : [];
@@ -35,40 +38,46 @@ const matchedFeatures = computed(() => {
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);
}
/** 只保存 URL / Key,不改动悬浮层里的店铺配置 */
async function persistSettings() {
errorText.value = '';
try {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
statusText.value = '配置已保存';
} catch (error) {
errorText.value = toUserMessage(error);
}
async function saveConfig() {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
}
async function checkConnectivity() {
async function persistAndCheck() {
checking.value = true;
errorText.value = '';
statusText.value = '';
try {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
await saveConfig();
const manifest = await fetchFeatureManifest();
const count = manifest.features?.length ?? 0;
statusText.value = `连通正常(清单 ${count} 项${manifest.updatedAt ? `,更新于 ${manifest.updatedAt}` : ''})`;
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;
}
@@ -87,16 +96,14 @@ async function loadFeatures() {
featuresLoading.value = true;
errorText.value = '';
try {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
await saveConfig();
const manifest = await fetchFeatureManifest();
features.value = manifest.features ?? [];
featuresUpdatedAt.value = manifest.updatedAt ?? '';
statusText.value = `已加载功能清单(${features.value.length})`;
statusText.value = `清单已刷新(${features.value.length})`;
} catch (error) {
errorText.value = toUserMessage(error);
configOpen.value = true;
} finally {
featuresLoading.value = false;
}
@@ -116,18 +123,11 @@ async function handleOpenFeature(item: ExtensionFeatureItem) {
errorText.value = '';
statusText.value = '';
try {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
await saveConfig();
const remote = await downloadAndVerifyFeature(item);
statusText.value = remote.verified
? `远程脚本已校验(${item.version})`
: `已下载远程脚本(未配置 sha256)`;
await mountFeatureOnPage(item.id, remote.code);
activeFeatureId.value = item.id;
statusText.value = `已打开:${item.name}(远程脚本)`;
statusText.value = `已打开 ${item.name}`;
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
@@ -141,7 +141,7 @@ async function handleCloseFeature() {
try {
await unmountFeatureOnPage();
activeFeatureId.value = null;
statusText.value = '已关闭当前功能';
statusText.value = '已关闭';
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
@@ -153,100 +153,95 @@ onMounted(async () => {
await loadLocalSettings();
await refreshTabUrl();
await refreshPanelState();
if (apiBaseUrl.value && apiKey.value) {
if (hasConfig.value) {
await loadFeatures();
}
});
</script>
<template>
<div class="flex w-[360px] flex-col gap-3 bg-antd-bg p-4 text-antd-text">
<header>
<h1 class="text-base font-semibold tracking-tight text-antd-text">{{ EXTENSION_NAME }}</h1>
<p class="mt-0.5 text-xs text-antd-text-secondary">
Popup 负责配置与功能入口;店铺选择在页内悬浮层完成。
</p>
<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 class="space-y-2 rounded-antd-lg border border-antd-border-secondary bg-white p-3 shadow-sm">
<h2 class="text-xs font-semibold text-antd-text-secondary">Open API 配置</h2>
<label class="block text-xs text-antd-text-secondary">
<section v-show="configOpen" class="popup-card space-y-2">
<label class="popup-label">
Base URL
<input
v-model="apiBaseUrl"
class="mt-1 w-full rounded-antd border border-antd-border px-2.5 py-1.5 text-sm outline-none transition focus:border-antd-primary focus:shadow-[0_0_0_2px_rgba(22,119,255,0.2)]"
class="popup-input"
placeholder="https://host/api"
autocomplete="off"
/>
</label>
<label class="block text-xs text-antd-text-secondary">
<label class="popup-label">
API Key
<input
v-model="apiKey"
class="popup-input"
type="password"
class="mt-1 w-full rounded-antd border border-antd-border px-2.5 py-1.5 text-sm outline-none transition focus:border-antd-primary focus:shadow-[0_0_0_2px_rgba(22,119,255,0.2)]"
placeholder="sk_live_..."
autocomplete="off"
/>
</label>
<p class="text-[11px] leading-relaxed text-antd-text-tertiary">
连通性 / 清单需要 {{ OPEN_SCOPES.extensionFeatureRead }};推广费另需店铺/导入 scope。
</p>
<div class="flex gap-2">
<button
class="flex-1 rounded-antd border border-antd-border bg-white px-3 py-1.5 text-sm text-antd-text transition hover:border-antd-primary hover:text-antd-primary disabled:opacity-50"
type="button"
:disabled="busy || checking"
@click="persistSettings"
>
保存配置
</button>
<button
class="flex-1 rounded-antd bg-antd-primary px-3 py-1.5 text-sm font-medium text-white transition hover:bg-antd-primary-hover disabled:opacity-50"
type="button"
:disabled="busy || checking || !apiBaseUrl.trim() || !apiKey.trim()"
@click="checkConnectivity"
>
{{ checking ? '检查中…' : '检查连通性' }}
</button>
</div>
<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="space-y-2 rounded-antd-lg border border-antd-border-secondary bg-white p-3 shadow-sm">
<div class="flex items-center justify-between">
<h2 class="text-xs font-semibold text-antd-text-secondary">功能列表</h2>
<section class="popup-card space-y-2">
<div class="flex items-center justify-between gap-2">
<h2 class="popup-section-title">功能</h2>
<button
class="text-xs text-antd-primary hover:underline"
class="popup-link"
type="button"
:disabled="featuresLoading || busy || checking"
:disabled="featuresLoading || busy || checking || !hasConfig"
:title="featuresUpdatedAt ? `更新于 ${featuresUpdatedAt}` : ''"
@click="loadFeatures"
>
{{ featuresLoading ? '加载中…' : '刷新清单' }}
{{ featuresLoading ? '…' : '刷新' }}
</button>
</div>
<p v-if="featuresUpdatedAt" class="text-[11px] text-antd-text-tertiary">
清单更新于 {{ featuresUpdatedAt }}
</p>
<p v-if="!features.length" class="text-xs text-antd-text-tertiary">
尚未加载。配置 URL / Key 后可先「检查连通性」,再「刷新清单」。
<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="rounded-antd border border-antd-border-secondary bg-antd-fill px-2.5 py-2"
>
<div class="flex items-start justify-between gap-2">
<div class="min-w-0">
<div class="truncate text-sm font-medium text-antd-text">{{ item.name }}</div>
<div class="text-[11px] text-antd-text-tertiary">
{{ item.id }} · v{{ item.version }}
</div>
<p v-if="item.description" class="mt-1 text-[11px] text-antd-text-secondary">
{{ item.description }}
</p>
</div>
<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
class="shrink-0 rounded-antd bg-antd-primary px-2.5 py-1 text-xs font-medium text-white hover:bg-antd-primary-hover disabled:opacity-50"
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)"
@@ -255,19 +250,9 @@ onMounted(async () => {
</button>
</div>
</div>
<button
v-if="activeFeatureId"
class="w-full rounded-antd border border-antd-border bg-white px-3 py-1.5 text-sm transition hover:border-antd-primary hover:text-antd-primary disabled:opacity-50"
type="button"
:disabled="busy || checking"
@click="handleCloseFeature"
>
关闭当前功能
</button>
</section>
<p v-if="statusText" class="text-xs text-antd-success">{{ statusText }}</p>
<p v-if="errorText" class="text-xs text-antd-error">{{ errorText }}</p>
<p v-if="statusText" class="popup-status">{{ statusText }}</p>
<p v-if="errorText" class="popup-error">{{ errorText }}</p>
</div>
</template>
+247 -3
View File
@@ -5,10 +5,9 @@
html,
body {
margin: 0;
min-width: 360px;
min-height: 480px;
min-width: 320px;
color: rgba(0, 0, 0, 0.88);
background: #ffffff;
background: #f5f5f5;
font-family:
-apple-system,
BlinkMacSystemFont,
@@ -18,4 +17,249 @@ body {
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;
}