summaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/backend.js4
-rw-r--r--ext/bg/js/mecab.js14
-rw-r--r--ext/bg/js/settings.js15
3 files changed, 32 insertions, 1 deletions
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));
}
@@ -441,6 +442,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
*/