From b039d300249087aee18c7e8565ae7447d2cf5b15 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 4 Mar 2017 17:56:10 -0800 Subject: [PATCH] wip --- ext/bg/js/display-window.js | 52 +++++++++++++++++++++++++ ext/bg/js/gecko.js | 36 ----------------- ext/bg/js/search.js | 78 ------------------------------------- ext/bg/js/util.js | 8 ++++ ext/bg/js/yomichan.js | 68 +++++++++++++++++--------------- ext/bg/search.html | 5 ++- ext/fg/js/display-frame.js | 4 +- ext/mixed/js/display.js | 2 +- 8 files changed, 103 insertions(+), 150 deletions(-) create mode 100644 ext/bg/js/display-window.js delete mode 100644 ext/bg/js/gecko.js delete mode 100644 ext/bg/js/search.js 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 + * Author: Alex Yatskov + * + * 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 . + */ + + +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 - * Author: Alex Yatskov - * - * 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 - * Author: Alex Yatskov - * - * 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 . - */ - - -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,8 +144,36 @@ class Yomichan { }); } + definitionAdd(definition, mode) { + const note = this.formatNote(definition, mode); + return this.anki.addNote(note); + } + + definitionsAddable(definitions, modes) { + const notes = []; + for (const definition of definitions) { + for (const mode of modes) { + notes.push(this.formatNote(definition, mode)); + } + } + + return this.anki.canAddNotes(notes).then(raw => { + const states = []; + for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + state[modes[modeOffset]] = raw[resultBase + modeOffset]; + } + + states.push(state); + } + + return states; + }); + } + textRender(template, data) { - return Handlebars.templates[template](data); + return Promise.resolve(Handlebars.templates[template](data)); } api_optionsGet({callback}) { @@ -159,38 +189,14 @@ class Yomichan { } api_textRender({template, data, callback}) { - callback({result: this.textRender(template, data)}); + promiseCallback(this.textRender(template, data), callback); } api_definitionAdd({definition, mode, callback}) { - const note = this.formatNote(definition, mode); - promiseCallback(this.anki.addNote(note), callback); + promiseCallback(this.definitionAdd(definition, mode), callback); } api_definitionsAddable({definitions, modes, callback}) { - const notes = []; - for (const definition of definitions) { - for (const mode of modes) { - notes.push(this.formatNote(definition, mode)); - } - } - - const promise = this.anki.canAddNotes(notes).then(raw => { - const states = []; - for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { - const state = {}; - for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { - state[modes[modeOffset]] = raw[resultBase + modeOffset]; - } - - states.push(state); - } - - return states; - }); - - promiseCallback(promise, callback); + promiseCallback(this.definitionsAddable(definitions, modes), callback); } -} - -window.yomichan = new Yomichan(); +}; 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 @@
+ - - + + 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'; }