diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js index beb52fe..aae07c0 100644 --- a/src/background/infrastructures/content-message-listener.js +++ b/src/background/infrastructures/content-message-listener.js @@ -16,6 +16,8 @@ export default class ContentMessageListener { this.linkController = new LinkController(); this.backgroundOperationController = new OperationController(); this.markController = new MarkController(); + + this.consolePorts = {}; } run() { @@ -38,6 +40,7 @@ export default class ContentMessageListener { }); } }); + browser.runtime.onConnect.addListener(this.onConnected.bind(this)); } onMessage(message, sender) { @@ -65,6 +68,8 @@ export default class ContentMessageListener { return this.onMarkSetGlobal(message.key, message.x, message.y); case messages.MARK_JUMP_GLOBAL: return this.onMarkJumpGlobal(message.key); + case messages.CONSOLE_FRAME_MESSAGE: + return this.onConsoleFrameMessage(sender.tab.id, message.message); } } @@ -116,4 +121,21 @@ export default class ContentMessageListener { onMarkJumpGlobal(key) { return this.markController.jumpGlobal(key); } + + onConsoleFrameMessage(tabId, message) { + let port = this.consolePorts[tabId]; + if (!port) { + return; + } + port.postMessage(message); + } + + onConnected(port) { + if (port.name !== 'vimvixen-console') { + return; + } + + let id = port.sender.tab.id; + this.consolePorts[id] = port; + } } diff --git a/src/console/index.jsx b/src/console/index.jsx index c0d1807..dfd323e 100644 --- a/src/console/index.jsx +++ b/src/console/index.jsx @@ -38,6 +38,5 @@ const onMessage = (message) => { }; browser.runtime.onMessage.addListener(onMessage); -window.addEventListener('message', (event) => { - onMessage(JSON.parse(event.data)); -}, false); +let port = browser.runtime.connect({ name: 'vimvixen-console' }); +port.onMessage.addListener(onMessage); diff --git a/src/content/actions/find.js b/src/content/actions/find.js index b3d7e30..e08d7e5 100644 --- a/src/content/actions/find.js +++ b/src/content/actions/find.js @@ -9,25 +9,6 @@ import messages from 'shared/messages'; import actions from 'content/actions'; import * as consoleFrames from '../console-frames'; -const postPatternNotFound = (pattern) => { - return consoleFrames.postError( - window.document, - 'Pattern not found: ' + pattern); -}; - -const postPatternFound = (pattern) => { - return consoleFrames.postInfo( - window.document, - 'Pattern found: ' + pattern, - ); -}; - -const postNoPrevious = () => { - return consoleFrames.postError( - window.document, - 'No previous search keywords'); -}; - const find = (string, backwards) => { let caseSensitive = false; let wrapScan = true; @@ -60,13 +41,13 @@ const findNext = async(currentKeyword, reset, backwards) => { }); } if (!keyword) { - return postNoPrevious(); + return consoleFrames.postError('No previous search keywords'); } let found = find(keyword, backwards); if (found) { - postPatternFound(keyword); + consoleFrames.postInfo('Pattern found: ' + keyword); } else { - postPatternNotFound(keyword); + consoleFrames.postError('Pattern not found: ' + keyword); } return { diff --git a/src/content/actions/operation.js b/src/content/actions/operation.js index 1aeb8be..b96c6b9 100644 --- a/src/content/actions/operation.js +++ b/src/content/actions/operation.js @@ -85,7 +85,7 @@ const exec = (operation, repeat, settings, addonEnabled) => { break; case operations.URLS_YANK: urls.yank(window); - consoleFrames.postInfo(window.document, 'Current url yanked'); + consoleFrames.postInfo('Current url yanked'); break; case operations.URLS_PASTE: urls.paste( diff --git a/src/content/components/common/mark.js b/src/content/components/common/mark.js index 1ed636b..0f838a9 100644 --- a/src/content/components/common/mark.js +++ b/src/content/components/common/mark.js @@ -33,7 +33,7 @@ export default class MarkComponent { } if (key.ctrlKey || key.metaKey || key.altKey) { - consoleFrames.postError(window.document, 'Unknown mark'); + consoleFrames.postError('Unknown mark'); } else if (globalKey(key.key) && markStage.setMode) { this.doSetGlobal(key); } else if (globalKey(key.key) && markStage.jumpMode) { @@ -55,7 +55,7 @@ export default class MarkComponent { doJump(marks, key, smoothscroll) { if (!marks[key.key]) { - consoleFrames.postError(window.document, 'Mark is not set'); + consoleFrames.postError('Mark is not set'); return; } diff --git a/src/content/console-frames.js b/src/content/console-frames.js index 0c0ec02..401765c 100644 --- a/src/content/console-frames.js +++ b/src/content/console-frames.js @@ -16,22 +16,23 @@ const blur = (doc) => { iframe.blur(); }; -const postMessage = (doc, message) => { - let iframe = doc.getElementById('vimvixen-console-frame'); - iframe.contentWindow.postMessage(JSON.stringify(message), '*'); -}; - -const postError = (doc, message) => { - return postMessage(doc, { - type: messages.CONSOLE_SHOW_ERROR, - text: message, +const postError = (text) => { + browser.runtime.sendMessage({ + type: messages.CONSOLE_FRAME_MESSAGE, + message: { + type: messages.CONSOLE_SHOW_ERROR, + text, + }, }); }; -const postInfo = (doc, message) => { - return postMessage(doc, { - type: messages.CONSOLE_SHOW_INFO, - text: message, +const postInfo = (text) => { + browser.runtime.sendMessage({ + type: messages.CONSOLE_FRAME_MESSAGE, + message: { + type: messages.CONSOLE_SHOW_INFO, + text, + }, }); }; diff --git a/src/shared/messages.js b/src/shared/messages.js index dad2b7a..e86600e 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -63,6 +63,9 @@ export default { SETTINGS_CHANGED: 'settings.changed', SETTINGS_QUERY: 'settings.query', + WINDOW_TOP_MESSAGE: 'window.top.message', + CONSOLE_FRAME_MESSAGE: 'console.frame.message', + onWebMessage, onBackgroundMessage, onMessage,