From 68753b3660f0d6996c9b5a3c433d2cdc275dd4e4 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 2 Sep 2017 21:48:26 +0900 Subject: [PATCH] response by type --- src/background/index.js | 5 ++++- src/content/index.js | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/background/index.js b/src/background/index.js index d8047a6..8f2e44c 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -17,7 +17,10 @@ const keyPressHandle = (request, sender, sendResponse) => { if (actions.isBackgroundAction(action[0])) { doBackgroundAction(sender, action); } else if (actions.isContentAction(action[0])) { - sendResponse(action); + sendResponse({ + type: 'response.action', + action: action + }); } }; diff --git a/src/content/index.js b/src/content/index.js index 731d6a5..12251a8 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -7,7 +7,7 @@ import Follow from './follow'; let vvConsole = new ConsoleFrame(window); -const invokeEvent = (action) => { +const doAction = (action) => { if (typeof action === 'undefined' || action === null) { return; } @@ -57,6 +57,18 @@ const invokeEvent = (action) => { } } +const handleResponse = (response) => { + if (!response) { + return; + } + + switch(response.type) { + case 'response.action': + doAction(response.action); + break; + } +}; + window.addEventListener("keypress", (e) => { if (e.target instanceof HTMLInputElement) { return; @@ -69,10 +81,10 @@ window.addEventListener("keypress", (e) => { } browser.runtime.sendMessage(request) - .then(invokeEvent, - (err) => { - console.log(`Vim Vixen: ${err}`); - }); + .then(handleResponse) + .catch((err) => { + console.log(`Vim Vixen: ${err}`); + }); }); messages.receive(window, (message) => {