Created
February 23, 2017 11:31
-
-
Save PaperclipBadger/af3c3262ef1142804f9e9f18ae472c7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.