ref: b5367ed18ac96f54595fbe6c17fe8a4a897020aa
parent: 85b00e56a034713a8e34f9e10423ae14dbc810e0
author: Ben Harris <bjh21@bjh21.me.uk>
date: Mon Aug 21 18:45:48 EDT 2023
js: load preferences from HTML elements It will be useful on KaiOS to be able to specify default user preferences that aren't the standard ones, in the same way that we specify some environment variables. As with environment variables, we can now do this be embedding a <script> element in the HTML like this: <script class="preferences" type="text/plain"> show-labels=true </script> These are loaded before the preferences from localStorage, so they just set defaults and can be overridden by the user (but not on KaiOS yet, because we still don't have dialogue boxes there).
--- a/emcclib.js
+++ b/emcclib.js
@@ -821,9 +821,7 @@
* pass it back in as a string, via prefs_load_callback.
*/
js_load_prefs: function(me) {
- try {
- var prefsdata =
- localStorage.getItem(location.pathname + " preferences");
+ function load_prefs_from_string(prefsdata) {
if (prefsdata !== undefined && prefsdata !== null) {
var lenbytes = lengthBytesUTF8(prefsdata) + 1;
var dest = _malloc(lenbytes);
@@ -833,6 +831,15 @@
_free(dest);
}
}
+ }
+ // Load any default preferences embedded in HTML.
+ for (var prefsscript of
+ document.querySelectorAll("script.preferences"))
+ load_prefs_from_string(prefsscript.textContent);
+ // Load saved preferences if they exist.
+ try {
+ load_prefs_from_string(
+ localStorage.getItem(location.pathname + " preferences"));
} catch (error) {
// Log the error but otherwise pretend the settings were
// absent.