Validate settings when getting its values

This commit is contained in:
redphx 2023-09-08 17:16:38 +07:00
parent 651402a6b4
commit cd7a7c92c7

View File

@ -1327,17 +1327,17 @@ class Preferences {
}, },
[Preferences.VIDEO_SATURATION]: { [Preferences.VIDEO_SATURATION]: {
'default': 100, 'default': 100,
'min': 0, 'min': 50,
'max': 150, 'max': 150,
}, },
[Preferences.VIDEO_CONTRAST]: { [Preferences.VIDEO_CONTRAST]: {
'default': 100, 'default': 100,
'min': 0, 'min': 50,
'max': 150, 'max': 150,
}, },
[Preferences.VIDEO_BRIGHTNESS]: { [Preferences.VIDEO_BRIGHTNESS]: {
'default': 100, 'default': 100,
'min': 0, 'min': 50,
'max': 150, 'max': 150,
}, },
[Preferences.AUDIO_MIC_ON_PLAYING]: { [Preferences.AUDIO_MIC_ON_PLAYING]: {
@ -1417,34 +1417,16 @@ class Preferences {
} }
} }
get(key, defaultValue=null) { #validateValue(key, value) {
if (typeof key === 'undefined') { const config = Preferences.SETTINGS[key];
debugger; if (!config) {
return;
}
// Return "default" for STREAM_TOUCH_CONTROLLER pref when the browser doesn't support touch
if (!HAS_TOUCH_SUPPORT && key === Preferences.STREAM_TOUCH_CONTROLLER) {
return 'default';
}
const value = this._prefs[key];
if (typeof value !== 'undefined' && value !== null && value !== '') {
return value; return value;
} }
if (defaultValue !== null) { if (typeof value === 'undefined' || value === null) {
return defaultValue; value = config.default;
} }
// Return default value
return Preferences.SETTINGS[key].default;
}
set(key, value) {
const config = Preferences.SETTINGS[key];
if (config) {
if ('min' in config) { if ('min' in config) {
value = Math.max(config.min, value); value = Math.max(config.min, value);
} }
@ -1467,8 +1449,30 @@ class Preferences {
value = config.default; value = config.default;
} }
} }
return value;
} }
get(key) {
if (typeof key === 'undefined') {
debugger;
return;
}
// Return "default" for STREAM_TOUCH_CONTROLLER pref when the browser doesn't support touch
if (!HAS_TOUCH_SUPPORT && key === Preferences.STREAM_TOUCH_CONTROLLER) {
return 'default';
}
let value = this._prefs[key];
value = this.#validateValue(key, value);
return value;
}
set(key, value) {
value = this.#validateValue(key, value);
this._prefs[key] = value; this._prefs[key] = value;
this._update_storage(); this._update_storage();
} }
@ -1649,8 +1653,8 @@ const PREFS = new Preferences();
function checkForUpdate() { function checkForUpdate() {
const CHECK_INTERVAL_SECONDS = 4 * 3600; // check every 4 hours const CHECK_INTERVAL_SECONDS = 4 * 3600; // check every 4 hours
const currentVersion = PREFS.get(Preferences.CURRENT_VERSION, ''); const currentVersion = PREFS.get(Preferences.CURRENT_VERSION);
const lastCheck = PREFS.get(Preferences.LAST_UPDATE_CHECK, 0); const lastCheck = PREFS.get(Preferences.LAST_UPDATE_CHECK);
const now = Math.round((+new Date) / 1000); const now = Math.round((+new Date) / 1000);
if (currentVersion === SCRIPT_VERSION && now - lastCheck < CHECK_INTERVAL_SECONDS) { if (currentVersion === SCRIPT_VERSION && now - lastCheck < CHECK_INTERVAL_SECONDS) {
@ -2669,7 +2673,7 @@ function injectSettingsButton($parent) {
const CE = createElement; const CE = createElement;
const PREF_PREFERRED_REGION = getPreferredServerRegion(); const PREF_PREFERRED_REGION = getPreferredServerRegion();
const PREF_LATEST_VERSION = PREFS.get(Preferences.LATEST_VERSION, null); const PREF_LATEST_VERSION = PREFS.get(Preferences.LATEST_VERSION);
// Setup Settings button // Setup Settings button
const $button = CE('button', {'class': 'better-xcloud-settings-button'}, PREF_PREFERRED_REGION); const $button = CE('button', {'class': 'better-xcloud-settings-button'}, PREF_PREFERRED_REGION);