diff --git a/src/console/console.js b/src/console/console.js index f8a91a6..67b9eab 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,41 +1,42 @@ import './console.scss'; +import * as messages from '../shared/messages'; const parent = window.parent; // TODO consider object-oriented var prevValue = ""; -const blurData = () => { - return JSON.stringify({ +const blurMessage = () => { + return { type: 'vimvixen.commandline.blur' - }); + }; }; -const keydownData = (input) => { - return JSON.stringify({ +const keydownMessage = (input) => { + return { type: 'vimvixen.commandline.enter', value: input.value - }); + }; }; -const keyupData = (input) => { - return JSON.stringify({ +const keyupMessage = (input) => { + return { type: 'vimvixen.commandline.change', value: input.value - }); + }; }; const handleBlur = () => { - parent.postMessage(blurData(), '*'); + messages.send(parent, blurMessage()); }; const handleKeydown = (e) => { switch(e.keyCode) { case KeyboardEvent.DOM_VK_ESCAPE: - parent.postMessage(blurData(), '*'); + messages.send(parent, blurMessage()); break; case KeyboardEvent.DOM_VK_RETURN: - parent.postMessage(keydownData(e.target), '*'); + messages.send(parent, keydownMessage(e.target)); break; } }; @@ -44,7 +45,7 @@ const handleKeyup = (e) => { if (e.target.value === prevValue) { return; } - parent.postMessage(keyupData(e.target), '*'); + messages.send(parent, keyupMessage(e.target)); prevValue = e.target.value; }; diff --git a/src/content/index.js b/src/content/index.js index 591aa98..ad3f740 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,6 +1,7 @@ import * as scrolls from './scrolls'; import * as histories from './histories'; import * as actions from '../shared/actions'; +import * as messages from '../shared/messages'; import ConsoleFrame from '../console/console-frame'; import Follow from './follow'; @@ -71,15 +72,7 @@ window.addEventListener("keypress", (e) => { }); }); -window.addEventListener('message', (e) => { - let message; - try { - message = JSON.parse(e.data); - } catch (e) { - // ignore message posted by author of web page - return; - } - +messages.receive(window, (message) => { switch (message.type) { case 'vimvixen.commandline.blur': if (cmd) { diff --git a/src/shared/messages.js b/src/shared/messages.js new file mode 100644 index 0000000..517fc4c --- /dev/null +++ b/src/shared/messages.js @@ -0,0 +1,19 @@ +const receive = (win, callback) => { + win.addEventListener('message', (e) => { + let message; + try { + message = JSON.parse(e.data); + } catch (e) { + // ignore message posted by author of web page + return; + } + + callback(message); + }) +} + +const send = (win, message) => { + win.postMessage(JSON.stringify(message), '*'); +} + +export { receive, send }; diff --git a/test/shared/messages.test.js b/test/shared/messages.test.js new file mode 100644 index 0000000..0ebaf1a --- /dev/null +++ b/test/shared/messages.test.js @@ -0,0 +1,25 @@ +import { expect } from "chai"; +import * as messages from '../../src/shared/messages'; + +describe('messages', () => { + describe('#receive', () => { + it('received a message', (done) => { + messages.receive(window, (message) => { + expect(message).to.deep.equal({ type: 'vimvixen.test' }); + done(); + }); + window.postMessage(JSON.stringify({ type: 'vimvixen.test' }), '*'); + }); + }); + + describe('#send', () => { + it('sends a message', (done) => { + window.addEventListener('message', (e) => { + let json = JSON.parse(e.data); + expect(json).to.deep.equal({ type: 'vimvixen.test' }); + done(); + }); + messages.send(window, { type: 'vimvixen.test' }); + }); + }); +});