This commit is contained in:
Alex Yatskov 2017-03-04 17:56:10 -08:00
parent c8eb77cfd9
commit b039d30024
8 changed files with 103 additions and 150 deletions

View File

@ -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));
}
};

View File

@ -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': () => {}
};
}

View File

@ -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);
});

View File

@ -172,12 +172,20 @@ function optionsVersion(options) {
} }
function optionsLoad() { function optionsLoad() {
if (!chrome.storage.sync) {
chrome.storage.sync = chrome.storage.local;
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.storage.sync.get(null, options => resolve(optionsVersion(options))); chrome.storage.sync.get(null, options => resolve(optionsVersion(options)));
}); });
} }
function optionsSave(options) { function optionsSave(options) {
if (!chrome.storage.sync) {
chrome.storage.sync = chrome.storage.local;
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.storage.sync.set(options, resolve); chrome.storage.sync.set(options, resolve);
}); });

View File

@ -17,7 +17,7 @@
*/ */
class Yomichan { window.yomichan = new class {
constructor() { constructor() {
Handlebars.partials = Handlebars.templates; Handlebars.partials = Handlebars.templates;
Handlebars.registerHelper('kanjiLinks', helperKanjiLinks); Handlebars.registerHelper('kanjiLinks', helperKanjiLinks);
@ -28,7 +28,9 @@ class Yomichan {
this.options = null; this.options = null;
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
if (chrome.runtime.onInstalled) {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
}
this.translator.prepare().then(optionsLoad).then(this.setOptions.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) { textRender(template, data) {
return Handlebars.templates[template](data); return Promise.resolve(Handlebars.templates[template](data));
} }
api_optionsGet({callback}) { api_optionsGet({callback}) {
@ -159,38 +189,14 @@ class Yomichan {
} }
api_textRender({template, data, callback}) { api_textRender({template, data, callback}) {
callback({result: this.textRender(template, data)}); promiseCallback(this.textRender(template, data), callback);
} }
api_definitionAdd({definition, mode, callback}) { api_definitionAdd({definition, mode, callback}) {
const note = this.formatNote(definition, mode); promiseCallback(this.definitionAdd(definition, mode), callback);
promiseCallback(this.anki.addNote(note), callback);
} }
api_definitionsAddable({definitions, modes, callback}) { api_definitionsAddable({definitions, modes, callback}) {
const notes = []; promiseCallback(this.definitionsAddable(definitions, modes), callback);
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);
}
}
window.yomichan = new Yomichan();

View File

@ -24,9 +24,10 @@
<div id="content"></div> <div id="content"></div>
</div> </div>
<script src="/mixed/lib/jquery-3.1.1.min.js"></script> <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/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> </body>
</html> </html>

View File

@ -27,8 +27,8 @@ window.displayFrame = new class extends Display {
return bgDefinitionAdd(definition, mode); return bgDefinitionAdd(definition, mode);
} }
definitionsAddable(definitions, mode) { definitionsAddable(definitions, modes) {
return bgDefinitionsAddable(definitions, mode); return bgDefinitionsAddable(definitions, modes);
} }
textRender(template, data) { textRender(template, data) {

View File

@ -30,7 +30,7 @@ class Display {
throw 'override me'; throw 'override me';
} }
definitionsAddable(definitions, mode) { definitionsAddable(definitions, modes) {
throw 'override me'; throw 'override me';
} }