JS store hint revealed state in local storage?

With the add-on Edit Field During Review (Cloze) you can edit a field during review using {{edit:field-1}} in the template. This works with hint fields as well with {{edit:hint:field-1}}.

However if you edit any field, all other revelead hints reverts back to hidden once you click out of the editing. I’m guessing this is because making edits causes a page reload of sorts.

I’m sure this can be fixed with some JavaScript code though. Does anyone know how to?

I asked AI and it suggested to add an event listener to check if the hint has been revealed, and then store the revealed state in the local storage. Is this the way or no?

Appreciate any help!

AI suggested solution:

To prevent revealed hint fields from being reset back to hidden on page reload in Anki during review, you can use the following JavaScript code:

// Check if the hint field is revealed and store its state in local storage
document.addEventListener('DOMContentLoaded', function() {
    const hintField = document.querySelector('.hint-field-selector'); // Replace with the actual selector for the hint field
    if (hintField.classList.contains('revealed')) {
        localStorage.setItem('hintRevealed', 'true');
    } else {
        localStorage.setItem('hintRevealed', 'false');
    }
});

// Retrieve the hint field state from local storage and set its visibility accordingly
document.addEventListener('DOMContentLoaded', function() {
    const hintField = document.querySelector('.hint-field-selector'); // Replace with the actual selector for the hint field
    const hintRevealed = localStorage.getItem('hintRevealed');
    if (hintRevealed === 'true') {
        hintField.classList.add('revealed');
    }
});

Replace '.hint-field-selector' with the actual CSS selector for the hint field. This code will store the state of the hint field in the browser’s local storage and set its visibility based on the stored state during page reload.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.