I’m trying to create a slideshow-style image carousel that remembers the last slide the user navigated to.
I’ve been able to implement this really well within a continuous anki instance on desktop using localstorage paired with noteids. Unfortunately, with the dynamic port assignment on anki startup, localstorage does not persist after anki desktop restarts. I know of no way to persist data after restart without an add-on.
I’m developing an addon with the goal of saving specific localstorage key/value pairs as a static array in a .js file in the media folder of an anki instance. The benefit of this is that it will sync across devices, with ankihub, and the .js file can be called in the note template so that ankidroid/mobile can at least read the relevant key/value pairs into their localstorage. This should create a read-only way to specify the starting slide of the slideshow, with write capacity in desktops with the addon installed.
Unfortunately, I’ve run into a problem with my code in retrieving the localstorage values from python.
I started out with mw.web.eval(“Object.entries(localStorage)”) which returned no values in the debugger or the addon.
I then verified that the key/value pairs exist with this javascript added to my notetypes:
var text = '';
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var value = localStorage.getItem(key);
text += key + ' : ' + value + '<br>';
}
After confirming they exist, I attempted to just call this javascript through python:
mw.web.evalWithCallback("""
var text = '';
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var value = localStorage.getItem(key);
text += key + ' : ' + value + '<br>';
}
text;
""", save_key_value_pairs)
I ran into some problems here, and the localstorage string was showing up as null again, so I ran the following in the debug console:
js = """
var text = '';
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var value = localStorage.getItem(key);
text += key + ' : ' + value + '<br>';
}
text;
"""
resultvar = mw.web.eval(js)
print(resultvar)
Sure enough, still returning as “None”. I went back into the cards and the javascript is still there correctly returning the storage. Is this something to do with the webview I am loading from? I’m using these hooks:
addHook("profileLoaded", load_key_value_pairs)
addHook("unloadProfile", save_key_value_pairs)
What am I doing wrong/not understanding? I verified syntax with other similar addons, but still can’t figure it out.