Skip to content

Commit 0b90919

Browse files
committed
Floating panel changed to use Chrome local storage, and also reset button has been added.
1 parent 9b6b123 commit 0b90919

File tree

5 files changed

+204
-17
lines changed

5 files changed

+204
-17
lines changed

config.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,82 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
416416
}
417417
})();
418418
return true;
419+
case 'resetAdvancedSelectors':
420+
clearAdvancedSelectors().then(result => {
421+
sendResponse({ success: result });
422+
logConfigurationRelatedStuff('Reset advanced selectors');
423+
}).catch(error => {
424+
handleStorageError(error);
425+
sendResponse({ error: error.message });
426+
});
427+
return true;
419428
// ----- End Custom Selectors Cases -----
429+
430+
// ----- Floating Panel Settings Cases -----
431+
case 'getFloatingPanelSettings':
432+
if (!request.hostname) {
433+
sendResponse({ error: 'Hostname is required' });
434+
return true;
435+
}
436+
437+
const floatingPanelKey = 'floating_panel_' + request.hostname;
438+
chrome.storage.local.get([floatingPanelKey], result => {
439+
if (result && result[floatingPanelKey]) {
440+
sendResponse({ settings: result[floatingPanelKey] });
441+
logConfigurationRelatedStuff(`Retrieved floating panel settings for ${request.hostname}`);
442+
} else {
443+
sendResponse({ settings: null });
444+
logConfigurationRelatedStuff(`No saved floating panel settings for ${request.hostname}`);
445+
}
446+
});
447+
return true;
448+
449+
case 'saveFloatingPanelSettings':
450+
if (!request.hostname || !request.settings) {
451+
sendResponse({ error: 'Hostname and settings are required' });
452+
return true;
453+
}
454+
455+
const saveKey = 'floating_panel_' + request.hostname;
456+
const saveData = {};
457+
saveData[saveKey] = request.settings;
458+
459+
chrome.storage.local.set(saveData, () => {
460+
if (chrome.runtime.lastError) {
461+
handleStorageError(chrome.runtime.lastError);
462+
sendResponse({ success: false, error: chrome.runtime.lastError.message });
463+
} else {
464+
sendResponse({ success: true });
465+
logConfigurationRelatedStuff(`Saved floating panel settings for ${request.hostname}`);
466+
}
467+
});
468+
return true;
469+
470+
case 'resetFloatingPanelSettings':
471+
chrome.storage.local.get(null, result => {
472+
const floatingPanelKeys = Object.keys(result).filter(key =>
473+
key.startsWith('floating_panel_')
474+
);
475+
476+
if (floatingPanelKeys.length === 0) {
477+
sendResponse({ success: true, count: 0 });
478+
logConfigurationRelatedStuff('No floating panel settings found to reset');
479+
return;
480+
}
481+
482+
chrome.storage.local.remove(floatingPanelKeys, () => {
483+
if (chrome.runtime.lastError) {
484+
handleStorageError(chrome.runtime.lastError);
485+
sendResponse({ success: false, error: chrome.runtime.lastError.message });
486+
} else {
487+
sendResponse({ success: true, count: floatingPanelKeys.length });
488+
logConfigurationRelatedStuff(`Reset ${floatingPanelKeys.length} floating panel settings`);
489+
}
490+
});
491+
});
492+
return true;
493+
// ----- End Floating Panel Settings Cases -----
494+
420495
default:
421496
logConfigurationRelatedStuff('Unknown message type received:', request.type);
422497
sendResponse({ error: 'Unknown message type' });

floating-panel.js

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ window.MaxExtensionFloatingPanel = {
527527

528528
// Clear existing content in the panel
529529
panelContent.innerHTML = '';
530-
530+
531531
// Get all the button configurations from global config
532532
const buttonConfigs = globalMaxExtensionConfig.customButtons;
533533
let configIndex = 0; // Track which config we're on
@@ -619,41 +619,96 @@ window.MaxExtensionFloatingPanel = {
619619
},
620620

621621
/**
622-
* Loads panel settings from localStorage.
622+
* Loads panel settings from Chrome's storage through the config service worker.
623623
* If no settings are found, default settings are used.
624624
*/
625625
loadPanelSettings: function() {
626626
try {
627+
// Initialize with default settings immediately to avoid null references
628+
this.currentPanelSettings = { ...this.defaultPanelSettings };
629+
627630
const hostname = window.location.hostname;
628-
const storageKey = this.storageKeyPrefix + hostname;
629-
const savedSettings = localStorage.getItem(storageKey);
630631

631-
if (savedSettings) {
632-
this.currentPanelSettings = JSON.parse(savedSettings);
633-
logConCgp('[floating-panel] Loaded panel settings for ' + hostname);
634-
} else {
635-
this.currentPanelSettings = { ...this.defaultPanelSettings };
636-
logConCgp('[floating-panel] Using default panel settings for ' + hostname);
637-
}
632+
// Use chrome.runtime.sendMessage to get settings from config service worker
633+
chrome.runtime.sendMessage({
634+
type: 'getFloatingPanelSettings',
635+
hostname: hostname
636+
}, (response) => {
637+
if (response && response.settings) {
638+
this.currentPanelSettings = response.settings;
639+
logConCgp('[floating-panel] Loaded panel settings for ' + hostname);
640+
} else {
641+
// Keep the default settings we already set
642+
logConCgp('[floating-panel] Using default panel settings for ' + hostname);
643+
}
644+
645+
// Apply settings to panel if it exists
646+
if (this.panelElement) {
647+
this.updatePanelFromSettings();
648+
}
649+
650+
// Restore panel visibility state after settings are loaded
651+
this.restorePanelState();
652+
});
638653
} catch (error) {
639654
logConCgp('[floating-panel] Error loading panel settings: ' + error.message);
640655
this.currentPanelSettings = { ...this.defaultPanelSettings };
641656
}
642657
},
643658

644659
/**
645-
* Saves panel settings to localStorage.
660+
* Saves panel settings to Chrome's storage through the config service worker.
646661
*/
647662
savePanelSettings: function() {
648663
try {
649664
const hostname = window.location.hostname;
650-
const storageKey = this.storageKeyPrefix + hostname;
651-
localStorage.setItem(storageKey, JSON.stringify(this.currentPanelSettings));
652-
logConCgp('[floating-panel] Saved panel settings for ' + hostname);
665+
666+
// Use chrome.runtime.sendMessage to save settings via config service worker
667+
chrome.runtime.sendMessage({
668+
type: 'saveFloatingPanelSettings',
669+
hostname: hostname,
670+
settings: this.currentPanelSettings
671+
}, (response) => {
672+
if (response && response.success) {
673+
logConCgp('[floating-panel] Saved panel settings for ' + hostname);
674+
} else {
675+
logConCgp('[floating-panel] Failed to save panel settings for ' + hostname);
676+
}
677+
});
653678
} catch (error) {
654679
logConCgp('[floating-panel] Error saving panel settings: ' + error.message);
655680
}
656681
},
682+
683+
/**
684+
* Updates the panel's appearance and position based on current settings.
685+
* Called after settings are loaded or changed.
686+
*/
687+
updatePanelFromSettings: function() {
688+
if (!this.panelElement) return;
689+
690+
this.panelElement.style.width = `${this.currentPanelSettings.width}px`;
691+
this.panelElement.style.height = `${this.currentPanelSettings.height}px`;
692+
this.panelElement.style.left = `${this.currentPanelSettings.posX}px`;
693+
this.panelElement.style.top = `${this.currentPanelSettings.posY}px`;
694+
695+
// Update opacity
696+
const bgOpacity = this.currentPanelSettings.opacity;
697+
this.panelElement.style.backgroundColor = `rgba(50, 50, 50, ${bgOpacity})`;
698+
699+
// Update header and footer opacity
700+
const headerFooterOpacity = bgOpacity + 0.1;
701+
const header = document.getElementById('max-extension-floating-panel-header');
702+
const footer = document.getElementById('max-extension-profile-switcher');
703+
704+
if (header) {
705+
header.style.backgroundColor = `rgba(40, 40, 40, ${headerFooterOpacity})`;
706+
}
707+
708+
if (footer) {
709+
footer.style.backgroundColor = `rgba(40, 40, 40, ${headerFooterOpacity})`;
710+
}
711+
},
657712

658713
/**
659714
* Restores the panel state based on saved settings.
@@ -671,9 +726,14 @@ window.MaxExtensionFloatingPanel = {
671726
* This method should be called when the extension is initialized.
672727
*/
673728
initialize: function() {
674-
this.loadPanelSettings();
729+
// First, set default settings to avoid null reference
730+
this.currentPanelSettings = { ...this.defaultPanelSettings };
731+
732+
// Create the panel with default settings initially
675733
this.createFloatingPanel();
676-
this.restorePanelState();
734+
735+
// Load settings and update the panel once settings are available
736+
this.loadPanelSettings();
677737

678738
// Load available profiles and initialize the profile switcher
679739
this.loadAvailableProfiles();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// popup-page-floating-window-handler.js
2+
// Version: 1.0
3+
// Handler script for floating window settings reset functionality
4+
5+
'use strict';
6+
7+
/**
8+
* Resets all floating panel settings across all websites.
9+
* Uses the config.js service worker to clear all floating panel settings
10+
* stored in Chrome's extension storage with the 'floating_panel_' prefix.
11+
*/
12+
async function resetFloatingWindowSettings() {
13+
try {
14+
// Send message to config service worker to reset all floating panel settings
15+
chrome.runtime.sendMessage({
16+
type: 'resetFloatingPanelSettings'
17+
}, (response) => {
18+
if (response && response.success) {
19+
// Log success and show confirmation to user
20+
const count = response.count || 0;
21+
22+
if (count > 0) {
23+
window.logToGUIConsole(`Successfully reset ${count} floating panel settings`);
24+
window.showToast('Floating window settings have been reset. Changes will take effect on your next page visit.', 'success');
25+
} else {
26+
window.logToGUIConsole('No floating panel settings found to reset');
27+
window.showToast('No floating panel settings found to reset.', 'info');
28+
}
29+
} else {
30+
// Log error and show error message
31+
const errorMsg = response && response.error ? response.error : 'Unknown error';
32+
window.logToGUIConsole(`Error resetting floating window settings: ${errorMsg}`);
33+
window.showToast(`Failed to reset floating window settings: ${errorMsg}`, 'error');
34+
}
35+
});
36+
} catch (error) {
37+
// Log error and show error message
38+
window.logToGUIConsole(`Error resetting floating window settings: ${error.message}`);
39+
window.showToast(`Failed to reset floating window settings: ${error.message}`, 'error');
40+
}
41+
}
42+
43+
// Export the function to be used by the popup script
44+
window.resetFloatingWindowSettings = resetFloatingWindowSettings;

popup-page-scripts/popup-page-script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ document.addEventListener('DOMContentLoaded', () => {
267267
document.getElementById('autoSendToggle').addEventListener('change', updateGlobalSettings);
268268
document.getElementById('shortcutsToggle').addEventListener('change', updateGlobalSettings);
269269
document.getElementById('revertDefault').addEventListener('click', revertToDefault);
270+
271+
// Floating Window Settings
272+
document.getElementById('resetFloatingWindowSettings').addEventListener('click', resetFloatingWindowSettings);
270273

271274
// Drag and drop events - attach them but implementation is in customButtons.js
272275
buttonCardsList.addEventListener('dragstart', handleDragStart);

popup.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ <h2>Settings</h2>
9898
<div class="row">
9999
<button id="revertDefault" class="danger">Revert Current Profile to Default</button>
100100
</div>
101+
<div class="row">
102+
<button id="resetFloatingWindowSettings">Reset Floating Window Settings for all pages</button>
103+
</div>
101104
</section>
102105

103106

@@ -257,6 +260,8 @@ <h2>Theme</h2>
257260
<script src="/popup-page-scripts/popup-page-theme.js"></script>
258261
<!-- Advanced Selector Configuration script -->
259262
<script src="/popup-page-scripts/popup-page-advanced.js"></script>
263+
<!-- Floating Window Settings Handler script -->
264+
<script src="/popup-page-scripts/popup-page-floating-window-handler.js"></script>
260265
</body>
261266

262267
</html>

0 commit comments

Comments
 (0)