From c1d24208d383e687f443c4e7dfc7bfda81d191bd Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 8 Nov 2019 00:49:20 +0200 Subject: start mecab only after enabling the setting --- ext/bg/js/backend.js | 4 ++++ ext/bg/js/mecab.js | 14 ++++++++++++- ext/bg/js/settings.js | 15 +++++++++++++ ext/bg/settings.html | 58 +++++++++++++++++++++++++-------------------------- 4 files changed, 61 insertions(+), 30 deletions(-) (limited to 'ext/bg') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index e97f32b5..027cc250 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -98,6 +98,10 @@ class Backend { } this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); + + if (options.parsing.enableMecabParser) { + this.mecab.startListener(); + } } async getFullOptions() { diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js index fba9b2eb..4c62c2b0 100644 --- a/ext/bg/js/mecab.js +++ b/ext/bg/js/mecab.js @@ -19,16 +19,20 @@ class Mecab { constructor() { + this.port = null; this.listeners = {}; this.sequence = 0; - this.startListener(); } async parseText(text) { + if (this.port === null) { + return {}; + } return await this.invoke('parse_text', {text}); } startListener() { + if (this.port !== null) { return; } this.port = chrome.runtime.connectNative('yomichan_mecab'); this.port.onMessage.addListener((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) { return new Promise((resolve, reject) => { const sequence = this.sequence++; diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f4fe032a..0013291a 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -154,6 +154,7 @@ async function formWrite(options) { function formSetupEventListeners() { $('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged)); + $('#parsing-mecab-enable').change(onParseMecabChanged); $('.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 */ diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 8505567b..08b9b6c1 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -410,6 +410,35 @@ +
+

Text Parsing Options

+ +

+ 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. +

+ +

+ 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. +

+ +

+ The second type is an external program called MeCab + that uses its own dictionaries and a special parsing algorithm. To get it working, you must first + install it and a native messaging component + that acts as a bridge between the program and Yomichan. +

+ +
+ +
+ +
+ +
+
+
@@ -587,35 +616,6 @@
-
-

Text Parsing Options

- -

- 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. -

- -

- 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. -

- -

- The second type is an external program called MeCab - that uses its own dictionaries and a special parsing algorithm. To get it working, you must first - install it and a native messaging component - that acts as a bridge between the program and Yomichan. -

- -
- -
- -
- -
-
-
-- cgit v1.2.3