forked from michalrus/anki-simple-cloze-overlapper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_cloze-overlapper-loader.js
80 lines (71 loc) · 2.85 KB
/
_cloze-overlapper-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(async () => {
'use strict';
const CLOZE_CONTAINER = /** @type {HTMLDivElement} */ (
document.getElementById('rendered-cloze'));
/** @param {any} e */
function showMessage(e) {
CLOZE_CONTAINER.textContent = e;
}
async function renderClozes(typesetMathJax = false) {
try {
const ClozeOverlapper = await import('./_cloze-overlapper.js');
await ClozeOverlapper.renderClozes(
{}, typesetMathJax ? ClozeOverlapper.typesetMathJax : undefined);
} catch (e) {
showMessage(e);
}
}
/** @typedef {Array<() => any>} HooksList */
/** @typedef {{ startup: { promise: Promise<void> }; }} MathJax */
/** @type {((r: 'anki/reviewer') => { onUpdateHook: HooksList; }) | undefined} */
const REQUIRE = /** @type {any} */ (globalThis).require;
/** @type {HooksList | undefined} */
const ON_UPDATE_HOOK = /** @type {any} */ (globalThis).onUpdateHook;
/** @returns {MathJax | undefined} */
function getMathJax() {
return /** @type {any} */ (globalThis).MathJax;
}
/** @return {HooksList | undefined} */
function getUpdateHook() {
// Anki Desktop has both require() and onUpdateHook.
// AnkiDroid has only onUpdateHook.
// AnkiWeb has neither.
// AnkiMobile – ???
if (REQUIRE) {
try {
return REQUIRE('anki/reviewer').onUpdateHook;
} catch (/** @type {any} */ e) {
if (!e?.message.includes('Cannot require')) {
throw e;
}
}
}
return ON_UPDATE_HOOK;
}
try {
const updateHook = getUpdateHook();
if (updateHook) {
// onUpdateHook guarantees the presence of a .card# class on the <body>.
// It also does a MathJax render, so we don't have to do one ourselves.
updateHook.push(renderClozes);
} else if (getMathJax()) {
// AnkiDroid loads MathJax only if \( and/or \[ are present.
// (Must not take this branch since AnkiDroid has onUpdateHook.)
// MathJax loading on AnkiWeb is kinda slow.
while (!getMathJax()?.startup?.promise) {
showMessage('Waiting for MathJax to initialise...');
await new Promise(resolve => setTimeout(resolve, 1000));
}
await /** @type {MathJax} */ (getMathJax()).startup.promise;
showMessage(null);
// Assume that waiting for MathJax to initialise is enough to get a .card# class
// on #qa_box on AnkiWeb (actually never saw #qa_box's class list change).
// Without onUpdateHook, MathJax must be re-typeset explicitly.
renderClozes(true);
} else {
renderClozes();
}
} catch (e) {
showMessage(e);
}
})();