From 7f56a08f3ba5407e6c8cdec12dc59aa71aa06d03 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 17 Jun 2018 21:42:52 +0900 Subject: [PATCH] Use async/await on content script --- src/console/components/console.js | 7 ++-- src/content/actions/find.js | 46 ++++++++++++-------------- src/content/components/common/index.js | 13 ++++---- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/console/components/console.js b/src/console/components/console.js index a9ae4ed..417c9f6 100644 --- a/src/console/components/console.js +++ b/src/console/components/console.js @@ -107,16 +107,15 @@ export default class ConsoleComponent { } } - onInput(e) { + async onInput(e) { this.store.dispatch(consoleActions.setConsoleText(e.target.value)); let source = e.target.value; - return browser.runtime.sendMessage({ + let completions = await browser.runtime.sendMessage({ type: messages.CONSOLE_QUERY_COMPLETIONS, text: source, - }).then((completions) => { - this.store.dispatch(consoleActions.setCompletions(source, completions)); }); + this.store.dispatch(consoleActions.setCompletions(source, completions)); } onInputShown(state) { diff --git a/src/content/actions/find.js b/src/content/actions/find.js index 249a8a9..b3d7e30 100644 --- a/src/content/actions/find.js +++ b/src/content/actions/find.js @@ -35,47 +35,45 @@ const find = (string, backwards) => { // NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work // because of same origin policy + let found = window.find(string, caseSensitive, backwards, wrapScan); + if (found) { + return found; + } + window.getSelection().removeAllRanges(); return window.find(string, caseSensitive, backwards, wrapScan); }; -const findNext = (currentKeyword, reset, backwards) => { +const findNext = async(currentKeyword, reset, backwards) => { if (reset) { window.getSelection().removeAllRanges(); } - let promise = Promise.resolve(currentKeyword); + let keyword = currentKeyword; if (currentKeyword) { browser.runtime.sendMessage({ type: messages.FIND_SET_KEYWORD, keyword: currentKeyword, }); } else { - promise = browser.runtime.sendMessage({ + keyword = await browser.runtime.sendMessage({ type: messages.FIND_GET_KEYWORD, }); } + if (!keyword) { + return postNoPrevious(); + } + let found = find(keyword, backwards); + if (found) { + postPatternFound(keyword); + } else { + postPatternNotFound(keyword); + } - return promise.then((keyword) => { - if (!keyword) { - return postNoPrevious(); - } - let found = find(keyword, backwards); - if (!found) { - window.getSelection().removeAllRanges(); - found = find(keyword, backwards); - } - if (found) { - postPatternFound(keyword); - } else { - postPatternNotFound(keyword); - } - - return { - type: actions.FIND_SET_KEYWORD, - keyword, - found, - }; - }); + return { + type: actions.FIND_SET_KEYWORD, + keyword, + found, + }; }; const next = (currentKeyword, reset) => { diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index 9b7b083..6437011 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -44,15 +44,16 @@ export default class Common { } } - reloadSettings() { - browser.runtime.sendMessage({ - type: messages.SETTINGS_QUERY, - }).then((settings) => { + async reloadSettings() { + try { + let settings = await browser.runtime.sendMessage({ + type: messages.SETTINGS_QUERY, + }); this.store.dispatch(settingActions.set(settings)); - }).catch((e) => { + } catch (e) { // Sometime sendMessage fails when background script is not ready. console.warn(e); setTimeout(() => this.reloadSettings(), 500); - }); + } } }