diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-02-11 15:40:34 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-02-12 22:04:55 +0900 |
commit | 3f8bbe11d96b751affa9e2bf0a1995008f9e3220 (patch) | |
tree | 70a6f389331ca93bf51b4a5e46a1833817a4bfcf /e2e/ambassador/src/content | |
parent | cf03d278eb8bb8f7544ae6395b76e1a1e0781053 (diff) |
send press key to tabs
Diffstat (limited to 'e2e/ambassador/src/content')
-rw-r--r-- | e2e/ambassador/src/content/index.js | 30 | ||||
-rw-r--r-- | e2e/ambassador/src/content/ipc.js | 13 |
2 files changed, 38 insertions, 5 deletions
diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js index dd46688..0931f93 100644 --- a/e2e/ambassador/src/content/index.js +++ b/e2e/ambassador/src/content/index.js @@ -1,11 +1,35 @@ -import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import { + WINDOWS_CREATE, WINDOWS_REMOVE, TABS_CREATE, + EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, +} from '../shared/messages'; import * as ipc from './ipc'; ipc.receivePageMessage((message) => { switch (message.type) { case WINDOWS_CREATE: - return ipc.send(message); case WINDOWS_REMOVE: - return ipc.send(message); + case TABS_CREATE: + case EVENT_KEYPRESS: + case EVENT_KEYDOWN: + case EVENT_KEYUP: + return ipc.sendToBackground(message); } }); + +ipc.receiveBackgroundMesssage((message) => { + switch (message.type) { + case EVENT_KEYPRESS: + document.body.dispatchEvent( + new KeyboardEvent('keypress', { 'key': message.key })); + break; + case EVENT_KEYDOWN: + document.body.dispatchEvent( + new KeyboardEvent('keydown', { 'key': message.key })); + break; + case EVENT_KEYUP: + document.body.dispatchEvent( + new KeyboardEvent('keyup', { 'key': message.key })); + break; + } + return Promise.resolve({}); +}); diff --git a/e2e/ambassador/src/content/ipc.js b/e2e/ambassador/src/content/ipc.js index 62fcac1..917623c 100644 --- a/e2e/ambassador/src/content/ipc.js +++ b/e2e/ambassador/src/content/ipc.js @@ -1,9 +1,15 @@ import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages'; -const send = (message) => { +const sendToBackground = (message) => { return browser.runtime.sendMessage(message); }; +const receiveBackgroundMesssage = (func) => { + return browser.runtime.onMessage.addListener((message) => { + return Promise.resolve(func(message)); + }); +}; + const receivePageMessage = (func) => { window.addEventListener('message', (e) => { let packet = e.data; @@ -28,4 +34,7 @@ const receivePageMessage = (func) => { }); }; -export { send, receivePageMessage }; +export { + sendToBackground, receiveBackgroundMesssage, + receivePageMessage, +}; |