Skip to content

Commit 71f3913

Browse files
committed
gemini opening solution
1 parent 612372f commit 71f3913

File tree

3 files changed

+84
-51
lines changed

3 files changed

+84
-51
lines changed

buttons-injection.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
const EXTENDED_CHECK_INTERVAL = 15000; // 15 seconds
1313
const EXTENDED_CHECK_DURATION = 2 * 60 * 60 * 1000; // 2 hours in milliseconds
1414

15+
// Flag to coordinate with the floating panel toggle logic.
16+
window.OneClickPrompts_isTogglingPanel = false;
17+
1518
/**
1619
* Checks whether the custom buttons modifications already exist in the DOM.
20+
* This is strengthened to check for child elements, not just the container.
1721
* @returns {boolean} - True if modifications exist, false otherwise.
1822
*/
1923
function doCustomModificationsExist() {
20-
return document.getElementById(window.InjectionTargetsOnWebsite.selectors.buttonsContainerId) !== null;
24+
const el = document.getElementById(window.InjectionTargetsOnWebsite.selectors.buttonsContainerId);
25+
// The container must exist AND have child elements (buttons/toggles) inside it.
26+
return !!(el && el.children.length > 0);
2127
}
2228

2329
/**
@@ -30,7 +36,7 @@ function doCustomModificationsExist() {
3036
*/
3137
function buttonBoxCheckingAndInjection(enableResiliency = true, activeWebsite) {
3238
logConCgp('[button-injection] Checking if mods already exist...');
33-
39+
3440
// If modifications already exist and resiliency is disabled, skip the injection.
3541
if (doCustomModificationsExist() && !enableResiliency) {
3642
logConCgp('[button-injection] Modifications already exist and resiliency is disabled. Skipping initialization.');
@@ -91,7 +97,7 @@ function commenceEnhancedResiliencyChecks() {
9197
clearTimeout(window.OneClickPropmts_currentResiliencyTimeout);
9298
window.OneClickPropmts_currentResiliencyTimeout = null;
9399
}
94-
100+
95101
if (window.OneClickPropmts_extendedMonitoringObserver) {
96102
window.OneClickPropmts_extendedMonitoringObserver.disconnect();
97103
window.OneClickPropmts_extendedMonitoringObserver = null;
@@ -107,6 +113,13 @@ function commenceEnhancedResiliencyChecks() {
107113
logConCgp('[button-injection] Beginning enhanced resiliency checks with dynamic interval...');
108114

109115
function adaptiveCheck() {
116+
// If the panel is being toggled, pause the watchdog to prevent race conditions.
117+
if (window.OneClickPrompts_isTogglingPanel) {
118+
logConCgp('[button-injection] Panel toggling in progress. Resiliency check paused.');
119+
window.OneClickPropmts_currentResiliencyTimeout = setTimeout(adaptiveCheck, 150);
120+
return;
121+
}
122+
110123
totalIterationsPerformed++;
111124
const modificationsExist = doCustomModificationsExist();
112125
let delay;
@@ -172,7 +185,8 @@ function startExtendedMonitoringWithObserver() {
172185

173186
// Create new observer
174187
window.OneClickPropmts_extendedMonitoringObserver = new MutationObserver((mutations) => {
175-
if (!doCustomModificationsExist()) {
188+
// Also check the toggle flag here to avoid reacting to our own changes.
189+
if (!window.OneClickPrompts_isTogglingPanel && !doCustomModificationsExist()) {
176190
logConCgp('[button-injection] Modifications missing during extended monitoring - reinserting');
177191
enforceResiliencyMeasures();
178192
}

floating-panel-ui-creation.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@
2222
* Creates the floating panel element by fetching an HTML template and appending it.
2323
*/
2424
window.MaxExtensionFloatingPanel.createFloatingPanel = async function () {
25-
if (this.panelElement) {
25+
// Check if the panel element exists and is still attached to the document.
26+
if (this.panelElement && document.body.contains(this.panelElement)) {
2627
return this.panelElement;
2728
}
2829

30+
// If the panel element reference exists but is not in the DOM, it's been detached.
31+
if (this.panelElement) {
32+
this.panelElement = null; // Reset reference to allow re-creation.
33+
logConCgp('[floating-panel] Panel element was detached from the DOM. It will be recreated.');
34+
}
35+
2936
try {
3037
const response = await fetch(chrome.runtime.getURL('floating-panel.html'));
3138
if (!response.ok) {

floating-panel-ui-interaction.js

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,71 @@
2121
* @param {Event} [event] - The click event that triggered the toggle.
2222
*/
2323
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;
3426

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;
4233
}
4334

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();
5038

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+
}
5847

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+
}
6762

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+
}
7071

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);
7789
}
7890
};
7991

0 commit comments

Comments
 (0)