diff options
-rw-r--r-- | ext/bg/js/display-window.js | 52 | ||||
-rw-r--r-- | ext/bg/js/gecko.js | 36 | ||||
-rw-r--r-- | ext/bg/js/search.js | 78 | ||||
-rw-r--r-- | ext/bg/js/util.js | 8 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 64 | ||||
-rw-r--r-- | ext/bg/search.html | 5 | ||||
-rw-r--r-- | ext/fg/js/display-frame.js | 4 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 2 |
8 files changed, 101 insertions, 148 deletions
diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js new file mode 100644 index 00000000..44d1f7a9 --- /dev/null +++ b/ext/bg/js/display-window.js @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2016 Alex Yatskov <alex@foosoft.net> + * Author: Alex Yatskov <alex@foosoft.net> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +window.displayWindow = new class extends Display { + constructor() { + super($('#spinner'), $('#content')); + $('#search').click(this.onSearch.bind(this)); + } + + definitionAdd(definition, mode) { + return instYomi().definitionAdd(definition, mode); + } + + definitionsAddable(definitions, modes) { + return instYomi().definitionsAddable(definitions, modes); + } + + textRender(template, data) { + return instYomi().textRender(template, data); + } + + kanjiFind(character) { + return instYomi().kanjiFind(character); + } + + handleError(error) { + window.alert(`Error: ${error}`); + } + + onSearch(e) { + e.preventDefault(); + instYomi().termsFind($('#query').val()).then(({length, definitions}) => { + super.showTermDefs(definitions, instYomi().options); + }).catch(this.handleError.bind(this)); + } +}; diff --git a/ext/bg/js/gecko.js b/ext/bg/js/gecko.js deleted file mode 100644 index 3df07f0b..00000000 --- a/ext/bg/js/gecko.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2017 Alex Yatskov <alex@foosoft.net> - * Author: Alex Yatskov <alex@foosoft.net> - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -if (!chrome.storage.sync) { - // https://bugzilla.mozilla.org/show_bug.cgi?id=1220494 - chrome.storage.sync = chrome.storage.local; -} - -if (!chrome.runtime.onInstalled) { - // https://bugzilla.mozilla.org/show_bug.cgi?id=1252871 - chrome.runtime.onInstalled = { - 'addListener': () => {}, - 'hasListener': () => {}, - 'removeListener': () => {} - }; -} diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js deleted file mode 100644 index 9368a3cd..00000000 --- a/ext/bg/js/search.js +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2017 Alex Yatskov <alex@foosoft.net> - * Author: Alex Yatskov <alex@foosoft.net> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - - -function onSearch(e) { - e.preventDefault(); - - instYomi().termsFind($('#query').val()).then(({length, definitions}) => { - const options = instYomi().options; - const params = { - definitions, - grouped: options.general.groupResults, - addable: options.anki.enabled, - playback: options.general.audioPlayback - }; - - return instYomi().textRender('terms.html', params); - }).then(content => { - $('#content').html(content); - }); - - // const sequence = ++this.sequence; - // const params = { - // definitions, - // grouped: options.general.groupResults, - // addable: options.ankiMethod !== 'disabled', - // playback: options.general.audioPlayback - // }; - - // definitions.forEach(definition => { - // definition.sentence = context.sentence; - // definition.url = context.url; - // }); - - // this.definitions = definitions; - // this.showSpinner(false); - // window.scrollTo(0, 0); - - // bgTextRender(params, 'terms.html').then(content => { - // $('#content').html(content); - // $('.action-add-note').click(this.onAddNote.bind(this)); - - // $('.kanji-link').click(e => { - // e.preventDefault(); - // const character = $(e.target).text(); - // bgKanjiFind(character).then(definitions => this.api_showKanjiDefs({definitions, options, context})); - // }); - - // $('.action-play-audio').click(e => { - // e.preventDefault(); - // const index = $(e.currentTarget).data('index'); - // this.playAudio(this.definitions[index]); - // }); - - // this.updateAddNoteButtons(['term_kanji', 'term_kana'], sequence); - // }).catch(error => { - // this.handleError(error); - // }); -} - -$(document).ready(() => { - $('#search').click(onSearch); -}); diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 7a8f15ef..c57f6ae1 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -172,12 +172,20 @@ function optionsVersion(options) { } function optionsLoad() { + if (!chrome.storage.sync) { + chrome.storage.sync = chrome.storage.local; + } + return new Promise((resolve, reject) => { chrome.storage.sync.get(null, options => resolve(optionsVersion(options))); }); } function optionsSave(options) { + if (!chrome.storage.sync) { + chrome.storage.sync = chrome.storage.local; + } + return new Promise((resolve, reject) => { chrome.storage.sync.set(options, resolve); }); diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 898f2eea..f7bec615 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -17,7 +17,7 @@ */ -class Yomichan { +window.yomichan = new class { constructor() { Handlebars.partials = Handlebars.templates; Handlebars.registerHelper('kanjiLinks', helperKanjiLinks); @@ -28,7 +28,9 @@ class Yomichan { this.options = null; chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); - chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); + if (chrome.runtime.onInstalled) { + chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); + } this.translator.prepare().then(optionsLoad).then(this.setOptions.bind(this)); } @@ -142,32 +144,12 @@ class Yomichan { }); } - textRender(template, data) { - return Handlebars.templates[template](data); - } - - api_optionsGet({callback}) { - promiseCallback(optionsLoad(), callback); - } - - api_kanjiFind({text, callback}) { - promiseCallback(this.kanjiFind(text), callback); - } - - api_termsFind({text, callback}) { - promiseCallback(this.termsFind(text), callback); - } - - api_textRender({template, data, callback}) { - callback({result: this.textRender(template, data)}); - } - - api_definitionAdd({definition, mode, callback}) { + definitionAdd(definition, mode) { const note = this.formatNote(definition, mode); - promiseCallback(this.anki.addNote(note), callback); + return this.anki.addNote(note); } - api_definitionsAddable({definitions, modes, callback}) { + definitionsAddable(definitions, modes) { const notes = []; for (const definition of definitions) { for (const mode of modes) { @@ -175,7 +157,7 @@ class Yomichan { } } - const promise = this.anki.canAddNotes(notes).then(raw => { + return this.anki.canAddNotes(notes).then(raw => { const states = []; for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { const state = {}; @@ -188,9 +170,33 @@ class Yomichan { return states; }); + } - promiseCallback(promise, callback); + textRender(template, data) { + return Promise.resolve(Handlebars.templates[template](data)); + } + + api_optionsGet({callback}) { + promiseCallback(optionsLoad(), callback); } -} -window.yomichan = new Yomichan(); + api_kanjiFind({text, callback}) { + promiseCallback(this.kanjiFind(text), callback); + } + + api_termsFind({text, callback}) { + promiseCallback(this.termsFind(text), callback); + } + + api_textRender({template, data, callback}) { + promiseCallback(this.textRender(template, data), callback); + } + + api_definitionAdd({definition, mode, callback}) { + promiseCallback(this.definitionAdd(definition, mode), callback); + } + + api_definitionsAddable({definitions, modes, callback}) { + promiseCallback(this.definitionsAddable(definitions, modes), callback); + } +}; diff --git a/ext/bg/search.html b/ext/bg/search.html index 7921501b..937cc602 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -24,9 +24,10 @@ <div id="content"></div> </div> + <script src="/mixed/lib/jquery-3.1.1.min.js"></script> - <script src="/bg/js/gecko.js"></script> <script src="/bg/js/util.js"></script> - <script src="/bg/js/search.js"></script> + <script src="/mixed/js/display.js"></script> + <script src="/bg/js/display-window.js"></script> </body> </html> diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index 8f4a790f..16435a27 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -27,8 +27,8 @@ window.displayFrame = new class extends Display { return bgDefinitionAdd(definition, mode); } - definitionsAddable(definitions, mode) { - return bgDefinitionsAddable(definitions, mode); + definitionsAddable(definitions, modes) { + return bgDefinitionsAddable(definitions, modes); } textRender(template, data) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 6a283b5f..26d50862 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -30,7 +30,7 @@ class Display { throw 'override me'; } - definitionsAddable(definitions, mode) { + definitionsAddable(definitions, modes) { throw 'override me'; } |