Skip to content

Instantly share code, notes, and snippets.

@PaperclipBadger
Created February 23, 2017 11:31
Show Gist options
  • Save PaperclipBadger/af3c3262ef1142804f9e9f18ae472c7e to your computer and use it in GitHub Desktop.
Save PaperclipBadger/af3c3262ef1142804f9e9f18ae472c7e to your computer and use it in GitHub Desktop.
function DummyStore() {
var instance;
return instance =
{ store: {}
, setItem: function (key, val) {
instance.store[key] = val;
return val;
}
, getItem: function (key, def) {
var val = instance.store[key];
return val == null ? def : val;
}
, tryItem: function (key, val) {
var curr = instance.getItem(key, undefined);
return curr == null ? instance.setItem(key, val) : curr;
}
, clear: function () {
this.store = {};
}
};
}
function PyStore(py) {
py.data = py.data || {};
var instance;
return instance =
{ setItem: function (key, val) {
py.data[key] = val;
return val;
}
, getItem: function (key, def) {
var val = py.data[key];
return val == null ? def : val;
}
, tryItem: function (key, val) {
var curr = instance.getItem(key, undefined);
return curr == null ? instance.setItem(key, val) : curr;
}
, clear: function () {
py.data = {};
}
};
}
function SessionStore(sessionStorage) {
var instance;
return instance =
{ setItem: function (key, val) {
sessionStorage.setItem(key, val);
return val;
}
, getItem: function (key, def) {
var val = sessionStorage.getItem(key);
return val == null ? def : val;
}
, tryItem: function (key, val) {
var curr = instance.getItem(key, undefined);
return curr == null ? instance.setItem(key, val) : curr;
}
, clear: function () {
sessionStorage.clear();
}
};
}
function persist(cb) {
window.setTimeout(function() {
// Determine whether to use Anki's Bridge object (Desktop) or sessionStorage (AnkiDroid) to store data across sides.
store = typeof(py) !== "undefined"
? PyStore(py)
: typeof(sessionStorage) !== "undefined"
? SessionStore(sessionStorage)
: DummyStore();
if (!document.getElementById('answer')) {
store.clear()
}
cb(store);
}, 0); //Execute after Anki has loaded its Bridge object.
}
function run(store) {
elements = document.getElementsByClassName('choice');
while (elements.length > 0) {
choices = eval(elements[0].innerText);
rand = store.tryItem('rand' + choices[0], Math.random());
choice = choices.slice(1)[Math.floor(rand * (choices.length - 1))];
if (typeof(choice) === "string") {
elements[0].insertAdjacentText('afterEnd', choice);
} else {
html = '<div class="choice">'+JSON.stringify(choice)+'</div>';
elements[0].insertAdjacentHTML('afterEnd', html);
}
var parent = elements[0].parentElement;
parent.removeChild(elements[0]);
elements = document.getElementsByClassName('choice');
}
}
persist(run);
@edjane-stewart
Copy link

Thanks for sharing this code. It helped me solve the problem along with the code yingtongli.me created. Now my anki cards do what I wanted them to do, randomize choices for quiz questions.

@yitzhakbg
Copy link

Doesn't work with newer editions of Anki. See replacement using Simon Lammer's Pesistence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment