summaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js107
1 files changed, 54 insertions, 53 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 2691b7d9..238ac52c 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -48,6 +48,41 @@ class Backend {
this.apiForwarder = new BackendApiForwarder();
this.messageToken = yomichan.generateId(16);
+
+ this._messageHandlers = new Map([
+ ['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],
+ ['optionsGet', this._onApiOptionsGet.bind(this)],
+ ['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
+ ['optionsSet', this._onApiOptionsSet.bind(this)],
+ ['optionsSave', this._onApiOptionsSave.bind(this)],
+ ['kanjiFind', this._onApiKanjiFind.bind(this)],
+ ['termsFind', this._onApiTermsFind.bind(this)],
+ ['textParse', this._onApiTextParse.bind(this)],
+ ['textParseMecab', this._onApiTextParseMecab.bind(this)],
+ ['definitionAdd', this._onApiDefinitionAdd.bind(this)],
+ ['definitionsAddable', this._onApiDefinitionsAddable.bind(this)],
+ ['noteView', this._onApiNoteView.bind(this)],
+ ['templateRender', this._onApiTemplateRender.bind(this)],
+ ['commandExec', this._onApiCommandExec.bind(this)],
+ ['audioGetUrl', this._onApiAudioGetUrl.bind(this)],
+ ['screenshotGet', this._onApiScreenshotGet.bind(this)],
+ ['forward', this._onApiForward.bind(this)],
+ ['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
+ ['injectStylesheet', this._onApiInjectStylesheet.bind(this)],
+ ['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)],
+ ['clipboardGet', this._onApiClipboardGet.bind(this)],
+ ['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)],
+ ['getQueryParserTemplatesHtml', this._onApiGetQueryParserTemplatesHtml.bind(this)],
+ ['getZoom', this._onApiGetZoom.bind(this)],
+ ['getMessageToken', this._onApiGetMessageToken.bind(this)]
+ ]);
+
+ this._commandHandlers = new Map([
+ ['search', this._onCommandSearch.bind(this)],
+ ['help', this._onCommandHelp.bind(this)],
+ ['options', this._onCommandOptions.bind(this)],
+ ['toggle', this._onCommandToggle.bind(this)]
+ ]);
}
async prepare() {
@@ -65,10 +100,10 @@ class Backend {
this.onOptionsUpdated('background');
if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) {
- chrome.commands.onCommand.addListener((command) => this._runCommand(command));
+ chrome.commands.onCommand.addListener(this._runCommand.bind(this));
}
if (isObject(chrome.tabs) && isObject(chrome.tabs.onZoomChange)) {
- chrome.tabs.onZoomChange.addListener((info) => this._onZoomChange(info));
+ chrome.tabs.onZoomChange.addListener(this._onZoomChange.bind(this));
}
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
@@ -81,7 +116,7 @@ class Backend {
this.isPreparedResolve = null;
this.isPreparedPromise = null;
- this.clipboardMonitor.onClipboardText = (text) => this._onClipboardText(text);
+ this.clipboardMonitor.onClipboardText = this._onClipboardText.bind(this);
}
onOptionsUpdated(source) {
@@ -96,11 +131,11 @@ class Backend {
}
onMessage({action, params}, sender, callback) {
- const handler = Backend._messageHandlers.get(action);
+ const handler = this._messageHandlers.get(action);
if (typeof handler !== 'function') { return false; }
try {
- const promise = handler(this, params, sender);
+ const promise = handler(params, sender);
promise.then(
(result) => callback({result}),
(error) => callback({error: errorToJson(error)})
@@ -243,10 +278,10 @@ class Backend {
}
_runCommand(command, params) {
- const handler = Backend._commandHandlers.get(command);
+ const handler = this._commandHandlers.get(command);
if (typeof handler !== 'function') { return false; }
- handler(this, params);
+ handler(params);
return true;
}
@@ -357,11 +392,11 @@ class Backend {
async _onApiTextParseMecab({text, optionsContext}) {
const options = await this.getOptions(optionsContext);
- const results = {};
+ const results = [];
const rawResults = await this.mecab.parseText(text);
- for (const mecabName in rawResults) {
+ for (const [mecabName, parsedLines] of Object.entries(rawResults)) {
const result = [];
- for (const parsedLine of rawResults[mecabName]) {
+ for (const parsedLine of parsedLines) {
for (const {expression, reading, source} of parsedLine) {
const term = [];
if (expression !== null && reading !== null) {
@@ -381,7 +416,7 @@ class Backend {
}
result.push([{text: '\n'}]);
}
- results[mecabName] = result;
+ results.push([mecabName, result]);
}
return results;
}
@@ -693,9 +728,10 @@ class Backend {
}
_onCommandOptions(params) {
- if (!(params && params.newTab)) {
+ const {mode='existingOrNewTab'} = params || {};
+ if (mode === 'existingOrNewTab') {
chrome.runtime.openOptionsPage();
- } else {
+ } else if (mode === 'newTab') {
const manifest = chrome.runtime.getManifest();
const url = chrome.runtime.getURL(manifest.options_ui.page);
chrome.tabs.create({url});
@@ -718,8 +754,8 @@ class Backend {
async _injectScreenshot(definition, fields, screenshot) {
let usesScreenshot = false;
- for (const name in fields) {
- if (fields[name].includes('{screenshot}')) {
+ for (const fieldValue of Object.values(fields)) {
+ if (fieldValue.includes('{screenshot}')) {
usesScreenshot = true;
break;
}
@@ -802,7 +838,7 @@ class Backend {
chrome.tabs.update(tab.id, {active: true}, () => {
const e = chrome.runtime.lastError;
if (e) {
- reject(e);
+ reject(new Error(e.message));
} else {
resolve();
}
@@ -819,7 +855,7 @@ class Backend {
chrome.windows.get(tab.windowId, {}, (value) => {
const e = chrome.runtime.lastError;
if (e) {
- reject(e);
+ reject(new Error(e.message));
} else {
resolve(value);
}
@@ -830,7 +866,7 @@ class Backend {
chrome.windows.update(tab.windowId, {focused: true}, () => {
const e = chrome.runtime.lastError;
if (e) {
- reject(e);
+ reject(new Error(e.message));
} else {
resolve();
}
@@ -867,40 +903,5 @@ class Backend {
}
}
-Backend._messageHandlers = new Map([
- ['optionsSchemaGet', (self, ...args) => self._onApiOptionsSchemaGet(...args)],
- ['optionsGet', (self, ...args) => self._onApiOptionsGet(...args)],
- ['optionsGetFull', (self, ...args) => self._onApiOptionsGetFull(...args)],
- ['optionsSet', (self, ...args) => self._onApiOptionsSet(...args)],
- ['optionsSave', (self, ...args) => self._onApiOptionsSave(...args)],
- ['kanjiFind', (self, ...args) => self._onApiKanjiFind(...args)],
- ['termsFind', (self, ...args) => self._onApiTermsFind(...args)],
- ['textParse', (self, ...args) => self._onApiTextParse(...args)],
- ['textParseMecab', (self, ...args) => self._onApiTextParseMecab(...args)],
- ['definitionAdd', (self, ...args) => self._onApiDefinitionAdd(...args)],
- ['definitionsAddable', (self, ...args) => self._onApiDefinitionsAddable(...args)],
- ['noteView', (self, ...args) => self._onApiNoteView(...args)],
- ['templateRender', (self, ...args) => self._onApiTemplateRender(...args)],
- ['commandExec', (self, ...args) => self._onApiCommandExec(...args)],
- ['audioGetUrl', (self, ...args) => self._onApiAudioGetUrl(...args)],
- ['screenshotGet', (self, ...args) => self._onApiScreenshotGet(...args)],
- ['forward', (self, ...args) => self._onApiForward(...args)],
- ['frameInformationGet', (self, ...args) => self._onApiFrameInformationGet(...args)],
- ['injectStylesheet', (self, ...args) => self._onApiInjectStylesheet(...args)],
- ['getEnvironmentInfo', (self, ...args) => self._onApiGetEnvironmentInfo(...args)],
- ['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)],
- ['getDisplayTemplatesHtml', (self, ...args) => self._onApiGetDisplayTemplatesHtml(...args)],
- ['getQueryParserTemplatesHtml', (self, ...args) => self._onApiGetQueryParserTemplatesHtml(...args)],
- ['getZoom', (self, ...args) => self._onApiGetZoom(...args)],
- ['getMessageToken', (self, ...args) => self._onApiGetMessageToken(...args)]
-]);
-
-Backend._commandHandlers = new Map([
- ['search', (self, ...args) => self._onCommandSearch(...args)],
- ['help', (self, ...args) => self._onCommandHelp(...args)],
- ['options', (self, ...args) => self._onCommandOptions(...args)],
- ['toggle', (self, ...args) => self._onCommandToggle(...args)]
-]);
-
window.yomichanBackend = new Backend();
window.yomichanBackend.prepare();