start mecab only after enabling the setting

This commit is contained in:
siikamiika 2019-11-08 00:49:20 +02:00
parent 955e131f96
commit c1d24208d3
4 changed files with 61 additions and 30 deletions

View File

@ -98,6 +98,10 @@ class Backend {
} }
this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull();
if (options.parsing.enableMecabParser) {
this.mecab.startListener();
}
} }
async getFullOptions() { async getFullOptions() {

View File

@ -19,16 +19,20 @@
class Mecab { class Mecab {
constructor() { constructor() {
this.port = null;
this.listeners = {}; this.listeners = {};
this.sequence = 0; this.sequence = 0;
this.startListener();
} }
async parseText(text) { async parseText(text) {
if (this.port === null) {
return {};
}
return await this.invoke('parse_text', {text}); return await this.invoke('parse_text', {text});
} }
startListener() { startListener() {
if (this.port !== null) { return; }
this.port = chrome.runtime.connectNative('yomichan_mecab'); this.port = chrome.runtime.connectNative('yomichan_mecab');
this.port.onMessage.addListener((message) => { this.port.onMessage.addListener((message) => {
const {sequence, data} = message; const {sequence, data} = message;
@ -41,6 +45,14 @@ class Mecab {
}); });
} }
stopListener() {
if (this.port === null) { return; }
this.port.disconnect();
this.port = null;
this.listeners = {};
this.sequence = 0;
}
invoke(action, params) { invoke(action, params) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const sequence = this.sequence++; const sequence = this.sequence++;

View File

@ -154,6 +154,7 @@ async function formWrite(options) {
function formSetupEventListeners() { function formSetupEventListeners() {
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged)); $('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged));
$('#parsing-mecab-enable').change(onParseMecabChanged);
$('.anki-model').change(utilAsync(onAnkiModelChanged)); $('.anki-model').change(utilAsync(onAnkiModelChanged));
} }
@ -440,6 +441,20 @@ function onMessage({action, params}, sender, callback) {
} }
/*
* Text parsing
*/
function onParseMecabChanged(e) {
const mecab = utilBackend().mecab;
if (e.target.checked) {
mecab.startListener();
} else {
mecab.stopListener();
}
}
/* /*
* Anki * Anki
*/ */

View File

@ -410,6 +410,35 @@
</div> </div>
</div> </div>
<div id="text-parsing">
<h3>Text Parsing Options</h3>
<p class="help-block">
Yomichan can attempt to parse entire sentences or longer text blocks on the search page,
adding furigana above words and a small space between words.
</p>
<p class="help-block">
Two types of parsers are supported. The first one, enabled by default, works using the built-in
scanning functionality by automatically advancing in the sentence after a matching word.
</p>
<p class="help-block">
The second type is an external program called <a href="https://en.wikipedia.org/wiki/MeCab" target="_blank" rel="noopener">MeCab</a>
that uses its own dictionaries and a special parsing algorithm. To get it working, you must first
install it and <a href="https://github.com/siikamiika/yomichan-mecab-installer" target="_blank" rel="noopener">a native messaging component</a>
that acts as a bridge between the program and Yomichan.
</p>
<div class="checkbox">
<label><input type="checkbox" id="parsing-scan-enable"> Enable text parsing using installed dictionaries</label>
</div>
<div class="checkbox">
<label><input type="checkbox" id="parsing-mecab-enable"> Enable text parsing using MeCab</label>
</div>
</div>
<div class="ignore-form-changes"> <div class="ignore-form-changes">
<div> <div>
<img src="/mixed/img/spinner.gif" class="pull-right" id="dict-spinner" alt> <img src="/mixed/img/spinner.gif" class="pull-right" id="dict-spinner" alt>
@ -587,35 +616,6 @@
</div></div> </div></div>
</div> </div>
<div id="text-parsing">
<h3>Text Parsing Options</h3>
<p class="help-block">
Yomichan can attempt to parse entire sentences or longer text blocks on the search page,
adding furigana above words and a small space between words.
</p>
<p class="help-block">
Two types of parsers are supported. The first one, enabled by default, works using the built-in
scanning functionality by automatically advancing in the sentence after a matching word.
</p>
<p class="help-block">
The second type is an external program called <a href="https://en.wikipedia.org/wiki/MeCab" target="_blank" rel="noopener">MeCab</a>
that uses its own dictionaries and a special parsing algorithm. To get it working, you must first
install it and <a href="https://github.com/siikamiika/yomichan-mecab-installer" target="_blank" rel="noopener">a native messaging component</a>
that acts as a bridge between the program and Yomichan.
</p>
<div class="checkbox">
<label><input type="checkbox" id="parsing-scan-enable"> Enable text parsing using installed dictionaries</label>
</div>
<div class="checkbox">
<label><input type="checkbox" id="parsing-mecab-enable"> Enable text parsing using MeCab</label>
</div>
</div>
<div> <div>
<div> <div>
<img src="/mixed/img/spinner.gif" class="pull-right" id="anki-spinner" alt> <img src="/mixed/img/spinner.gif" class="pull-right" id="anki-spinner" alt>