Use localStorage for storing stettings #689
Replies: 2 comments 1 reply
-
Thanks for the suggestion, I'll look into that. A couple initial thoughts though: Since the majority of config settings are only relevant to the back end, I'm curious how localstorage would reliably communicate these settings to the server in the event that the user's cookies are cleared and they attempt to start a new search with a new session. I suppose it would be possible to encode it into query params on a per search basis, but that wouldn't work for things like the OpenSearch template, which is generated server side. The server does already support naming and loading custom configurations though, so there could be a solution there. I was thinking that as long as the user saves their config in the same session that they perform the initial load of the OpenSearch template, the server could fill in a simple |
Beta Was this translation helpful? Give feedback.
-
as a quick example of what I mean, something like so for cookie replacement: let settings = {
// default settings object
aSetting: true,
theme: 'darkMode',
myCookie: 'test1=Hello; SameSite=None; Secure',
};
// use to change a setting
function changeSetting(key, value) {
settings[key] = value;
try {
localStorage.setItem('mySettings', JSON.stringify(settings))
} catch (error) {
console.error(`Unable to save settings after changing ${key} to ${value}`, error);
}
}
// to be called on page loaded
function setup() {
try {
// try to retrieve saved settings from localStorage
settings = JSON.parse(localStorage.getItem('mySettings'));
} catch (error) {
// either nothing saved because new user or localStorage deleted
// console.log(error) // or do nothing?
}
// check for old cookie
const cookies = document.cookie;
const myCookieIsThere = cookies.includes('test1='); // boolean
// if no cookie, add it
if (!myCookieIsThere) {
document.cookie = settings.myCookie;
// uncomment to force reload
// window.location.reload(true);
}
}
window.addEventListener('DOMContentLoaded', setup); |
Beta Was this translation helpful? Give feedback.
-
Hi @benbusby
I heard you on Opt Out talking about looking for another way to store user config. I would suggest localStorage as a better alternative that will persist after a user's web extension clears their cookies. It is a simple key value store, where both keys and values are strings but I have used it in the past to store stringified JSON which can be parsed into an object on page load and user settings applied based on the object. Or better, make it a Class that will save itself on any change.
You can also use JS to encode it as query parameters before changing Whoogle instance with farside. I would not suggest using the URL query params for and kind of persistent storage. There are already privacy extensions that will remove them if they are interpreted as tracking or privacy leaking.
Of course this is no help to people who disable JavaScript in their browser, but I guess they will have persistent storage issues anyway.
Beta Was this translation helpful? Give feedback.
All reactions