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. 27
      src/content/console-frames.js
  7. 3
      src/shared/messages.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;
}
}

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

@ -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 {

@ -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(

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

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

@ -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,