@@ -6,6 +6,18 @@ let websocket: WebSocket | null = null
66let is_reconnecting = false
77let last_ping_timestamp = Date . now ( )
88
9+ browser . storage . onChanged . addListener ( ( changes , areaName ) => {
10+ if ( areaName == 'local' && changes [ 'browser-profile-name' ] ) {
11+ if ( websocket ) {
12+ is_reconnecting = false
13+ const old_ws = websocket
14+ websocket = null
15+ old_ws . close ( )
16+ }
17+ connect_websocket ( )
18+ }
19+ } )
20+
921export const check_server_health = async ( ) : Promise < boolean > => {
1022 try {
1123 const response = await fetch ( `http://localhost:${ DEFAULT_PORT } /health` )
@@ -16,7 +28,7 @@ export const check_server_health = async (): Promise<boolean> => {
1628}
1729
1830export const check_and_recover_connection = ( ) => {
19- if ( websocket ?. readyState === WebSocket . OPEN ) {
31+ if ( websocket ?. readyState == WebSocket . OPEN ) {
2032 if ( Date . now ( ) - last_ping_timestamp > 20000 ) {
2133 console . warn (
2234 'WebSocket connection is stale (no recent pings). Force reconnecting...'
@@ -28,8 +40,8 @@ export const check_and_recover_connection = () => {
2840
2941 if (
3042 ! is_reconnecting &&
31- websocket ?. readyState !== WebSocket . OPEN &&
32- websocket ?. readyState !== WebSocket . CONNECTING
43+ websocket ?. readyState != WebSocket . OPEN &&
44+ websocket ?. readyState != WebSocket . CONNECTING
3345 ) {
3446 connect_websocket ( )
3547 }
@@ -38,8 +50,8 @@ export const check_and_recover_connection = () => {
3850export const connect_websocket = async ( ) : Promise < void > => {
3951 if (
4052 is_reconnecting ||
41- websocket ?. readyState === WebSocket . OPEN ||
42- websocket ?. readyState === WebSocket . CONNECTING
53+ websocket ?. readyState == WebSocket . OPEN ||
54+ websocket ?. readyState == WebSocket . CONNECTING
4355 ) {
4456 return
4557 }
@@ -59,9 +71,17 @@ export const connect_websocket = async (): Promise<void> => {
5971 const version = manifest . version
6072 const user_agent = navigator . userAgent
6173
62- const ws = new WebSocket (
63- `ws://localhost:${ DEFAULT_PORT } ?token=${ SECURITY_TOKENS . BROWSERS } &version=${ version } &user_agent=${ encodeURIComponent ( user_agent ) } `
64- )
74+ const storage = await browser . storage . local . get ( 'browser-profile-name' )
75+ const profile_name = (
76+ storage [ 'browser-profile-name' ] as string | undefined
77+ ) ?. trim ( )
78+
79+ let ws_url = `ws://localhost:${ DEFAULT_PORT } ?token=${ SECURITY_TOKENS . BROWSERS } &version=${ version } &user_agent=${ encodeURIComponent ( user_agent ) } `
80+ if ( profile_name ) {
81+ ws_url += `&profile_name=${ encodeURIComponent ( profile_name ) } `
82+ }
83+
84+ const ws = new WebSocket ( ws_url )
6585 websocket = ws
6686
6787 ws . onopen = ( ) => {
0 commit comments