Use webextension message to console

jh-changes
Shin'ya Ueoka 6 years ago
parent 8220336130
commit 63b93ce1ca
  1. 22
      src/background/infrastructures/content-message-listener.js
  2. 5
      src/console/index.jsx
  3. 25
      src/content/actions/find.js
  4. 2
      src/content/actions/operation.js
  5. 4
      src/content/components/common/mark.js
  6. 23
      src/content/console-frames.js
  7. 3
      src/shared/messages.js

@ -16,6 +16,8 @@ export default class ContentMessageListener {
this.linkController = new LinkController(); this.linkController = new LinkController();
this.backgroundOperationController = new OperationController(); this.backgroundOperationController = new OperationController();
this.markController = new MarkController(); this.markController = new MarkController();
this.consolePorts = {};
} }
run() { run() {
@ -38,6 +40,7 @@ export default class ContentMessageListener {
}); });
} }
}); });
browser.runtime.onConnect.addListener(this.onConnected.bind(this));
} }
onMessage(message, sender) { onMessage(message, sender) {
@ -65,6 +68,8 @@ export default class ContentMessageListener {
return this.onMarkSetGlobal(message.key, message.x, message.y); return this.onMarkSetGlobal(message.key, message.x, message.y);
case messages.MARK_JUMP_GLOBAL: case messages.MARK_JUMP_GLOBAL:
return this.onMarkJumpGlobal(message.key); 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) { onMarkJumpGlobal(key) {
return this.markController.jumpGlobal(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;
}
} }

@ -38,6 +38,5 @@ const onMessage = (message) => {
}; };
browser.runtime.onMessage.addListener(onMessage); browser.runtime.onMessage.addListener(onMessage);
window.addEventListener('message', (event) => { let port = browser.runtime.connect({ name: 'vimvixen-console' });
onMessage(JSON.parse(event.data)); port.onMessage.addListener(onMessage);
}, false);

@ -9,25 +9,6 @@ import messages from 'shared/messages';
import actions from 'content/actions'; import actions from 'content/actions';
import * as consoleFrames from '../console-frames'; 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) => { const find = (string, backwards) => {
let caseSensitive = false; let caseSensitive = false;
let wrapScan = true; let wrapScan = true;
@ -60,13 +41,13 @@ const findNext = async(currentKeyword, reset, backwards) => {
}); });
} }
if (!keyword) { if (!keyword) {
return postNoPrevious(); return consoleFrames.postError('No previous search keywords');
} }
let found = find(keyword, backwards); let found = find(keyword, backwards);
if (found) { if (found) {
postPatternFound(keyword); consoleFrames.postInfo('Pattern found: ' + keyword);
} else { } else {
postPatternNotFound(keyword); consoleFrames.postError('Pattern not found: ' + keyword);
} }
return { return {

@ -85,7 +85,7 @@ const exec = (operation, repeat, settings, addonEnabled) => {
break; break;
case operations.URLS_YANK: case operations.URLS_YANK:
urls.yank(window); urls.yank(window);
consoleFrames.postInfo(window.document, 'Current url yanked'); consoleFrames.postInfo('Current url yanked');
break; break;
case operations.URLS_PASTE: case operations.URLS_PASTE:
urls.paste( urls.paste(

@ -33,7 +33,7 @@ export default class MarkComponent {
} }
if (key.ctrlKey || key.metaKey || key.altKey) { if (key.ctrlKey || key.metaKey || key.altKey) {
consoleFrames.postError(window.document, 'Unknown mark'); consoleFrames.postError('Unknown mark');
} else if (globalKey(key.key) && markStage.setMode) { } else if (globalKey(key.key) && markStage.setMode) {
this.doSetGlobal(key); this.doSetGlobal(key);
} else if (globalKey(key.key) && markStage.jumpMode) { } else if (globalKey(key.key) && markStage.jumpMode) {
@ -55,7 +55,7 @@ export default class MarkComponent {
doJump(marks, key, smoothscroll) { doJump(marks, key, smoothscroll) {
if (!marks[key.key]) { if (!marks[key.key]) {
consoleFrames.postError(window.document, 'Mark is not set'); consoleFrames.postError('Mark is not set');
return; return;
} }

@ -16,22 +16,23 @@ const blur = (doc) => {
iframe.blur(); iframe.blur();
}; };
const postMessage = (doc, message) => { const postError = (text) => {
let iframe = doc.getElementById('vimvixen-console-frame'); browser.runtime.sendMessage({
iframe.contentWindow.postMessage(JSON.stringify(message), '*'); type: messages.CONSOLE_FRAME_MESSAGE,
}; message: {
const postError = (doc, message) => {
return postMessage(doc, {
type: messages.CONSOLE_SHOW_ERROR, type: messages.CONSOLE_SHOW_ERROR,
text: message, text,
},
}); });
}; };
const postInfo = (doc, message) => { const postInfo = (text) => {
return postMessage(doc, { browser.runtime.sendMessage({
type: messages.CONSOLE_FRAME_MESSAGE,
message: {
type: messages.CONSOLE_SHOW_INFO, type: messages.CONSOLE_SHOW_INFO,
text: message, text,
},
}); });
}; };

@ -63,6 +63,9 @@ export default {
SETTINGS_CHANGED: 'settings.changed', SETTINGS_CHANGED: 'settings.changed',
SETTINGS_QUERY: 'settings.query', SETTINGS_QUERY: 'settings.query',
WINDOW_TOP_MESSAGE: 'window.top.message',
CONSOLE_FRAME_MESSAGE: 'console.frame.message',
onWebMessage, onWebMessage,
onBackgroundMessage, onBackgroundMessage,
onMessage, onMessage,