Merge pull request #437 from toasted-nutbread/backend-api-handler-changes
Backend api handler changes
This commit is contained in:
commit
a864cf094f
@ -76,33 +76,33 @@ class Backend {
|
|||||||
this.messageToken = yomichan.generateId(16);
|
this.messageToken = yomichan.generateId(16);
|
||||||
|
|
||||||
this._messageHandlers = new Map([
|
this._messageHandlers = new Map([
|
||||||
['yomichanCoreReady', this._onApiYomichanCoreReady.bind(this)],
|
['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: false}],
|
||||||
['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],
|
['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: false}],
|
||||||
['optionsGet', this._onApiOptionsGet.bind(this)],
|
['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: false}],
|
||||||
['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
|
['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: false}],
|
||||||
['optionsSet', this._onApiOptionsSet.bind(this)],
|
['optionsSet', {handler: this._onApiOptionsSet.bind(this), async: true}],
|
||||||
['optionsSave', this._onApiOptionsSave.bind(this)],
|
['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}],
|
||||||
['kanjiFind', this._onApiKanjiFind.bind(this)],
|
['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}],
|
||||||
['termsFind', this._onApiTermsFind.bind(this)],
|
['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}],
|
||||||
['textParse', this._onApiTextParse.bind(this)],
|
['textParse', {handler: this._onApiTextParse.bind(this), async: true}],
|
||||||
['textParseMecab', this._onApiTextParseMecab.bind(this)],
|
['textParseMecab', {handler: this._onApiTextParseMecab.bind(this), async: true}],
|
||||||
['definitionAdd', this._onApiDefinitionAdd.bind(this)],
|
['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}],
|
||||||
['definitionsAddable', this._onApiDefinitionsAddable.bind(this)],
|
['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}],
|
||||||
['noteView', this._onApiNoteView.bind(this)],
|
['noteView', {handler: this._onApiNoteView.bind(this), async: true}],
|
||||||
['templateRender', this._onApiTemplateRender.bind(this)],
|
['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}],
|
||||||
['commandExec', this._onApiCommandExec.bind(this)],
|
['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}],
|
||||||
['audioGetUri', this._onApiAudioGetUri.bind(this)],
|
['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}],
|
||||||
['screenshotGet', this._onApiScreenshotGet.bind(this)],
|
['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}],
|
||||||
['forward', this._onApiForward.bind(this)],
|
['broadcastTab', {handler: this._onApiBroadcastTab.bind(this), async: false}],
|
||||||
['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
|
['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}],
|
||||||
['injectStylesheet', this._onApiInjectStylesheet.bind(this)],
|
['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}],
|
||||||
['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)],
|
['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}],
|
||||||
['clipboardGet', this._onApiClipboardGet.bind(this)],
|
['clipboardGet', {handler: this._onApiClipboardGet.bind(this), async: true}],
|
||||||
['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)],
|
['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}],
|
||||||
['getQueryParserTemplatesHtml', this._onApiGetQueryParserTemplatesHtml.bind(this)],
|
['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}],
|
||||||
['getZoom', this._onApiGetZoom.bind(this)],
|
['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}],
|
||||||
['getMessageToken', this._onApiGetMessageToken.bind(this)],
|
['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: false}],
|
||||||
['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)]
|
['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
this._commandHandlers = new Map([
|
this._commandHandlers = new Map([
|
||||||
@ -166,16 +166,23 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMessage({action, params}, sender, callback) {
|
onMessage({action, params}, sender, callback) {
|
||||||
const handler = this._messageHandlers.get(action);
|
const messageHandler = this._messageHandlers.get(action);
|
||||||
if (typeof handler !== 'function') { return false; }
|
if (typeof messageHandler === 'undefined') { return false; }
|
||||||
|
|
||||||
|
const {handler, async} = messageHandler;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const promise = handler(params, sender);
|
const promiseOrResult = handler(params, sender);
|
||||||
promise.then(
|
if (async) {
|
||||||
(result) => callback({result}),
|
promiseOrResult.then(
|
||||||
(error) => callback({error: errorToJson(error)})
|
(result) => callback({result}),
|
||||||
);
|
(error) => callback({error: errorToJson(error)})
|
||||||
return true;
|
);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
callback({result: promiseOrResult});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
callback({error: errorToJson(error)});
|
callback({error: errorToJson(error)});
|
||||||
return false;
|
return false;
|
||||||
@ -312,27 +319,26 @@ class Backend {
|
|||||||
|
|
||||||
_onApiYomichanCoreReady(_params, sender) {
|
_onApiYomichanCoreReady(_params, sender) {
|
||||||
// tab ID isn't set in background (e.g. browser_action)
|
// tab ID isn't set in background (e.g. browser_action)
|
||||||
|
const callback = () => this.checkLastError(chrome.runtime.lastError);
|
||||||
|
const data = {action: 'backendPrepared'};
|
||||||
if (typeof sender.tab === 'undefined') {
|
if (typeof sender.tab === 'undefined') {
|
||||||
const callback = () => this.checkLastError(chrome.runtime.lastError);
|
chrome.runtime.sendMessage(data, callback);
|
||||||
chrome.runtime.sendMessage({action: 'backendPrepared'}, callback);
|
return false;
|
||||||
return Promise.resolve();
|
} else {
|
||||||
|
chrome.tabs.sendMessage(sender.tab.id, data, callback);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tabId = sender.tab.id;
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
chrome.tabs.sendMessage(tabId, {action: 'backendPrepared'}, resolve);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiOptionsSchemaGet() {
|
_onApiOptionsSchemaGet() {
|
||||||
return this.getOptionsSchema();
|
return this.getOptionsSchema();
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiOptionsGet({optionsContext}) {
|
_onApiOptionsGet({optionsContext}) {
|
||||||
return this.getOptions(optionsContext);
|
return this.getOptions(optionsContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiOptionsGetFull() {
|
_onApiOptionsGetFull() {
|
||||||
return this.getFullOptions();
|
return this.getFullOptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -539,7 +545,7 @@ class Backend {
|
|||||||
return this._renderTemplate(template, data);
|
return this._renderTemplate(template, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiCommandExec({command, params}) {
|
_onApiCommandExec({command, params}) {
|
||||||
return this._runCommand(command, params);
|
return this._runCommand(command, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,15 +565,15 @@ class Backend {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_onApiForward({action, params}, sender) {
|
_onApiBroadcastTab({action, params}, sender) {
|
||||||
if (!(sender && sender.tab)) {
|
if (!(sender && sender.tab)) {
|
||||||
return Promise.resolve();
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tabId = sender.tab.id;
|
const tabId = sender.tab.id;
|
||||||
return new Promise((resolve) => {
|
const callback = () => this.checkLastError(chrome.runtime.lastError);
|
||||||
chrome.tabs.sendMessage(tabId, {action, params}, (response) => resolve(response));
|
chrome.tabs.sendMessage(tabId, {action, params}, callback);
|
||||||
});
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onApiFrameInformationGet(params, sender) {
|
_onApiFrameInformationGet(params, sender) {
|
||||||
@ -690,11 +696,11 @@ class Backend {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiGetMessageToken() {
|
_onApiGetMessageToken() {
|
||||||
return this.messageToken;
|
return this.messageToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiGetDefaultAnkiFieldTemplates() {
|
_onApiGetDefaultAnkiFieldTemplates() {
|
||||||
return this.defaultAnkiFieldTemplates;
|
return this.defaultAnkiFieldTemplates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
/* global
|
/* global
|
||||||
* Display
|
* Display
|
||||||
* apiForward
|
* apiBroadcastTab
|
||||||
* apiGetMessageToken
|
* apiGetMessageToken
|
||||||
* popupNestedInitialize
|
* popupNestedInitialize
|
||||||
*/
|
*/
|
||||||
@ -79,7 +79,7 @@ class DisplayFloat extends Display {
|
|||||||
|
|
||||||
this.setContentScale(scale);
|
this.setContentScale(scale);
|
||||||
|
|
||||||
apiForward('popupPrepareCompleted', {targetPopupId: this._popupId});
|
apiBroadcastTab('popupPrepareCompleted', {targetPopupId: this._popupId});
|
||||||
}
|
}
|
||||||
|
|
||||||
onError(error) {
|
onError(error) {
|
||||||
@ -180,7 +180,7 @@ class DisplayFloat extends Display {
|
|||||||
},
|
},
|
||||||
2000
|
2000
|
||||||
);
|
);
|
||||||
apiForward('requestDocumentInformationBroadcast', {uniqueId});
|
apiBroadcastTab('requestDocumentInformationBroadcast', {uniqueId});
|
||||||
|
|
||||||
const {title} = await promise;
|
const {title} = await promise;
|
||||||
return title;
|
return title;
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* global
|
/* global
|
||||||
* apiForward
|
* apiBroadcastTab
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class FrameOffsetForwarder {
|
class FrameOffsetForwarder {
|
||||||
@ -96,6 +96,6 @@ class FrameOffsetForwarder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_forwardFrameOffsetOrigin(offset, uniqueId) {
|
_forwardFrameOffsetOrigin(offset, uniqueId) {
|
||||||
apiForward('frameOffset', {offset, uniqueId});
|
apiBroadcastTab('frameOffset', {offset, uniqueId});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
* Frontend
|
* Frontend
|
||||||
* PopupProxy
|
* PopupProxy
|
||||||
* PopupProxyHost
|
* PopupProxyHost
|
||||||
* apiForward
|
* apiBroadcastTab
|
||||||
* apiOptionsGet
|
* apiOptionsGet
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ async function main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
apiForward('rootPopupRequestInformationBroadcast');
|
apiBroadcastTab('rootPopupRequestInformationBroadcast');
|
||||||
const {popupId, frameId} = await rootPopupInformationPromise;
|
const {popupId, frameId} = await rootPopupInformationPromise;
|
||||||
|
|
||||||
const frameOffsetForwarder = new FrameOffsetForwarder();
|
const frameOffsetForwarder = new FrameOffsetForwarder();
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
/* global
|
/* global
|
||||||
* TextScanner
|
* TextScanner
|
||||||
* apiForward
|
* apiBroadcastTab
|
||||||
* apiGetZoom
|
* apiGetZoom
|
||||||
* apiKanjiFind
|
* apiKanjiFind
|
||||||
* apiOptionsGet
|
* apiOptionsGet
|
||||||
@ -260,12 +260,12 @@ class Frontend extends TextScanner {
|
|||||||
|
|
||||||
_broadcastRootPopupInformation() {
|
_broadcastRootPopupInformation() {
|
||||||
if (!this.popup.isProxy() && this.popup.depth === 0) {
|
if (!this.popup.isProxy() && this.popup.depth === 0) {
|
||||||
apiForward('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId});
|
apiBroadcastTab('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_broadcastDocumentInformation(uniqueId) {
|
_broadcastDocumentInformation(uniqueId) {
|
||||||
apiForward('documentInformationBroadcast', {
|
apiBroadcastTab('documentInformationBroadcast', {
|
||||||
uniqueId,
|
uniqueId,
|
||||||
frameId: this.popup.frameId,
|
frameId: this.popup.frameId,
|
||||||
title: document.title
|
title: document.title
|
||||||
|
@ -80,8 +80,8 @@ function apiScreenshotGet(options) {
|
|||||||
return _apiInvoke('screenshotGet', {options});
|
return _apiInvoke('screenshotGet', {options});
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiForward(action, params) {
|
function apiBroadcastTab(action, params) {
|
||||||
return _apiInvoke('forward', {action, params});
|
return _apiInvoke('broadcastTab', {action, params});
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiFrameInformationGet() {
|
function apiFrameInformationGet() {
|
||||||
|
@ -22,9 +22,9 @@
|
|||||||
* DisplayGenerator
|
* DisplayGenerator
|
||||||
* WindowScroll
|
* WindowScroll
|
||||||
* apiAudioGetUri
|
* apiAudioGetUri
|
||||||
|
* apiBroadcastTab
|
||||||
* apiDefinitionAdd
|
* apiDefinitionAdd
|
||||||
* apiDefinitionsAddable
|
* apiDefinitionsAddable
|
||||||
* apiForward
|
|
||||||
* apiKanjiFind
|
* apiKanjiFind
|
||||||
* apiNoteView
|
* apiNoteView
|
||||||
* apiOptionsGet
|
* apiOptionsGet
|
||||||
@ -854,7 +854,7 @@ class Display {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setPopupVisibleOverride(visible) {
|
setPopupVisibleOverride(visible) {
|
||||||
return apiForward('popupSetVisibleOverride', {visible});
|
return apiBroadcastTab('popupSetVisibleOverride', {visible});
|
||||||
}
|
}
|
||||||
|
|
||||||
setSpinnerVisible(visible) {
|
setSpinnerVisible(visible) {
|
||||||
|
Loading…
Reference in New Issue
Block a user