aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures/ContentMessageListener.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-04-13 20:37:36 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2020-04-13 20:37:36 +0900
commite1dac618a8b8929f601c7ec8aca3842c5ebf9d03 (patch)
tree6a914a8243c8c02e7752a83667a54d3fa832955c /src/background/infrastructures/ContentMessageListener.ts
parent685f2b7b69218b06b5bb676069e35f79c5048c9b (diff)
Use plugin:prettier/recommended
Diffstat (limited to 'src/background/infrastructures/ContentMessageListener.ts')
-rw-r--r--src/background/infrastructures/ContentMessageListener.ts153
1 files changed, 81 insertions, 72 deletions
diff --git a/src/background/infrastructures/ContentMessageListener.ts b/src/background/infrastructures/ContentMessageListener.ts
index 2fbb9cf..6978d35 100644
--- a/src/background/infrastructures/ContentMessageListener.ts
+++ b/src/background/infrastructures/ContentMessageListener.ts
@@ -1,18 +1,18 @@
-import { injectable } from 'tsyringe';
-import * as messages from '../../shared/messages';
-import * as operations from '../../shared/operations';
-import CommandController from '../controllers/CommandController';
-import SettingController from '../controllers/SettingController';
-import FindController from '../controllers/FindController';
-import AddonEnabledController from '../controllers/AddonEnabledController';
-import LinkController from '../controllers/LinkController';
-import OperationController from '../controllers/OperationController';
-import MarkController from '../controllers/MarkController';
+import { injectable } from "tsyringe";
+import * as messages from "../../shared/messages";
+import * as operations from "../../shared/operations";
+import CommandController from "../controllers/CommandController";
+import SettingController from "../controllers/SettingController";
+import FindController from "../controllers/FindController";
+import AddonEnabledController from "../controllers/AddonEnabledController";
+import LinkController from "../controllers/LinkController";
+import OperationController from "../controllers/OperationController";
+import MarkController from "../controllers/MarkController";
import CompletionController from "../controllers/CompletionController";
@injectable()
export default class ContentMessageListener {
- private consolePorts: {[tabId: number]: browser.runtime.Port};
+ private consolePorts: { [tabId: number]: browser.runtime.Port };
constructor(
private settingController: SettingController,
@@ -22,21 +22,29 @@ export default class ContentMessageListener {
private addonEnabledController: AddonEnabledController,
private linkController: LinkController,
private operationController: OperationController,
- private markController: MarkController,
+ private markController: MarkController
) {
this.consolePorts = {};
}
run(): void {
- browser.runtime.onMessage.addListener((
- message: any, sender: browser.runtime.MessageSender,
- ) => {
- try {
- const ret = this.onMessage(message, sender.tab as browser.tabs.Tab);
- if (!(ret instanceof Promise)) {
- return {};
- }
- return ret.catch((e) => {
+ browser.runtime.onMessage.addListener(
+ (message: any, sender: browser.runtime.MessageSender) => {
+ try {
+ const ret = this.onMessage(message, sender.tab as browser.tabs.Tab);
+ if (!(ret instanceof Promise)) {
+ return {};
+ }
+ return ret.catch((e) => {
+ if (!sender.tab || !sender.tab.id) {
+ return;
+ }
+ return browser.tabs.sendMessage(sender.tab.id, {
+ type: messages.CONSOLE_SHOW_ERROR,
+ text: e.message,
+ });
+ });
+ } catch (e) {
if (!sender.tab || !sender.tab.id) {
return;
}
@@ -44,64 +52,62 @@ export default class ContentMessageListener {
type: messages.CONSOLE_SHOW_ERROR,
text: e.message,
});
- });
- } catch (e) {
- if (!sender.tab || !sender.tab.id) {
- return;
}
- return browser.tabs.sendMessage(sender.tab.id, {
- type: messages.CONSOLE_SHOW_ERROR,
- text: e.message,
- });
}
- });
+ );
browser.runtime.onConnect.addListener(this.onConnected.bind(this));
}
onMessage(
- message: messages.Message, senderTab: browser.tabs.Tab,
+ message: messages.Message,
+ senderTab: browser.tabs.Tab
): Promise<any> | any {
switch (message.type) {
- case messages.CONSOLE_GET_COMPLETION_TYPES:
- return this.completionController.getCompletionTypes();
- case messages.CONSOLE_REQUEST_SEARCH_ENGINES_MESSAGE:
- return this.completionController.requestSearchEngines(message.query);
- case messages.CONSOLE_REQUEST_BOOKMARKS:
- return this.completionController.requestBookmarks(message.query);
- case messages.CONSOLE_REQUEST_HISTORY:
- return this.completionController.requestHistory(message.query);
- case messages.CONSOLE_REQUEST_TABS:
- return this.completionController.queryTabs(message.query, message.excludePinned);
- case messages.CONSOLE_GET_PROPERTIES:
- return this.completionController.getProperties();
- case messages.CONSOLE_ENTER_COMMAND:
- return this.onConsoleEnterCommand(message.text);
- case messages.SETTINGS_QUERY:
- return this.onSettingsQuery();
- case messages.FIND_GET_KEYWORD:
- return this.onFindGetKeyword();
- case messages.FIND_SET_KEYWORD:
- return this.onFindSetKeyword(message.keyword);
- case messages.ADDON_ENABLED_RESPONSE:
- return this.onAddonEnabledResponse(message.enabled);
- case messages.OPEN_URL:
- return this.onOpenUrl(
- message.newTab,
- message.url,
- senderTab.id as number,
- message.background);
- case messages.BACKGROUND_OPERATION:
- return this.onBackgroundOperation(message.repeat, message.operation);
- case messages.MARK_SET_GLOBAL:
- 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(
- senderTab.id as number, message.message,
- );
+ case messages.CONSOLE_GET_COMPLETION_TYPES:
+ return this.completionController.getCompletionTypes();
+ case messages.CONSOLE_REQUEST_SEARCH_ENGINES_MESSAGE:
+ return this.completionController.requestSearchEngines(message.query);
+ case messages.CONSOLE_REQUEST_BOOKMARKS:
+ return this.completionController.requestBookmarks(message.query);
+ case messages.CONSOLE_REQUEST_HISTORY:
+ return this.completionController.requestHistory(message.query);
+ case messages.CONSOLE_REQUEST_TABS:
+ return this.completionController.queryTabs(
+ message.query,
+ message.excludePinned
+ );
+ case messages.CONSOLE_GET_PROPERTIES:
+ return this.completionController.getProperties();
+ case messages.CONSOLE_ENTER_COMMAND:
+ return this.onConsoleEnterCommand(message.text);
+ case messages.SETTINGS_QUERY:
+ return this.onSettingsQuery();
+ case messages.FIND_GET_KEYWORD:
+ return this.onFindGetKeyword();
+ case messages.FIND_SET_KEYWORD:
+ return this.onFindSetKeyword(message.keyword);
+ case messages.ADDON_ENABLED_RESPONSE:
+ return this.onAddonEnabledResponse(message.enabled);
+ case messages.OPEN_URL:
+ return this.onOpenUrl(
+ message.newTab,
+ message.url,
+ senderTab.id as number,
+ message.background
+ );
+ case messages.BACKGROUND_OPERATION:
+ return this.onBackgroundOperation(message.repeat, message.operation);
+ case messages.MARK_SET_GLOBAL:
+ 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(
+ senderTab.id as number,
+ message.message
+ );
}
- throw new Error('unsupported message: ' + message.type);
+ throw new Error("unsupported message: " + message.type);
}
onConsoleEnterCommand(text: string): Promise<any> {
@@ -125,7 +131,10 @@ export default class ContentMessageListener {
}
onOpenUrl(
- newTab: boolean, url: string, openerId: number, background: boolean,
+ newTab: boolean,
+ url: string,
+ openerId: number,
+ background: boolean
): Promise<any> {
if (newTab) {
return this.linkController.openNewTab(url, openerId, background);
@@ -154,7 +163,7 @@ export default class ContentMessageListener {
}
onConnected(port: browser.runtime.Port): void {
- if (port.name !== 'vimvixen-console') {
+ if (port.name !== "vimvixen-console") {
return;
}