From 300f0ba04b88504a26d417e728035ea49ed235a8 Mon Sep 17 00:00:00 2001 From: tsukkee Date: Tue, 12 May 2020 02:46:57 +0900 Subject: [PATCH 1/6] Prototyping to browser.runtime.sendMessage() --- src/content/controllers/KeymapController.ts | 4 ++++ src/shared/operations.ts | 25 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index 092e55ce..50302ccf 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -48,6 +48,10 @@ export default class KeymapController { return () => this.addonEnabledUseCase.disable(); case operations.ADDON_TOGGLE_ENABLED: return () => this.addonEnabledUseCase.toggle(); + case operations.ADDON_SENDMESSAGE: + return () => { + browser.runtime.sendMessage(op.extensionId, op.message); + }; case operations.FIND_NEXT: return () => this.findSlaveUseCase.findNext(); case operations.FIND_PREV: diff --git a/src/shared/operations.ts b/src/shared/operations.ts index 35445020..c2a1091a 100644 --- a/src/shared/operations.ts +++ b/src/shared/operations.ts @@ -5,6 +5,7 @@ export const CANCEL = "cancel"; export const ADDON_ENABLE = "addon.enable"; export const ADDON_DISABLE = "addon.disable"; export const ADDON_TOGGLE_ENABLED = "addon.toggle.enabled"; +export const ADDON_SENDMESSAGE = "addon.sendmessage"; // Command export const COMMAND_SHOW = "command.show"; @@ -97,6 +98,12 @@ export interface AddonToggleEnabledOperation { type: typeof ADDON_TOGGLE_ENABLED; } +export interface AddonSendmessageOperation { + type: typeof ADDON_SENDMESSAGE; + extensionId: string; + message: string | object; +} + export interface CommandShowOperation { type: typeof COMMAND_SHOW; } @@ -315,6 +322,7 @@ export type Operation = | AddonEnableOperation | AddonDisableOperation | AddonToggleEnabledOperation + | AddonSendmessageOperation | CommandShowOperation | CommandShowOpenOperation | CommandShowTabopenOperation @@ -409,12 +417,29 @@ const assertRequiredString = (obj: any, name: string) => { } }; +const assertRequiredObjectOrString = (obj: any, name: string) => { + if ( + !Object.prototype.hasOwnProperty.call(obj, name) || + !(typeof obj[name] === "string" || typeof obj[name] == "object") + ) { + throw new TypeError(`Missing object or string parameter: '${name}`); + } +}; + // eslint-disable-next-line complexity, max-lines-per-function export const valueOf = (o: any): Operation => { if (!Object.prototype.hasOwnProperty.call(o, "type")) { throw new TypeError(`Missing 'type' field`); } switch (o.type) { + case ADDON_SENDMESSAGE: + assertRequiredString(o, "extensionId"); + assertRequiredObjectOrString(o, "message"); + return { + type: o.type, + extensionId: o.extensionId, + message: o.message + }; case COMMAND_SHOW_OPEN: case COMMAND_SHOW_TABOPEN: case COMMAND_SHOW_WINOPEN: From 47bb0969cf0c4efa299b3d16f2ea20baa77531b3 Mon Sep 17 00:00:00 2001 From: tsukkee Date: Thu, 14 May 2020 02:44:44 +0900 Subject: [PATCH 2/6] Create the use case class for sendMessage() --- src/content/controllers/KeymapController.ts | 6 +++--- src/content/usecases/AddonSendmessageUseCase.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 src/content/usecases/AddonSendmessageUseCase.ts diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index 50302ccf..ecb27cc8 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -2,6 +2,7 @@ import { injectable, inject } from "tsyringe"; import * as operations from "../../shared/operations"; import KeymapUseCase from "../usecases/KeymapUseCase"; import AddonEnabledUseCase from "../usecases/AddonEnabledUseCase"; +import AddonSendmessageUseCase from "../usecases/AddonSendmessageUseCase"; import FindSlaveUseCase from "../usecases/FindSlaveUseCase"; import ScrollUseCase from "../usecases/ScrollUseCase"; import FocusUseCase from "../usecases/FocusUseCase"; @@ -16,6 +17,7 @@ export default class KeymapController { constructor( private keymapUseCase: KeymapUseCase, private addonEnabledUseCase: AddonEnabledUseCase, + private addonSendmessageUseCase: AddonSendmessageUseCase, private findSlaveUseCase: FindSlaveUseCase, private scrollUseCase: ScrollUseCase, private focusUseCase: FocusUseCase, @@ -49,9 +51,7 @@ export default class KeymapController { case operations.ADDON_TOGGLE_ENABLED: return () => this.addonEnabledUseCase.toggle(); case operations.ADDON_SENDMESSAGE: - return () => { - browser.runtime.sendMessage(op.extensionId, op.message); - }; + return () => this.addonSendmessageUseCase.sendMessage(op.extensionId, op.message); case operations.FIND_NEXT: return () => this.findSlaveUseCase.findNext(); case operations.FIND_PREV: diff --git a/src/content/usecases/AddonSendmessageUseCase.ts b/src/content/usecases/AddonSendmessageUseCase.ts new file mode 100644 index 00000000..d46f7f42 --- /dev/null +++ b/src/content/usecases/AddonSendmessageUseCase.ts @@ -0,0 +1,15 @@ +import { injectable} from "tsyringe"; + + +@injectable() +export default class AddonSendmessageUseCase { + constructor() { } + + async sendMessage(extensionId: string, message: any) { + const sending = browser.runtime.sendMessage(extensionId, message); + sending.catch((reason: any) => { + throw new Error(reason); + }); + return sending; + } +} \ No newline at end of file From 2abaaa2f6936ec983e9776a21abbb4373adcd0f2 Mon Sep 17 00:00:00 2001 From: tsukkee Date: Thu, 11 Jun 2020 23:45:22 +0900 Subject: [PATCH 3/6] improve error reporting --- src/content/controllers/KeymapController.ts | 3 ++- src/content/usecases/AddonSendmessageUseCase.ts | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index ecb27cc8..45cf2e1e 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -51,7 +51,8 @@ export default class KeymapController { case operations.ADDON_TOGGLE_ENABLED: return () => this.addonEnabledUseCase.toggle(); case operations.ADDON_SENDMESSAGE: - return () => this.addonSendmessageUseCase.sendMessage(op.extensionId, op.message); + return () => + this.addonSendmessageUseCase.sendMessage(op.extensionId, op.message); case operations.FIND_NEXT: return () => this.findSlaveUseCase.findNext(); case operations.FIND_PREV: diff --git a/src/content/usecases/AddonSendmessageUseCase.ts b/src/content/usecases/AddonSendmessageUseCase.ts index d46f7f42..d0b6599f 100644 --- a/src/content/usecases/AddonSendmessageUseCase.ts +++ b/src/content/usecases/AddonSendmessageUseCase.ts @@ -1,15 +1,17 @@ -import { injectable} from "tsyringe"; - +import { injectable, inject } from "tsyringe"; +import ConsoleClient from "../client/ConsoleClient"; @injectable() export default class AddonSendmessageUseCase { - constructor() { } + constructor( + @inject("ConsoleClient") private consoleClient: ConsoleClient + ) {} async sendMessage(extensionId: string, message: any) { const sending = browser.runtime.sendMessage(extensionId, message); sending.catch((reason: any) => { - throw new Error(reason); + this.consoleClient.error(`Error on sending message to ${extensionId}: ${reason}`); }); return sending; } -} \ No newline at end of file +} From 70bbde7bcf73a7fb87f2ab6df4da18ddf97e5948 Mon Sep 17 00:00:00 2001 From: tsukkee Date: Tue, 25 Aug 2020 14:30:16 +0900 Subject: [PATCH 4/6] add document for sendMessage --- docs/_layouts/default.html | 1 + docs/sendmessage.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 docs/sendmessage.md diff --git a/docs/_layouts/default.html b/docs/_layouts/default.html index 0569452f..988b2d7b 100644 --- a/docs/_layouts/default.html +++ b/docs/_layouts/default.html @@ -37,6 +37,7 @@

Guide

  • Blacklist
  • Search engines
  • Properties
  • +
  • Properties
  • diff --git a/docs/sendmessage.md b/docs/sendmessage.md new file mode 100644 index 00000000..d45029e6 --- /dev/null +++ b/docs/sendmessage.md @@ -0,0 +1,33 @@ +--- +title: Send Message +--- + +# Send Message + +Vim Vixen can send messages to other add-on. + +Note that, currently this feature can be set only when using "Use plain JSON". + +## Example + +Following example enables to toggle collapsed state of [Tree Style Tab]()'s active tab by pressing zc. You can find complete API reference on [Tree Style Tab's API reference](https://github.com/piroor/treestyletab/wiki/API-for-other-addons). + +```json +{ + "keymaps": { + "zc": { + "type": "addon.sendmessage", + "extensionId": "treestyletab@piro.sakura.ne.jp", + "message": { + "type": "toggle-tree-collapsed", + "tab": "active" + } + } + } +} +``` + +## Misc + +You may want to see [Wiki page for the same function on Gesturefy](https://twitter.com/tomo_ahm/status/1297849816907575296). + From 51d8f17be59ab0b37f6c731d7711c05a5fbfd7a9 Mon Sep 17 00:00:00 2001 From: tsukkee Date: Tue, 25 Aug 2020 15:18:20 +0900 Subject: [PATCH 5/6] apply eslint fix --- src/content/controllers/KeymapController.ts | 5 ++++- .../usecases/AddonSendmessageUseCase.ts | 20 +++++++++---------- src/shared/operations.ts | 4 ++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index 45cf2e1e..72ef46d9 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -52,7 +52,10 @@ export default class KeymapController { return () => this.addonEnabledUseCase.toggle(); case operations.ADDON_SENDMESSAGE: return () => - this.addonSendmessageUseCase.sendMessage(op.extensionId, op.message); + this.addonSendmessageUseCase.sendMessage( + op.extensionId, + op.message + ); case operations.FIND_NEXT: return () => this.findSlaveUseCase.findNext(); case operations.FIND_PREV: diff --git a/src/content/usecases/AddonSendmessageUseCase.ts b/src/content/usecases/AddonSendmessageUseCase.ts index d0b6599f..b5d50d3b 100644 --- a/src/content/usecases/AddonSendmessageUseCase.ts +++ b/src/content/usecases/AddonSendmessageUseCase.ts @@ -3,15 +3,15 @@ import ConsoleClient from "../client/ConsoleClient"; @injectable() export default class AddonSendmessageUseCase { - constructor( - @inject("ConsoleClient") private consoleClient: ConsoleClient - ) {} + constructor(@inject("ConsoleClient") private consoleClient: ConsoleClient) {} - async sendMessage(extensionId: string, message: any) { - const sending = browser.runtime.sendMessage(extensionId, message); - sending.catch((reason: any) => { - this.consoleClient.error(`Error on sending message to ${extensionId}: ${reason}`); - }); - return sending; - } + async sendMessage(extensionId: string, message: any) { + const sending = browser.runtime.sendMessage(extensionId, message); + sending.catch((reason: any) => { + this.consoleClient.error( + `Error on sending message to ${extensionId}: ${reason}` + ); + }); + return sending; + } } diff --git a/src/shared/operations.ts b/src/shared/operations.ts index c2a1091a..21653df6 100644 --- a/src/shared/operations.ts +++ b/src/shared/operations.ts @@ -101,7 +101,7 @@ export interface AddonToggleEnabledOperation { export interface AddonSendmessageOperation { type: typeof ADDON_SENDMESSAGE; extensionId: string; - message: string | object; + message: string | Record; } export interface CommandShowOperation { @@ -438,7 +438,7 @@ export const valueOf = (o: any): Operation => { return { type: o.type, extensionId: o.extensionId, - message: o.message + message: o.message, }; case COMMAND_SHOW_OPEN: case COMMAND_SHOW_TABOPEN: From 1dc769e6e4f693f4eeaa5f4af1fd069ffd81c09d Mon Sep 17 00:00:00 2001 From: tsukkee Date: Tue, 25 Aug 2020 15:36:33 +0900 Subject: [PATCH 6/6] modify docs --- docs/_layouts/default.html | 2 +- docs/sendmessage.md | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/_layouts/default.html b/docs/_layouts/default.html index 988b2d7b..f72b5525 100644 --- a/docs/_layouts/default.html +++ b/docs/_layouts/default.html @@ -37,7 +37,7 @@

    Guide

  • Blacklist
  • Search engines
  • Properties
  • -
  • Properties
  • +
  • SendMessage
  • diff --git a/docs/sendmessage.md b/docs/sendmessage.md index d45029e6..50a38b33 100644 --- a/docs/sendmessage.md +++ b/docs/sendmessage.md @@ -1,16 +1,16 @@ --- -title: Send Message +title: SendMessage --- -# Send Message +# SendMessage -Vim Vixen can send messages to other add-on. +Vim Vixen can send messages to other add-ons to controll their functionality by keyboard, if they support. To use this feature, you need to specify `addon.sendmessage` as `type`, `extensionId` and `message` (this can be string or object) for keymaps. Note that, currently this feature can be set only when using "Use plain JSON". ## Example -Following example enables to toggle collapsed state of [Tree Style Tab]()'s active tab by pressing zc. You can find complete API reference on [Tree Style Tab's API reference](https://github.com/piroor/treestyletab/wiki/API-for-other-addons). +Following example enables to toggle collapsed state of [Tree Style Tab](https://addons.mozilla.org/firefox/addon/tree-style-tab/)'s active tab by pressing zc. This kind of API might be described in add-ons' web site, for example you can find Tree Style Tab's API reference [here](https://github.com/piroor/treestyletab/wiki/API-for-other-addons). ```json { @@ -27,7 +27,5 @@ Following example enables to toggle collapsed state of [Tree Style Tab]()'s acti } ``` -## Misc - -You may want to see [Wiki page for the same function on Gesturefy](https://twitter.com/tomo_ahm/status/1297849816907575296). +You may want to see [the Wiki page for the same feature on Gesturefy](https://twitter.com/tomo_ahm/status/1297849816907575296) for more example.