diff --git a/src/console/console-frame.js b/src/console/console-frame.js index cf72f25..f8aeeeb 100644 --- a/src/console/console-frame.js +++ b/src/console/console-frame.js @@ -1,19 +1,43 @@ import './console-frame.scss'; +import * as messages from '../shared/messages'; export default class ConsoleFrame { constructor(win, initial = '') { - let url = browser.runtime.getURL('build/console.html') + - '#' + encodeURIComponent(initial); - let element = window.document.createElement('iframe'); - element.src = url; + element.src = browser.runtime.getURL('build/console.html'); element.className = 'vimvixen-console-frame'; win.document.body.append(element); this.element = element; + + this.hide(); + } + + showCommand(text) { + this.showFrame(); + + let message = { + type: 'vimvixen.console.show.command', + text: text + }; + messages.send(this.element.contentWindow, message); + } + + showError(text) { + this.showFrame(); + + let message = { + type: 'vimvixen.console.show.error', + text: text + }; + messages.send(this.element.contentWindow, message); + } + + showFrame() { + this.element.style.display = 'block'; } - remove() { - this.element.remove(); + hide() { + this.element.style.display = 'none'; } } diff --git a/src/console/console.html b/src/console/console.html index feee5d0..228660b 100644 --- a/src/console/console.html +++ b/src/console/console.html @@ -6,7 +6,7 @@ -

diff --git a/src/console/console.js b/src/console/console.js index 67b9eab..2db9d29 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -50,16 +50,23 @@ const handleKeyup = (e) => { }; window.addEventListener('load', () => { - let hash = window.location.hash; - let initial = ''; - if (hash.length > 0) { - initial = decodeURIComponent(hash.substring(1)); - } - let input = window.document.querySelector('#vimvixen-console-command-input'); input.addEventListener('blur', handleBlur); input.addEventListener('keydown', handleKeydown); input.addEventListener('keyup', handleKeyup); - input.value = initial; - input.focus(); +}); + +messages.receive(window, (message) => { + switch (message.type) { + case 'vimvixen.console.show.command': + if (message.text) { + let input = window.document.querySelector('#vimvixen-console-command-input'); + input.value = message.text; + input.focus(); + } + break; + case 'vimvixen.console.show.error': + window.document.querySelector('#vimvixen-console-error').textContent = message.text; + break; + } }); diff --git a/src/content/index.js b/src/content/index.js index ad3f740..66b1121 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -5,7 +5,7 @@ import * as messages from '../shared/messages'; import ConsoleFrame from '../console/console-frame'; import Follow from './follow'; -let cmd = null; +let vvConsole = new ConsoleFrame(window); const invokeEvent = (action) => { if (typeof action === 'undefined' || action === null) { @@ -14,14 +14,14 @@ const invokeEvent = (action) => { switch (action[0]) { case actions.CMD_OPEN: - cmd = new ConsoleFrame(window); + vvConsole.showCommand(''); break; case actions.CMD_TABS_OPEN: if (action[1] || false) { // alter url - cmd = new ConsoleFrame(window, 'open ' + window.location.href); + vvConsole.showCommand('open ' + window.location.href); } else { - cmd = new ConsoleFrame(window, 'open '); + vvConsole.showCommand('open '); } break; case actions.SCROLL_LINES: @@ -75,10 +75,7 @@ window.addEventListener("keypress", (e) => { messages.receive(window, (message) => { switch (message.type) { case 'vimvixen.commandline.blur': - if (cmd) { - cmd.remove(); - cmd = null; - } + vvConsole.hide(); break; case 'vimvixen.commandline.enter': browser.runtime.sendMessage({