-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (43 loc) · 1.6 KB
/
index.js
File metadata and controls
59 lines (43 loc) · 1.6 KB
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
import AnkiExport from 'anki-apkg-export';
import { saveAs } from 'file-saver';
import initSqlJs from 'sql.js';
(async () => {
const sql = await initSqlJs();
document.querySelector('#buttonSimple').addEventListener('click', () => {
handleSimple(sql);
});
document.querySelector('#buttonAjax').addEventListener('click', () => {
handleAjax(sql);
});
document.querySelector('input').addEventListener('change', e => {
handleInput(sql, e.target.files[0]);
});
})();
async function handleSimple(sql) {
const apkg = new AnkiExport('deck-name', {}, sql);
apkg.addCard('card #1 front', 'card #1 back');
apkg.addCard('card #2 front', 'card #2 back');
const zip = await apkg.save();
saveAs(zip, 'output.apkg');
}
async function handleAjax(sql) {
const apkg = new AnkiExport('deck-name-ajax', {}, sql);
const response = await fetch('https://raw.githubusercontent.com/ewnd9/anki-apkg-export/39ebdd664ab23b5237eee95b7dd88c457e263a20/example/assets/anki.png');
const myBlob = await response.blob();
apkg.addMedia('anki.png', myBlob);
apkg.addCard('card #1 with image <img src="anki.png" />', 'card #1 back');
const zip = await apkg.save()
saveAs(zip, 'output.apkg');
}
async function handleInput(sql, file) {
const apkg = new AnkiExport('deck-name', {}, sql);
const reader = new FileReader();
reader.onload = async e => {
const file = e.target.result;
apkg.addMedia('anki.png', file);
apkg.addCard('card #1 with image <img src="anki.png" />', 'card #1 back');
const zip = await apkg.save()
saveAs(zip, 'output.apkg');
};
reader.readAsArrayBuffer(file);
}