diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-05-29 12:46:37 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-05-29 12:46:37 -0700 |
commit | 8c944c62944e24459d69432851dd848c7aa95d87 (patch) | |
tree | 0a1a2fe39e16bd854342e7f716ebddf9145def13 /ext/bg/js | |
parent | 17f0282b09c141abac4d2248ec2aee274254f947 (diff) |
New fact creation
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/yomichan.js | 79 |
1 files changed, 73 insertions, 6 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index d62f97aa..6918432c 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -91,12 +91,12 @@ class Yomichan { break; } - Yomichan.notifyTabs('state', this.state); + this.notifyTabs('state', this.state); } setOptions(options) { this.options = options; - Yomichan.notifyTabs('options', this.options); + this.notifyTabs('options', this.options); } ankiInvoke(action, params, pool, callback) { @@ -118,13 +118,60 @@ class Yomichan { xhr.open('POST', 'http://127.0.0.1:8765'); xhr.withCredentials = true; xhr.setRequestHeader('Content-Type', 'text/json'); - xhr.send(JSON.stringify({action: action, params: params})); + xhr.send(JSON.stringify({action, params})); } else { callback(null); } } - static notifyTabs(name, value) { + formatField(field, definition, kana) { + const supported = ['character', 'expression', 'glossary', 'kunyomi', 'onyomi', 'reading']; + + for (const key in definition) { + if (supported.indexOf(key) === -1) { + continue; + } + + let value = definition[key]; + if (kana) { + if (key === 'expression') { + value = definition.reading; + } else if (key === 'reading') { + value = ''; + } + } + if (key === 'glossary') { + value = definition.glossary.join('; '); + } + + field = field.replace(`{${key}}`, value); + } + + return field; + } + + formatNote(definition, mode) { + const note = {fields: {}, tags: []}; + + let fields = []; + if (mode === 'kanji') { + fields = this.options.ankiKanjiFields; + note.deckName = this.options.ankiKanjiDeck; + note.modelName = this.options.ankiKanjiModel; + } else { + fields = this.options.ankiVocabFields; + note.deckName = this.options.ankiVocabDeck; + note.modelName = this.options.ankiVocabModel; + } + + for (const name in fields) { + note.fields[name] = this.formatField(fields[name], definition, mode === 'vocabReading'); + } + + return note; + } + + notifyTabs(name, value) { chrome.tabs.query({}, (tabs) => { for (const tab of tabs) { chrome.tabs.sendMessage(tab.id, {name: name, value: value}, () => null); @@ -133,11 +180,31 @@ class Yomichan { } api_addNote({definition, mode, callback}) { - this.ankiInvoke('addNote', {definition: definition, mode: mode}, null, callback); + const note = this.formatNote(definition, mode); + this.ankiInvoke('addNote', {note}, null, callback); } api_canAddNotes({definitions, modes, callback}) { - this.ankiInvoke('canAddNotes', {definitions: definitions, modes: modes}, 'notes', callback); + let notes = []; + for (const definition of definitions) { + for (const mode of modes) { + notes.push(this.formatNote(definition, mode)); + } + } + + this.ankiInvoke('canAddNotes', {notes}, 'notes', (results) => { + const states = []; + for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + state[modes[modeOffset]] = results[resultBase + modeOffset]; + } + + states.push(state); + } + + callback(states); + }); } api_findKanji({text, callback}) { |