@@ -5,18 +5,30 @@ import { UpdateMessage } from "@lib/utils/messages";
55import { getSupportedIntegration } from "@lib/integrations" ;
66import { attachShadow } from "@lib/utils/dom/shadow" ;
77import { CloneButton } from "./content/CloneButton" ;
8+ import _ from "lodash" ;
89
910const MAX_INIT_ATTEMPTS = 12 ;
1011
1112let buttonContainer : HTMLDivElement | null = null ;
1213
14+ const debouncedInit = _ . debounce ( ( ) => init ( ) , 250 ) ;
15+
16+ function isElementVisible ( el : HTMLElement ) {
17+ return el . offsetWidth > 0 && el . offsetHeight > 0 ;
18+ }
19+
1320function init ( attempts : number = 0 ) {
1421 if ( attempts > MAX_INIT_ATTEMPTS ) {
1522 // Too many attempts, aborting
1623 return ;
1724 }
18- if ( document . contains ( buttonContainer ) ) {
19- // ALready initialized
25+ if ( buttonContainer && document . contains ( buttonContainer ) ) {
26+ // Already initialized
27+ if ( ! isElementVisible ( buttonContainer ) ) {
28+ // Button is no longer visible, we need to try to place it again
29+ buttonContainer ?. remove ( ) ;
30+ debouncedInit ( ) ;
31+ }
2032 return ;
2133 }
2234 try {
@@ -40,6 +52,15 @@ function init(attempts: number = 0) {
4052 portal = { portalContainer }
4153 /> ,
4254 ) ;
55+
56+ // Try to catch if the button gets removed dynamically and reinitialize
57+ const observer = new MutationObserver ( debouncedInit ) ;
58+ observer . observe ( buttonTarget , {
59+ childList : true ,
60+ subtree : true ,
61+ } ) ;
62+ window . removeEventListener ( "focus" , debouncedInit ) ;
63+ window . addEventListener ( "focus" , debouncedInit ) ;
4364 } catch ( error ) {
4465 if ( error instanceof ENoIntegrationError ) {
4566 // Ignore, expected error when the site is not supported.
0 commit comments