11import 'webextension-polyfill' ;
22import { startListenTabs } from './tabs' ;
33import { ignoreHref } from '@extension/shared' ;
4- import { exampleThemeStorage , translationModeStorage , contentUIStateStorage } from '@extension/storage' ;
5- import type { AllMessage , QueryResponse , State , ElementPosition } from '@extension/shared' ;
6-
7- console . log ( 'Background loaded' ) ;
8- console . log ( "Edit 'chrome-extension/src/background/index.ts' and save to reload." ) ;
9-
10- exampleThemeStorage . get ( ) . then ( theme => {
11- console . log ( 'theme' , theme ) ;
12- } ) ;
13-
14- translationModeStorage . get ( ) . then ( mode => {
15- console . log ( 'mode' , mode ) ;
16- } ) ;
4+ import { contentUIStateStorage } from '@extension/storage' ;
5+ import type { AllMessage , QueryResponse , State } from '@extension/shared' ;
176
187const WS_PORT = 52346 ;
198const WS_URL = `ws://localhost:${ WS_PORT } /ws` ;
@@ -38,9 +27,7 @@ const state: State = {
3827// 从 storage 中恢复状态
3928const initializeStateFromStorage = async ( ) => {
4029 try {
41- console . log ( 'background: 从 storage 中恢复状态' ) ;
4230 const storedState = await contentUIStateStorage . get ( ) ;
43- console . log ( 'background: 恢复的状态' , storedState ) ;
4431
4532 // 更新状态,但保持 running 状态为 false(需要 WebSocket 连接)
4633 state . interactionMode = storedState . interactionMode ;
@@ -49,10 +36,8 @@ const initializeStateFromStorage = async () => {
4936 state . showBBox = storedState . showBBox ;
5037 state . ignored = storedState . ignored ;
5138 state . running = false ; // 初始时设为 false,等 WebSocket 连接成功后再设为 true
52-
53- console . log ( 'background: 状态已恢复' , state ) ;
54- } catch ( error ) {
55- console . error ( 'background: 恢复状态失败' , error ) ;
39+ } catch ( e ) {
40+ console . error ( e ) ;
5641 }
5742} ;
5843
@@ -99,7 +84,6 @@ const listenMessageForUI = (
9984 return true ;
10085 }
10186 case 'GetState' : {
102- console . log ( 'Background.Send: GetStateResponse' , state ) ;
10387 sendResponse ( {
10488 func : 'GetStateResponse' ,
10589 ...state ,
@@ -109,7 +93,6 @@ const listenMessageForUI = (
10993 }
11094 case 'SetState' : {
11195 const { interactionMode, demoMode, inspecting, showBBox } = message ;
112- console . log ( 'background: 收到 SetState' , { interactionMode, demoMode, inspecting, showBBox } ) ;
11396
11497 state . interactionMode = interactionMode ;
11598 state . demoMode = demoMode ;
@@ -150,7 +133,7 @@ const listenMessageForUI = (
150133 body : { positions, tabId : actualTabId } ,
151134 } ) ;
152135 } catch ( e ) {
153- console . warn ( 'background: 转发位置同步消息失败' , e ) ;
136+ console . error ( e ) ;
154137 }
155138 }
156139
@@ -186,15 +169,13 @@ const connectWebSocket = () => {
186169 return ;
187170 }
188171
189- console . log ( '🔌 尝试连接 WebSocket...' ) ;
190172 isConnecting = true ;
191173 let success = false ;
192174
193175 try {
194176 ws = new WebSocket ( WS_URL ) ;
195177
196178 ws . onopen = ( ) => {
197- console . log ( '✅ WebSocket 已连接' ) ;
198179 isConnecting = false ;
199180 ws ?. send ( JSON . stringify ( { type : 'ping' } ) ) ;
200181 state . running = true ;
0 commit comments