This commit is contained in:
Alex Yatskov 2016-05-07 18:24:31 -07:00
parent e6ea1ddb15
commit 4a6055d781
5 changed files with 41 additions and 19 deletions

View File

@ -18,18 +18,20 @@
function optionsToForm(opts) { function optionsToForm(opts) {
$('#activateOnStartup').prop('checked', opts.activateOnStartup);
$('#enableAnkiConnect').prop('checked', opts.enableAnkiConnect); $('#enableAnkiConnect').prop('checked', opts.enableAnkiConnect);
$('#selectMatchedText').prop('checked', opts.selectMatchedText); $('#loadEnamDict').prop('checked', opts.loadEnamDict);
$('#loadOnStartup').prop('checked', opts.loadOnStartup);
$('#scanLength').val(opts.scanLength); $('#scanLength').val(opts.scanLength);
$('#selectMatchedText').prop('checked', opts.selectMatchedText);
} }
function formToOptions() { function formToOptions() {
return sanitizeOptions({ return sanitizeOptions({
loadOnStartup: $('#loadOnStartup').prop('checked'), activateOnStartup: $('#activateOnStartup').prop('checked'),
selectMatchedText: $('#selectMatchedText').prop('checked'),
enableAnkiConnect: $('#enableAnkiConnect').prop('checked'), enableAnkiConnect: $('#enableAnkiConnect').prop('checked'),
scanLength: $('#scanLength').val() loadEnamDict: $('#loadEnamDict').prop('checked'),
scanLength: $('#scanLength').val(),
selectMatchedText: $('#selectMatchedText').prop('checked')
}); });
} }

View File

@ -19,10 +19,11 @@
function sanitizeOptions(options) { function sanitizeOptions(options) {
const defaults = { const defaults = {
loadOnStartup: false, activateOnStartup: false,
selectMatchedText: true,
enableAnkiConnect: false, enableAnkiConnect: false,
scanLength: 20 loadEnamDict: false,
scanLength: 20,
selectMatchedText: true
}; };
for (const key in defaults) { for (const key in defaults) {

View File

@ -33,14 +33,19 @@ class Translator {
this.deinflector = new Deinflector(); this.deinflector = new Deinflector();
} }
loadData(callback) { loadData({loadEnamDict=true}, callback) {
if (this.loaded) { if (this.loaded) {
callback(); callback();
return; return;
} }
let files = ['rules', 'tags', 'edict', 'kanjidic'];
if (loadEnamDict) {
files = files.concat('enamdict');
}
const pendingLoads = []; const pendingLoads = [];
for (const key of ['rules', 'tags', 'edict', 'enamdict', 'kanjidic']) { for (const key of files) {
pendingLoads.push(key); pendingLoads.push(key);
Translator.loadData(this.paths[key], (response) => { Translator.loadData(this.paths[key], (response) => {
switch (key) { switch (key) {
@ -82,12 +87,14 @@ class Translator {
return tags; return tags;
}); });
if (dfs !== null) { if (dfs === null) {
continue;
}
for (const df of dfs) { for (const df of dfs) {
this.processTerm(groups, df.source, df.tags, df.rules, df.root); this.processTerm(groups, df.source, df.tags, df.rules, df.root);
} }
} }
}
let definitions = []; let definitions = [];
for (const key in groups) { for (const key in groups) {

View File

@ -43,7 +43,7 @@ class Yomichan {
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
if (this.options.loadOnStartup) { if (this.options.activateOnStartup) {
this.setState('loading'); this.setState('loading');
} }
}); });
@ -52,6 +52,7 @@ class Yomichan {
onMessage(request, sender, callback) { onMessage(request, sender, callback) {
const {action, params} = request, handlers = { const {action, params} = request, handlers = {
canAddNotes: ({definitions, modes}) => this.ankiInvoke('canAddNotes', {definitions: definitions, modes: modes}, 'notes', callback), canAddNotes: ({definitions, modes}) => this.ankiInvoke('canAddNotes', {definitions: definitions, modes: modes}, 'notes', callback),
addNote: ({definition, mode}) => this.ankiInvoke('addNote', {definition: definition, mode: mode}, null, callback),
findKanji: (text) => callback(this.translator.findKanji(text)), findKanji: (text) => callback(this.translator.findKanji(text)),
findTerm: (text) => callback(this.translator.findTerm(text)), findTerm: (text) => callback(this.translator.findTerm(text)),
getOptions: () => callback(this.options), getOptions: () => callback(this.options),
@ -90,7 +91,10 @@ class Yomichan {
break; break;
case 'loading': case 'loading':
chrome.browserAction.setBadgeText({text: '...'}); chrome.browserAction.setBadgeText({text: '...'});
this.translator.loadData(() => this.setState('enabled')); this.translator.loadData(
{loadEnamDict: this.options.loadEnamDict},
() => this.setState('enabled')
);
break; break;
} }

View File

@ -23,7 +23,15 @@
<div class="form-group"> <div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <div class="col-sm-offset-2 col-sm-10">
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" id="loadOnStartup"> Load on startup</label> <label><input type="checkbox" id="activateOnStartup"> Activate on startup</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox" id="loadEnamDict"> Load <a href="http://www.edrdg.org/enamdict/enamdict_doc.html">ENAMDICT</a> (requires restart)</label>
</div> </div>
</div> </div>
</div> </div>
@ -39,7 +47,7 @@
<div class="form-group"> <div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <div class="col-sm-offset-2 col-sm-10">
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" id="enableAnkiConnect" checked> Enable AnkiConnect</label> <label><input type="checkbox" id="enableAnkiConnect" checked> Enable <a href="https://foosoft.net/projects/yomichan">AnkiConnect</a></label>
</div> </div>
</div> </div>
</div> </div>