message passing by helper functions

jh-changes
Shin'ya Ueoka 7 years ago
parent 95c095f366
commit adcaf2aa48
  1. 27
      src/console/console.js
  2. 11
      src/content/index.js
  3. 19
      src/shared/messages.js
  4. 25
      test/shared/messages.test.js

@ -1,41 +1,42 @@
import './console.scss'; import './console.scss';
import * as messages from '../shared/messages';
const parent = window.parent; const parent = window.parent;
// TODO consider object-oriented // TODO consider object-oriented
var prevValue = ""; var prevValue = "";
const blurData = () => { const blurMessage = () => {
return JSON.stringify({ return {
type: 'vimvixen.commandline.blur' type: 'vimvixen.commandline.blur'
}); };
}; };
const keydownData = (input) => { const keydownMessage = (input) => {
return JSON.stringify({ return {
type: 'vimvixen.commandline.enter', type: 'vimvixen.commandline.enter',
value: input.value value: input.value
}); };
}; };
const keyupData = (input) => { const keyupMessage = (input) => {
return JSON.stringify({ return {
type: 'vimvixen.commandline.change', type: 'vimvixen.commandline.change',
value: input.value value: input.value
}); };
}; };
const handleBlur = () => { const handleBlur = () => {
parent.postMessage(blurData(), '*'); messages.send(parent, blurMessage());
}; };
const handleKeydown = (e) => { const handleKeydown = (e) => {
switch(e.keyCode) { switch(e.keyCode) {
case KeyboardEvent.DOM_VK_ESCAPE: case KeyboardEvent.DOM_VK_ESCAPE:
parent.postMessage(blurData(), '*'); messages.send(parent, blurMessage());
break; break;
case KeyboardEvent.DOM_VK_RETURN: case KeyboardEvent.DOM_VK_RETURN:
parent.postMessage(keydownData(e.target), '*'); messages.send(parent, keydownMessage(e.target));
break; break;
} }
}; };
@ -44,7 +45,7 @@ const handleKeyup = (e) => {
if (e.target.value === prevValue) { if (e.target.value === prevValue) {
return; return;
} }
parent.postMessage(keyupData(e.target), '*'); messages.send(parent, keyupMessage(e.target));
prevValue = e.target.value; prevValue = e.target.value;
}; };

@ -1,6 +1,7 @@
import * as scrolls from './scrolls'; import * as scrolls from './scrolls';
import * as histories from './histories'; import * as histories from './histories';
import * as actions from '../shared/actions'; import * as actions from '../shared/actions';
import * as messages from '../shared/messages';
import ConsoleFrame from '../console/console-frame'; import ConsoleFrame from '../console/console-frame';
import Follow from './follow'; import Follow from './follow';
@ -71,15 +72,7 @@ window.addEventListener("keypress", (e) => {
}); });
}); });
window.addEventListener('message', (e) => { messages.receive(window, (message) => {
let message;
try {
message = JSON.parse(e.data);
} catch (e) {
// ignore message posted by author of web page
return;
}
switch (message.type) { switch (message.type) {
case 'vimvixen.commandline.blur': case 'vimvixen.commandline.blur':
if (cmd) { if (cmd) {

@ -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 };

@ -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' });
});
});
});