switch to new ankiconnect interface

This commit is contained in:
Alex Yatskov 2017-02-19 10:09:15 -08:00
parent 1a52a2d8d5
commit 64db42d536
4 changed files with 68 additions and 33 deletions

View File

@ -20,7 +20,7 @@ class AnkiConnect {
constructor(server) { constructor(server) {
this.server = server; this.server = server;
this.asyncPools = {}; this.asyncPools = {};
this.localVersion = 1; this.localVersion = 2;
this.remoteVersion = null; this.remoteVersion = null;
} }

View File

@ -100,9 +100,11 @@ class Yomichan {
note.deckName = this.options.anki.terms.deck; note.deckName = this.options.anki.terms.deck;
note.modelName = this.options.anki.terms.model; note.modelName = this.options.anki.terms.model;
if (definition.audio) {
const audio = { const audio = {
kanji: definition.expression, url: definition.audio.url,
kana: definition.reading, filename: definition.audio.filename,
skipHash: '7e2c2f954ef6051373ba916f000168dc',
fields: [] fields: []
}; };
@ -116,6 +118,7 @@ class Yomichan {
note.audio = audio; note.audio = audio;
} }
} }
}
for (const name in fields) { for (const name in fields) {
note.fields[name] = formatField( note.fields[name] = formatField(

View File

@ -113,7 +113,16 @@ class Frame {
const index = link.data('index'); const index = link.data('index');
const mode = link.data('mode'); const mode = link.data('mode');
addDefinition(this.definitions[index], mode).then(success => { const definition = this.definitions[index];
if (mode !== 'kanji') {
const url = buildAudioUrl(definition);
const filename = buildAudioFilename(definition);
if (url && filename) {
definition.audio = {url, filename};
}
}
addDefinition(definition, mode).then(success => {
if (success) { if (success) {
const button = this.findAddNoteButton(index, mode); const button = this.findAddNoteButton(index, mode);
button.addClass('disabled'); button.addClass('disabled');
@ -171,28 +180,11 @@ class Frame {
} }
} }
let kana = definition.reading; const url = buildAudioUrl(definition);
let kanji = definition.expression; if (!url) {
if (!kana) {
if (!kanji) {
return; return;
} }
if (wanakana.isHiragana(kanji)) {
kana = kanji;
kanji = null;
}
}
const params = [];
if (kanji) {
params.push(`kanji=${encodeURIComponent(kanji)}`);
}
if (kana) {
params.push(`kana=${encodeURIComponent(kana)}`);
}
const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
let audio = this.audioCache[url]; let audio = this.audioCache[url];
if (audio) { if (audio) {
audio.currentTime = 0; audio.currentTime = 0;

View File

@ -166,3 +166,43 @@ function extractSentence(source, extent) {
return content.substring(startPos, endPos).trim(); return content.substring(startPos, endPos).trim();
} }
function buildAudioUrl(definition) {
let kana = definition.reading;
let kanji = definition.expression;
if (!kana && !kanji) {
return null;
}
if (!kana && wanakana.isHiragana(kanji)) {
kana = kanji;
kanji = null;
}
const params = [];
if (kanji) {
params.push(`kanji=${encodeURIComponent(kanji)}`);
}
if (kana) {
params.push(`kana=${encodeURIComponent(kana)}`);
}
return `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
}
function buildAudioFilename(definition) {
if (!definition.reading && !definition.expression) {
return null;
}
let filename = 'yomichan';
if (definition.reading) {
filename += `_${definition.reading}`;
}
if (definition.expression) {
filename += `_${definition.expression}`;
}
return filename += '.mp3';
}