1- /**
2- * Sidworks DevTools for Shopware 6
3- * Background Service Worker - Manages communication between content and devtools
4- */
5-
61const ports = new Map ( ) ;
72
83// Handle connections from content scripts and devtools
94chrome . runtime . onConnect . addListener ( ( port ) => {
105 let portName = port . name ;
116 let tabId = 0 ;
12-
7+
138 // Get tab ID from sender
149 if ( port . sender ?. tab ?. id ) {
1510 tabId = port . sender . tab . id ;
1611 }
17-
12+
1813 // Create unique port identifier
1914 const portKey = `${ portName } :${ tabId } ` ;
2015 console . log ( 'Connected:' , portKey ) ;
21-
16+
2217 ports . set ( portKey , port ) ;
23-
18+
2419 // Handle messages
2520 port . onMessage . addListener ( ( msg ) => {
2621 // Add tab ID if not present
2722 if ( ! msg . tabId && port . sender ?. tab ?. id ) {
2823 msg . tabId = port . sender . tab . id ;
2924 }
30-
25+
3126 if ( ! msg . tabId ) {
3227 msg . tabId = 0 ;
3328 }
34-
29+
3530 console . log ( `Message for ${ msg . to } (${ msg . tabId } ): ${ msg . type } ` ) ;
36-
31+
3732 // Handle background-specific messages
3833 if ( msg . to === 'background' ) {
3934 if ( msg . type === 'icon' ) {
@@ -46,7 +41,7 @@ chrome.runtime.onConnect.addListener((port) => {
4641 forwardMessage ( msg ) ;
4742 }
4843 } ) ;
49-
44+
5045 // Cleanup on disconnect
5146 port . onDisconnect . addListener ( ( ) => {
5247 // Check and suppress BFCache-related errors
@@ -64,7 +59,7 @@ chrome.runtime.onConnect.addListener((port) => {
6459function forwardMessage ( msg ) {
6560 const targetPort = `${ msg . to } :${ msg . tabId } ` ;
6661 const port = ports . get ( targetPort ) ;
67-
62+
6863 if ( port ) {
6964 try {
7065 port . postMessage ( msg ) ;
0 commit comments