diff options
| author | siikamiika <siikamiika@users.noreply.github.com> | 2020-03-02 04:51:45 +0200 | 
|---|---|---|
| committer | siikamiika <siikamiika@users.noreply.github.com> | 2020-03-02 04:51:45 +0200 | 
| commit | 967e99b7f69d24fc76999675cef44b919602dd31 (patch) | |
| tree | 4128ebda33c7ff04a597f03a2ea46c183ae22f18 /ext/bg/js/backend.js | |
| parent | e6e5f23cf8481db31b94c18244f404dd3374ad90 (diff) | |
ensure Backend prepare in other places
Diffstat (limited to 'ext/bg/js/backend.js')
| -rw-r--r-- | ext/bg/js/backend.js | 22 | 
1 files changed, 15 insertions, 7 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index e849bc69..81578462 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -48,7 +48,7 @@ class Backend {          this.messageToken = yomichan.generateId(16);          this._messageHandlers = new Map([ -            ['isBackendReady', this._onApiIsBackendReady.bind(this)], +            ['yomichanOnline', this._onApiYomichanOnline.bind(this)],              ['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],              ['optionsGet', this._onApiOptionsGet.bind(this)],              ['optionsGetFull', this._onApiOptionsGetFull.bind(this)], @@ -114,19 +114,24 @@ class Backend {          }          this.clipboardMonitor.onClipboardText = this._onClipboardText.bind(this); -    } -    onOptionsUpdated(source) { -        this.applyOptions(); +        this._sendMessageAllTabs('backendPrepared'); +    } +    _sendMessageAllTabs(action, params={}) {          const callback = () => this.checkLastError(chrome.runtime.lastError);          chrome.tabs.query({}, (tabs) => {              for (const tab of tabs) { -                chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdated', params: {source}}, callback); +                chrome.tabs.sendMessage(tab.id, {action, params}, callback);              }          });      } +    onOptionsUpdated(source) { +        this.applyOptions(); +        this._sendMessageAllTabs('optionsUpdated', {source}); +    } +      onMessage({action, params}, sender, callback) {          const handler = this._messageHandlers.get(action);          if (typeof handler !== 'function') { return false; } @@ -268,8 +273,11 @@ class Backend {      // Message handlers -    async _onApiIsBackendReady() { -        return true; +    async _onApiYomichanOnline(_params, sender) { +        const tabId = sender.tab.id; +        return new Promise((resolve) => { +            chrome.tabs.sendMessage(tabId, {action: 'backendPrepared'}, resolve); +        });      }      async _onApiOptionsSchemaGet() {  |