From 1a52a2d8d5f5ea5c106ae244ca1e5bbf0da22b6e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 18 Feb 2017 19:12:39 -0800 Subject: fix not being able to play audio for kana only terms --- ext/fg/frame.html | 1 + ext/fg/js/frame.js | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ext/fg/frame.html b/ext/fg/frame.html index b7e2b41a..bf7f0f7b 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -17,6 +17,7 @@ + diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index ca0636f9..4f4a6378 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -164,11 +164,6 @@ class Frame { } playAudio(definition) { - let url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=${encodeURIComponent(definition.expression)}`; - if (definition.reading) { - url += `&kana=${encodeURIComponent(definition.reading)}`; - } - for (const key in this.audioCache) { const audio = this.audioCache[key]; if (audio !== null) { @@ -176,6 +171,28 @@ class Frame { } } + let kana = definition.reading; + let kanji = definition.expression; + if (!kana) { + if (!kanji) { + 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]; if (audio) { audio.currentTime = 0; -- cgit v1.2.3 From 64db42d536318cc4b91ed5a7484de302db7979f4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 19 Feb 2017 10:09:15 -0800 Subject: switch to new ankiconnect interface --- ext/bg/js/ankiconnect.js | 2 +- ext/bg/js/yomichan.js | 27 +++++++++++++++------------ ext/fg/js/frame.js | 34 +++++++++++++--------------------- ext/fg/js/util.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 34 deletions(-) diff --git a/ext/bg/js/ankiconnect.js b/ext/bg/js/ankiconnect.js index f6f94329..3a6e3690 100644 --- a/ext/bg/js/ankiconnect.js +++ b/ext/bg/js/ankiconnect.js @@ -20,7 +20,7 @@ class AnkiConnect { constructor(server) { this.server = server; this.asyncPools = {}; - this.localVersion = 1; + this.localVersion = 2; this.remoteVersion = null; } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 80a67588..4c70bf0f 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -100,20 +100,23 @@ class Yomichan { note.deckName = this.options.anki.terms.deck; note.modelName = this.options.anki.terms.model; - const audio = { - kanji: definition.expression, - kana: definition.reading, - fields: [] - }; - - for (const name in fields) { - if (fields[name].includes('{audio}')) { - audio.fields.push(name); + if (definition.audio) { + const audio = { + url: definition.audio.url, + filename: definition.audio.filename, + skipHash: '7e2c2f954ef6051373ba916f000168dc', + fields: [] + }; + + for (const name in fields) { + if (fields[name].includes('{audio}')) { + audio.fields.push(name); + } } - } - if (audio.fields.length > 0) { - note.audio = audio; + if (audio.fields.length > 0) { + note.audio = audio; + } } } diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 4f4a6378..1028f0f6 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -113,7 +113,16 @@ class Frame { const index = link.data('index'); 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) { const button = this.findAddNoteButton(index, mode); button.addClass('disabled'); @@ -171,28 +180,11 @@ class Frame { } } - let kana = definition.reading; - let kanji = definition.expression; - if (!kana) { - if (!kanji) { - 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 = buildAudioUrl(definition); + if (!url) { + return; } - const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; let audio = this.audioCache[url]; if (audio) { audio.currentTime = 0; diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index aeda36b5..c9ee4ed7 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -166,3 +166,43 @@ function extractSentence(source, extent) { 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'; +} -- cgit v1.2.3 From 5500ae27b24fc4242a5d180c129d649c3e992daa Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 19 Feb 2017 14:07:49 -0800 Subject: updating yomichan version --- ext/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/manifest.json b/ext/manifest.json index 0a1301c1..f2b73938 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Yomichan", - "version": "1.0.13", + "version": "1.0.14", "description": "Japanese dictionary with Anki integration", "icons": {"16": "img/icon16.png", "48": "img/icon48.png", "128": "img/icon128.png"}, -- cgit v1.2.3