11import 'webextension-polyfill' ;
22import { startListenTabs } from './tabs' ;
33import { ignoreHref } from '@extension/shared' ;
4- import { exampleThemeStorage , translationModeStorage } from '@extension/storage' ;
4+ import { exampleThemeStorage , translationModeStorage , contentUIStateStorage } from '@extension/storage' ;
55import type { AllMessage , QueryResponse , State } from '@extension/shared' ;
66
77console . log ( 'Background loaded' ) ;
@@ -24,6 +24,7 @@ let isConnecting = false;
2424
2525const waitingQuery : { [ key : string ] : { resolves : [ ( value : AllMessage ) => void ] } } = { } ;
2626
27+ // 初始化状态,从 storage 中恢复
2728const state : State = {
2829 interactionMode : 'full' ,
2930 demoMode : false ,
@@ -33,6 +34,29 @@ const state: State = {
3334 inspecting : false ,
3435} ;
3536
37+ // 从 storage 中恢复状态
38+ const initializeStateFromStorage = async ( ) => {
39+ try {
40+ console . log ( 'background: 从 storage 中恢复状态' ) ;
41+ const storedState = await contentUIStateStorage . get ( ) ;
42+ console . log ( 'background: 恢复的状态' , storedState ) ;
43+
44+ // 更新状态,但保持 running 状态为 false(需要 WebSocket 连接)
45+ state . interactionMode = storedState . interactionMode ;
46+ state . demoMode = storedState . demoMode ;
47+ state . inspecting = storedState . inspecting ;
48+ state . ignored = storedState . ignored ;
49+ state . running = false ; // 初始时设为 false,等 WebSocket 连接成功后再设为 true
50+
51+ console . log ( 'background: 状态已恢复' , state ) ;
52+ } catch ( error ) {
53+ console . error ( 'background: 恢复状态失败' , error ) ;
54+ }
55+ } ;
56+
57+ // 在 background script 启动时恢复状态
58+ initializeStateFromStorage ( ) ;
59+
3660const syncStateToContent = async ( ) => {
3761 const tabs = await chrome . tabs . query ( { } ) ;
3862 tabs . forEach ( tab => {
@@ -83,9 +107,21 @@ const listenMessageForUI = (
83107 }
84108 case 'SetState' : {
85109 const { interactionMode, demoMode, inspecting } = message ;
110+ console . log ( 'background: 收到 SetState' , { interactionMode, demoMode, inspecting } ) ;
111+
86112 state . interactionMode = interactionMode ;
87113 state . demoMode = demoMode ;
88114 state . inspecting = inspecting ;
115+
116+ // 同步到 storage
117+ contentUIStateStorage . updateGlobalState ( {
118+ running : state . running ,
119+ ignored : state . ignored ,
120+ interactionMode,
121+ demoMode,
122+ inspecting,
123+ } ) ;
124+
89125 syncStateToContent ( ) ;
90126 return false ;
91127 }
@@ -132,6 +168,16 @@ const connectWebSocket = () => {
132168 isConnecting = false ;
133169 ws ?. send ( JSON . stringify ( { type : 'ping' } ) ) ;
134170 state . running = true ;
171+
172+ // 更新 storage 中的 running 状态
173+ contentUIStateStorage . updateGlobalState ( {
174+ running : true ,
175+ ignored : state . ignored ,
176+ interactionMode : state . interactionMode ,
177+ demoMode : state . demoMode ,
178+ inspecting : state . inspecting ,
179+ } ) ;
180+
135181 syncStateToContent ( ) ;
136182 } ;
137183
@@ -152,6 +198,16 @@ const connectWebSocket = () => {
152198 console . error ( '❌ WebSocket 错误:' , err ) ;
153199 ws ?. close ( ) ; // 主动触发 onclose
154200 state . running = false ;
201+
202+ // 更新 storage 中的 running 状态
203+ contentUIStateStorage . updateGlobalState ( {
204+ running : false ,
205+ ignored : state . ignored ,
206+ interactionMode : state . interactionMode ,
207+ demoMode : state . demoMode ,
208+ inspecting : state . inspecting ,
209+ } ) ;
210+
155211 syncStateToContent ( ) ;
156212 } ;
157213
@@ -160,6 +216,16 @@ const connectWebSocket = () => {
160216 isConnecting = false ;
161217 ws = null ;
162218 state . running = false ;
219+
220+ // 更新 storage 中的 running 状态
221+ contentUIStateStorage . updateGlobalState ( {
222+ running : false ,
223+ ignored : state . ignored ,
224+ interactionMode : state . interactionMode ,
225+ demoMode : state . demoMode ,
226+ inspecting : state . inspecting ,
227+ } ) ;
228+
163229 syncStateToContent ( ) ;
164230 } ;
165231
@@ -169,6 +235,16 @@ const connectWebSocket = () => {
169235 isConnecting = false ;
170236 success = false ;
171237 state . running = false ;
238+
239+ // 更新 storage 中的 running 状态
240+ contentUIStateStorage . updateGlobalState ( {
241+ running : false ,
242+ ignored : state . ignored ,
243+ interactionMode : state . interactionMode ,
244+ demoMode : state . demoMode ,
245+ inspecting : state . inspecting ,
246+ } ) ;
247+
172248 syncStateToContent ( ) ;
173249 }
174250
0 commit comments