|
21 | 21 | * @param {Event} [event] - The click event that triggered the toggle. |
22 | 22 | */ |
23 | 23 | window.MaxExtensionFloatingPanel.togglePanel = async function (event) { |
24 | | - // Ensure panel DOM structure is available, creating it if it's the first time. |
25 | | - await this.createFloatingPanel(); |
26 | | - if (!this.panelElement) { |
27 | | - logConCgp('[floating-panel] Panel creation failed, aborting toggle.'); |
28 | | - return; |
29 | | - } |
30 | | - |
31 | | - this.isPanelVisible = !this.isPanelVisible; |
32 | | - this.currentPanelSettings.isVisible = this.isPanelVisible; |
33 | | - this.debouncedSavePanelSettings(); |
| 24 | + // Suppress the resiliency watchdog while we perform this intentional DOM surgery. |
| 25 | + window.OneClickPrompts_isTogglingPanel = true; |
34 | 26 |
|
35 | | - if (this.isPanelVisible) { |
36 | | - logConCgp('[floating-panel] Toggling panel ON. Re-creating buttons in panel.'); |
37 | | - // 1. Destroy the inline buttons. |
38 | | - const originalContainer = document.getElementById(window.InjectionTargetsOnWebsite.selectors.buttonsContainerId); |
39 | | - if (originalContainer) { |
40 | | - originalContainer.innerHTML = ''; |
41 | | - logConCgp('[floating-panel] Destroyed inline buttons.'); |
| 27 | + try { |
| 28 | + // Ensure panel DOM structure is available, creating it if it's the first time. |
| 29 | + await this.createFloatingPanel(); |
| 30 | + if (!this.panelElement) { |
| 31 | + logConCgp('[floating-panel] Panel creation failed, aborting toggle.'); |
| 32 | + return; |
42 | 33 | } |
43 | 34 |
|
44 | | - // 2. Create buttons directly in the panel. |
45 | | - const panelContent = document.getElementById('max-extension-floating-panel-content'); |
46 | | - if (panelContent) { |
47 | | - panelContent.innerHTML = ''; // Ensure it's clean before creating. |
48 | | - window.MaxExtensionButtonsInit.createAndInsertCustomElements(panelContent); |
49 | | - } |
| 35 | + this.isPanelVisible = !this.isPanelVisible; |
| 36 | + this.currentPanelSettings.isVisible = this.isPanelVisible; |
| 37 | + this.debouncedSavePanelSettings(); |
50 | 38 |
|
51 | | - // 3. Show and position the panel. |
52 | | - this.panelElement.style.display = 'flex'; |
53 | | - if (event) { |
54 | | - this.positionPanelAtCursor(event); |
55 | | - } else { |
56 | | - this.updatePanelFromSettings(); |
57 | | - } |
| 39 | + if (this.isPanelVisible) { |
| 40 | + logConCgp('[floating-panel] Toggling panel ON. Re-creating buttons in panel.'); |
| 41 | + // 1. Destroy the inline buttons container. This prevents resiliency checks from finding an empty container. |
| 42 | + const originalContainer = document.getElementById(window.InjectionTargetsOnWebsite.selectors.buttonsContainerId); |
| 43 | + if (originalContainer) { |
| 44 | + originalContainer.remove(); |
| 45 | + logConCgp('[floating-panel] Destroyed inline buttons container.'); |
| 46 | + } |
58 | 47 |
|
59 | | - } else { |
60 | | - logConCgp('[floating-panel] Toggling panel OFF. Re-creating buttons inline.'); |
61 | | - // 1. Destroy buttons inside the panel. |
62 | | - const panelContent = document.getElementById('max-extension-floating-panel-content'); |
63 | | - if (panelContent) { |
64 | | - panelContent.innerHTML = ''; |
65 | | - logConCgp('[floating-panel] Destroyed panel buttons.'); |
66 | | - } |
| 48 | + // 2. Create buttons directly in the panel. |
| 49 | + const panelContent = document.getElementById('max-extension-floating-panel-content'); |
| 50 | + if (panelContent) { |
| 51 | + panelContent.innerHTML = ''; // Ensure it's clean before creating. |
| 52 | + window.MaxExtensionButtonsInit.createAndInsertCustomElements(panelContent); |
| 53 | + } |
| 54 | + |
| 55 | + // 3. Show and position the panel. |
| 56 | + this.panelElement.style.display = 'flex'; |
| 57 | + if (event) { |
| 58 | + this.positionPanelAtCursor(event); |
| 59 | + } else { |
| 60 | + this.updatePanelFromSettings(); |
| 61 | + } |
67 | 62 |
|
68 | | - // 2. Hide the panel. |
69 | | - this.panelElement.style.display = 'none'; |
| 63 | + } else { |
| 64 | + logConCgp('[floating-panel] Toggling panel OFF. Re-creating buttons inline.'); |
| 65 | + // 1. Destroy buttons inside the panel. |
| 66 | + const panelContent = document.getElementById('max-extension-floating-panel-content'); |
| 67 | + if (panelContent) { |
| 68 | + panelContent.innerHTML = ''; |
| 69 | + logConCgp('[floating-panel] Destroyed panel buttons.'); |
| 70 | + } |
70 | 71 |
|
71 | | - // 3. Re-create the buttons in their original inline location. |
72 | | - const selectors = window.InjectionTargetsOnWebsite.selectors.containers; |
73 | | - MaxExtensionUtils.waitForElements(selectors, (targetDiv) => { |
74 | | - logConCgp('[floating-panel] Found inline target. Injecting buttons.'); |
75 | | - window.MaxExtensionButtonsInit.createAndInsertCustomElements(targetDiv); |
76 | | - }); |
| 72 | + // 2. Hide the panel. |
| 73 | + this.panelElement.style.display = 'none'; |
| 74 | + |
| 75 | + // 3. Perform a fast, direct re-creation of buttons in their original inline location. |
| 76 | + // The flag we set prevents the watchdog from interfering with this. |
| 77 | + const selectors = window.InjectionTargetsOnWebsite.selectors.containers; |
| 78 | + MaxExtensionUtils.waitForElements(selectors, (targetDiv) => { |
| 79 | + logConCgp('[floating-panel] Found inline target. Injecting buttons directly.'); |
| 80 | + window.MaxExtensionButtonsInit.createAndInsertCustomElements(targetDiv); |
| 81 | + }); |
| 82 | + } |
| 83 | + } finally { |
| 84 | + // Re-enable the watchdog after a short delay to allow the DOM to settle. |
| 85 | + setTimeout(() => { |
| 86 | + window.OneClickPrompts_isTogglingPanel = false; |
| 87 | + logConCgp('[button-injection] Panel toggling complete. Resiliency check resumed.'); |
| 88 | + }, 150); |
77 | 89 | } |
78 | 90 | }; |
79 | 91 |
|
|
0 commit comments