Skip to content

Commit 4c478b7

Browse files
committed
Refactor state management by removing ignoreHref and replacing it with ignoreDomain for improved domain handling. Updated related components and hooks to reflect this change, ensuring consistent state usage across the application.
1 parent b7aa028 commit 4c478b7

File tree

12 files changed

+83
-82
lines changed

12 files changed

+83
-82
lines changed

chrome-extension/src/background/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'webextension-polyfill';
22
import { startListenTabs } from './tabs';
3-
import { ignoreHref } from '@extension/shared';
43
import { exampleThemeStorage, translationModeStorage, contentUIStateStorage } from '@extension/storage';
5-
import type { AllMessage, QueryResponse, State, ElementPosition } from '@extension/shared';
4+
import type { AllMessage, QueryResponse, State } from '@extension/shared';
65

76
console.log('Background loaded');
87
console.log("Edit 'chrome-extension/src/background/index.ts' and save to reload.");
@@ -28,7 +27,6 @@ const waitingQuery: { [key: string]: { resolves: [(value: AllMessage) => void] }
2827
const state: State = {
2928
interactionMode: 'full',
3029
demoMode: false,
31-
ignored: false,
3230
running: false,
3331
ignoreHref,
3432
inspecting: false,
@@ -47,7 +45,6 @@ const initializeStateFromStorage = async () => {
4745
state.demoMode = storedState.demoMode;
4846
state.inspecting = storedState.inspecting;
4947
state.showBBox = storedState.showBBox;
50-
state.ignored = storedState.ignored;
5148
state.running = false; // 初始时设为 false,等 WebSocket 连接成功后再设为 true
5249

5350
console.log('background: 状态已恢复', state);
@@ -119,7 +116,6 @@ const listenMessageForUI = (
119116
// 同步到 storage
120117
contentUIStateStorage.updateGlobalState({
121118
running: state.running,
122-
ignored: state.ignored,
123119
interactionMode,
124120
demoMode,
125121
inspecting,
@@ -202,7 +198,6 @@ const connectWebSocket = () => {
202198
// 更新 storage 中的 running 状态
203199
contentUIStateStorage.updateGlobalState({
204200
running: true,
205-
ignored: state.ignored,
206201
interactionMode: state.interactionMode,
207202
demoMode: state.demoMode,
208203
inspecting: state.inspecting,
@@ -233,7 +228,6 @@ const connectWebSocket = () => {
233228
// 更新 storage 中的 running 状态
234229
contentUIStateStorage.updateGlobalState({
235230
running: false,
236-
ignored: state.ignored,
237231
interactionMode: state.interactionMode,
238232
demoMode: state.demoMode,
239233
inspecting: state.inspecting,
@@ -252,7 +246,6 @@ const connectWebSocket = () => {
252246
// 更新 storage 中的 running 状态
253247
contentUIStateStorage.updateGlobalState({
254248
running: false,
255-
ignored: state.ignored,
256249
interactionMode: state.interactionMode,
257250
demoMode: state.demoMode,
258251
inspecting: state.inspecting,
@@ -272,7 +265,6 @@ const connectWebSocket = () => {
272265
// 更新 storage 中的 running 状态
273266
contentUIStateStorage.updateGlobalState({
274267
running: false,
275-
ignored: state.ignored,
276268
interactionMode: state.interactionMode,
277269
demoMode: state.demoMode,
278270
inspecting: state.inspecting,

packages/shared/lib/utils/ignoreHref.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export const ignoreDomain = [
2+
// 🔮 LLM 相关平台
3+
'chatgpt.com',
4+
'chat.openai.com', // ✅ 实际用户更多使用此地址
5+
'chat.deepseek.com',
6+
'deepseek.com',
7+
'gemini.google.com/app',
8+
'kimi.com',
9+
'claude.ai',
10+
'grok.com',
11+
12+
// 🇨🇳 中文网站(内容大概率为中文,不建议翻译)
13+
'163.com', // 网易
14+
'qq.com', // 腾讯
15+
'weixin.qq.com', // 微信官网
16+
'jd.com', // 京东
17+
'taobao.com', // 淘宝
18+
'tmall.com', // 天猫
19+
'sina.com.cn', // 新浪新闻
20+
'weibo.com', // 微博
21+
'zhihu.com', // 知乎
22+
'bilibili.com', // 哔哩哔哩
23+
'douyin.com', // 抖音网页版
24+
'kuaishou.com', // 快手
25+
'xhs.com', // 小红书
26+
'huxiu.com', // 虎嗅
27+
'36kr.com', // 36氪
28+
'cctv.com', // 央视网
29+
'people.com.cn', // 人民网
30+
'chinanews.com.cn', // 中国新闻网
31+
'gov.cn', // 中国政府网
32+
'gamersky.com',
33+
'bilibili.com',
34+
't.bilibili.com',
35+
'zh.wikipedia.org',
36+
37+
'oa.rwkvos.com',
38+
];
39+
40+
export const isSameDomain = (href: string, domain: string): boolean => {
41+
try {
42+
const url = new URL(href);
43+
return url.hostname === domain;
44+
} catch (_) {
45+
return false; // 无效 URL
46+
}
47+
};

packages/shared/lib/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from './colorful-logger.js';
22
export * from './formatQueryText.js';
33
export * from './formatTranslation.js';
44
export * from './helpers.js';
5-
export * from './ignoreHref.js';
5+
export * from './ignoredDomains.js';
66
export * from './init-app-with-shadow.js';
77
export * from './is-chinese.js';
88
export * from './is-url.js';

packages/shared/lib/utils/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ export type AllMessage =
6969
export interface State {
7070
interactionMode: 'hover' | 'full';
7171
demoMode: boolean;
72-
ignored: boolean;
7372
running: boolean;
74-
ignoreHref: string[];
73+
ignoreDomain: string[];
7574
inspecting: boolean;
7675
showBBox: boolean;
7776
}

packages/storage/lib/base/types.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,5 @@ export type ContentUIStorageType = BaseStorageType<ContentUIStateType> & {
8282
toggleDemoMode: () => Promise<void>;
8383
toggleDiagnoseMode: () => Promise<void>;
8484
toggleBBox: () => Promise<void>;
85-
updateGlobalState: (state: {
86-
running: boolean;
87-
ignored: boolean;
88-
interactionMode: 'hover' | 'full';
89-
demoMode: boolean;
90-
inspecting: boolean;
91-
showBBox: boolean;
92-
}) => Promise<void>;
85+
updateGlobalState: (state: Partial<ContentUIStateType>) => Promise<void>;
9386
};

pages/content-ui/src/Dashboard.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ export const Dashboard: FC = () => {
1010
// 状态
1111
hovered,
1212
shouldShowOthers,
13-
running,
14-
inspecting, // 添加缺失的 inspecting 状态
1513
// 操作方法
1614
setHovered,
1715
setHoveringOthers,
16+
isIgnored,
1817
} = useContentUIState();
1918

2019
// 全屏检测
@@ -73,6 +72,9 @@ export const Dashboard: FC = () => {
7372
return null;
7473
}
7574

75+
const showInteractionMode = false;
76+
const showDemoMode = false;
77+
7678
return (
7779
<div style={dashboardStyle}>
7880
<div onMouseEnter={handleEntryMouseEnter} onMouseLeave={handleEntryMouseLeave}>
@@ -97,10 +99,10 @@ export const Dashboard: FC = () => {
9799
onMouseLeave={handleOthersMouseLeave}>
98100
<RunningStatus style={widgetAnimationStyle} />
99101
<IgnoredStatus style={widgetAnimationStyle} />
100-
<InteractionMode style={widgetAnimationStyle} />
101-
<DemoMode style={widgetAnimationStyle} />
102-
<DiagnoseMode style={widgetAnimationStyle} />
103-
<BBox style={widgetAnimationStyle} />
102+
{showInteractionMode && <InteractionMode style={widgetAnimationStyle} />}
103+
{showDemoMode && <DemoMode style={widgetAnimationStyle} />}
104+
{!isIgnored && <DiagnoseMode style={widgetAnimationStyle} />}
105+
{!isIgnored && <BBox style={widgetAnimationStyle} />}
104106
</div>
105107
</div>
106108
);

pages/content-ui/src/components/IgnoredStatus.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import type { FC } from 'react';
66
export const IgnoredStatus: FC<{
77
style?: React.CSSProperties;
88
}> = ({ style }) => {
9-
const { ignored } = useContentUIState();
9+
const { isIgnored } = useContentUIState();
1010

1111
return (
1212
<Base
13-
icon={ignored ? <FaEyeSlash /> : <FaEye />}
13+
icon={isIgnored ? <FaEyeSlash /> : <FaEye />}
1414
title="页面被忽略了"
15-
value={ignored ? '是' : '否'}
15+
value={isIgnored ? '是' : '否'}
1616
style={style}
1717
/>
1818
);

pages/content-ui/src/hooks/useContentUIState.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useStorage, ignoreHref, rwkvEvent } from '@extension/shared';
1+
import { useStorage, rwkvEvent, ignoreDomain, isSameDomain } from '@extension/shared';
22
import { contentUIStateStorage } from '@extension/storage';
33
import { useEffect, useState } from 'react';
44

@@ -10,6 +10,9 @@ export const useContentUIState = () => {
1010
const [hovered, setHovered] = useState(false);
1111
const [hoveringOthers, setHoveringOthers] = useState(false);
1212

13+
// 计算当前页面是否被忽略
14+
const isIgnored = ignoreDomain.some(domain => isSameDomain(window.location.href, domain));
15+
1316
// 监听来自background的状态变化
1417
useEffect(() => {
1518
const handleStateChanged = (event: CustomEvent) => {
@@ -20,7 +23,6 @@ export const useContentUIState = () => {
2023
// 只更新从 background 传来的状态,保持本地状态不变
2124
contentUIStateStorage.updateGlobalState({
2225
running,
23-
ignored: ignoreHref.some(href => window.location.href.startsWith(href)),
2426
interactionMode,
2527
demoMode,
2628
inspecting,
@@ -62,6 +64,9 @@ export const useContentUIState = () => {
6264
hovered,
6365
hoveringOthers,
6466

67+
// 页面状态
68+
isIgnored,
69+
6570
// UI状态计算属性 - 诊断模式或HUD诊断模式开启时保持显示
6671
shouldShowOthers: hovered || hoveringOthers || globalState.inspecting || globalState.showBBox,
6772

@@ -71,6 +76,7 @@ export const useContentUIState = () => {
7176
hovered,
7277
hoveringOthers,
7378
shouldShowOthers: hovered || hoveringOthers || globalState.inspecting || globalState.showBBox,
79+
isIgnored,
7480
},
7581

7682
// 全局操作方法

pages/content/src/contentStart.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { injectCss } from './injectcss';
22
import { parseNode } from './parseNode';
33
import { startPositionMonitoring, stopPositionMonitoring, createTestPosition } from './positionMonitor';
44
import { state } from './state';
5-
import { ignoreHref, rwkvClass, rwkvEvent } from '@extension/shared';
5+
import { isSameDomain, rwkvClass, rwkvEvent } from '@extension/shared';
66
import type { AllMessage } from '@extension/shared';
77

88
export const contentStart = () => {
@@ -206,10 +206,8 @@ export const contentStart = () => {
206206

207207
const observer = new MutationObserver(mutationsList => {
208208
const currentUrl = window.location.href;
209-
for (const href of ignoreHref) {
210-
const startWith = currentUrl.startsWith(href);
211-
if (startWith) return;
212-
}
209+
const isIgnored = state.ignoreDomain.some(domain => isSameDomain(currentUrl, domain));
210+
if (isIgnored) return;
213211

214212
for (const mutation of mutationsList) {
215213
const type = mutation.type;

0 commit comments

Comments
 (0)