@@ -15,6 +15,13 @@ if (existingTabButlerModalRoot) {
1515 existingTabButlerModalRoot . remove ( ) ;
1616}
1717
18+ const shutDownEvent = "tab-butler-shutdown" ;
19+ // send a shutdown message to any other content script in this tab, disabling them permernently
20+ document . dispatchEvent ( new CustomEvent ( shutDownEvent ) ) ;
21+ // add a listener for the same shutdown event
22+ document . addEventListener ( shutDownEvent , shutdownScript ) ;
23+
24+
1825// by only creating and appending the element dynamically when it is requested, it should save on memory and reduce some parts of the code
1926// like the visibility toggeling and the style tag removal in the root
2027// this should also help in sites where the dom might change from time to time, invalidating
@@ -46,6 +53,7 @@ const messageListener = (messagePayload: MessagePlayload) => {
4653browser . runtime . onMessage . addListener ( messageListener ) ;
4754
4855function mountSearchComponent ( message : Message ) {
56+ console . log ( "mounting..." ) ;
4957 // create a new modal root on mount and append as the last child of the body
5058 tabButlerModalRoot = document . createElement ( "tab-butler-modal" ) ;
5159// needs to be open so that the click event can bubble up
@@ -101,6 +109,14 @@ const unmountOnClick = (event: MouseEvent) => {
101109 }
102110} ;
103111
112+ function shutdownScript ( ) {
113+ console . log ( "shutting down" )
114+ unmountSearchComponent ( ) ;
115+ // remove the listener to so it can't be triggered again
116+ // this prevents any old content scripts in the tab from being triggered in the tab
117+ browser . runtime . onMessage . removeListener ( messageListener ) ;
118+ }
119+
104120window . addEventListener ( "beforeunload" , ( ) => {
105121 if ( isOpen ) {
106122 unmountSearchComponent ( ) ;
0 commit comments