CI / Build & Test (push) Successful in 2m52s
- Snapshot and restore latency badge values across innerHTML re-renders so badges never flash back to "…ms" on the 2-second state refresh cycle - Add CSS transition (color 0.4s ease) and in-flight opacity dim (.pinging) to .latency-badge for smooth color changes - Decouple latency pinging from the state refresh cycle — pings now run on their own independent interval instead of being triggered by refresh() - Add two new settings under a "Latency Ping" section in Settings: - Enable Latency Ping toggle (latencyPingEnabled, default: true) - Ping Interval in seconds (latencyPingIntervalMs, default: 5s) - Restart ping interval immediately on settings save so changes take effect without a page reload
65 lines
4.2 KiB
JavaScript
65 lines
4.2 KiB
JavaScript
/**
|
|
* Settings page — syncs the settings form with the persisted settings object
|
|
* and saves changes to the native host.
|
|
* Depends on: core/utils.js ($), core/state.js (settings, SETTINGS_DEFAULTS), ui/toast.js (showToast)
|
|
*/
|
|
|
|
/**
|
|
* Sync all settings form controls with the current `settings` object.
|
|
* Called on every refresh cycle to keep the UI in sync with persisted state.
|
|
*/
|
|
function updateSettingsUI() {
|
|
$('toggleNotify')?.classList.toggle('active', settings.notifyOnDisconnect === true);
|
|
$('toggleNotifyTunnelError')?.classList.toggle('active', settings.notifyOnTunnelError !== false);
|
|
$('toggleTunnelAutoReconnect')?.classList.toggle('active', settings.tunnelAutoReconnect === true);
|
|
$('toggleDebug')?.classList.toggle('active', settings.debug === true);
|
|
$('toggleDisableFileUrls')?.classList.toggle('active', settings.disableOnFileUrls === true);
|
|
$('toggleTheme')?.classList.toggle('active', document.documentElement.getAttribute('data-theme') === 'light');
|
|
$('toggleLatencyPing')?.classList.toggle('active', settings.latencyPingEnabled !== false);
|
|
const proxyPortEl = $('proxyPort');
|
|
const readyTimeoutMsEl = $('readyTimeoutMs');
|
|
const backupRetentionEl = $('backupRetention');
|
|
const backupIntervalEl = $('backupIntervalHours');
|
|
const latencyPingIntervalEl = $('latencyPingIntervalMs');
|
|
if (proxyPortEl) proxyPortEl.value = settings.proxyPort ?? SETTINGS_DEFAULTS.proxyPort;
|
|
if (readyTimeoutMsEl) readyTimeoutMsEl.value = settings.readyTimeoutMs ?? SETTINGS_DEFAULTS.readyTimeoutMs;
|
|
if (backupRetentionEl) backupRetentionEl.value = settings.backupRetention ?? SETTINGS_DEFAULTS.backupRetention;
|
|
if (backupIntervalEl) backupIntervalEl.value = settings.backupIntervalHours ?? SETTINGS_DEFAULTS.backupIntervalHours;
|
|
if (latencyPingIntervalEl) latencyPingIntervalEl.value = (settings.latencyPingIntervalMs ?? SETTINGS_DEFAULTS.latencyPingIntervalMs) / 1000;
|
|
}
|
|
|
|
function saveSettings() {
|
|
settings.notifyOnDisconnect = $('toggleNotify')?.classList.contains('active') ?? SETTINGS_DEFAULTS.notifyOnDisconnect;
|
|
settings.notifyOnTunnelError = $('toggleNotifyTunnelError')?.classList.contains('active') ?? SETTINGS_DEFAULTS.notifyOnTunnelError;
|
|
settings.tunnelAutoReconnect = $('toggleTunnelAutoReconnect')?.classList.contains('active') ?? SETTINGS_DEFAULTS.tunnelAutoReconnect;
|
|
settings.debug = $('toggleDebug')?.classList.contains('active') ?? SETTINGS_DEFAULTS.debug;
|
|
settings.disableOnFileUrls = $('toggleDisableFileUrls')?.classList.contains('active') ?? SETTINGS_DEFAULTS.disableOnFileUrls;
|
|
settings.latencyPingEnabled = $('toggleLatencyPing')?.classList.contains('active') ?? SETTINGS_DEFAULTS.latencyPingEnabled;
|
|
settings.proxyPort = parseInt($('proxyPort')?.value, 10) || SETTINGS_DEFAULTS.proxyPort;
|
|
settings.readyTimeoutMs = parseInt($('readyTimeoutMs')?.value, 10) || SETTINGS_DEFAULTS.readyTimeoutMs;
|
|
settings.backupRetention = Math.max(1, parseInt($('backupRetention')?.value, 10) || SETTINGS_DEFAULTS.backupRetention);
|
|
settings.backupIntervalHours = Math.max(0, parseInt($('backupIntervalHours')?.value, 10) || 0);
|
|
const pingSecs = parseFloat($('latencyPingIntervalMs')?.value);
|
|
settings.latencyPingIntervalMs = isNaN(pingSecs) || pingSecs <= 0
|
|
? SETTINGS_DEFAULTS.latencyPingIntervalMs
|
|
: Math.round(pingSecs * 1000);
|
|
chrome.runtime.sendMessage(
|
|
{ target: 'holesail-native', action: 'send', payload: { type: 'updateSettings', payload: { ...settings } } },
|
|
(response) => {
|
|
if (chrome.runtime.lastError) { showToast('Settings save failed: ' + chrome.runtime.lastError.message, 'error'); return; }
|
|
if (response && response.ok) {
|
|
if (response.settings) settings = { ...SETTINGS_DEFAULTS, ...response.settings };
|
|
// Restart the ping interval so the new interval / enabled state takes effect immediately
|
|
if (typeof window._restartPingInterval === 'function') window._restartPingInterval();
|
|
if (response.requiresRestart) {
|
|
showToast('Settings saved — restart the native host for proxy port changes to take effect', 'warning');
|
|
} else {
|
|
showToast('Settings saved', 'success');
|
|
}
|
|
} else {
|
|
showToast(response?.error || 'Failed to save settings', 'error');
|
|
}
|
|
}
|
|
);
|
|
}
|