diff options
Diffstat (limited to 'src/content')
-rw-r--r-- | src/content/Application.ts | 10 | ||||
-rw-r--r-- | src/content/MessageListener.ts | 7 | ||||
-rw-r--r-- | src/content/controllers/FindController.ts | 19 | ||||
-rw-r--r-- | src/content/di.ts | 2 | ||||
-rw-r--r-- | src/content/presenters/FindPresenter.ts | 23 | ||||
-rw-r--r-- | src/content/usecases/FindUseCase.ts | 22 |
6 files changed, 81 insertions, 2 deletions
diff --git a/src/content/Application.ts b/src/content/Application.ts index 9ea047c..a12c3c6 100644 --- a/src/content/Application.ts +++ b/src/content/Application.ts @@ -13,6 +13,7 @@ import SettingController from "./controllers/SettingController"; import ConsoleFrameController from "./controllers/ConsoleFrameController"; import NavigateController from "./controllers/NavigateController"; import * as messages from "../shared/messages"; +import FindController from "./controllers/FindController"; type Message = messages.Message; @@ -31,7 +32,8 @@ export default class Application { private addonEnabledController: AddonEnabledController, private settingController: SettingController, private consoleFrameController: ConsoleFrameController, - private navigateController: NavigateController + private navigateController: NavigateController, + private findController: FindController ) {} init(): Promise<void> { @@ -91,6 +93,12 @@ export default class Application { return this.navigateController.openLinkPrev(msg); case messages.CONSOLE_RESIZE: return this.consoleFrameController.resize(msg); + case messages.FIND_NEXT: + return this.findController.findNext(msg.keyword); + case messages.FIND_PREV: + return this.findController.findPrev(msg.keyword); + case messages.FIND_CLEAR_SELECTION: + return this.findController.clearSelection(); } if (window.self === window.top) { diff --git a/src/content/MessageListener.ts b/src/content/MessageListener.ts index ec6940b..31cc1ae 100644 --- a/src/content/MessageListener.ts +++ b/src/content/MessageListener.ts @@ -27,7 +27,12 @@ export default class MessageListener { ) { browser.runtime.onMessage.addListener( (msg: any, sender: WebExtMessageSender) => { - return Promise.resolve(listener(valueOf(msg), sender)); + try { + return Promise.resolve(listener(valueOf(msg), sender)); + } catch (e) { + console.warn(e); + return; + } } ); } diff --git a/src/content/controllers/FindController.ts b/src/content/controllers/FindController.ts new file mode 100644 index 0000000..adcdb0d --- /dev/null +++ b/src/content/controllers/FindController.ts @@ -0,0 +1,19 @@ +import { injectable } from "tsyringe"; +import FindUseCase from "../usecases/FindUseCase"; + +@injectable() +export default class FindController { + constructor(private findUseCase: FindUseCase) {} + + findNext(keyword: string): boolean { + return this.findUseCase.findNext(keyword); + } + + findPrev(keyword: string): boolean { + return this.findUseCase.findPrev(keyword); + } + + clearSelection() { + return this.findUseCase.clearSelection(); + } +} diff --git a/src/content/di.ts b/src/content/di.ts index 7a7fb08..4c85e76 100644 --- a/src/content/di.ts +++ b/src/content/di.ts @@ -27,6 +27,7 @@ import { TabsClientImpl } from "./client/TabsClient"; import { container } from "tsyringe"; import OperatorFactoryImpl from "./operators/impls/OperatorFactoryImpl"; import { URLRepositoryImpl } from "./operators/impls/URLRepository"; +import { FindPresenterImpl } from "./presenters/FindPresenter"; container.register("FollowMasterClient", { useValue: new FollowMasterClientImpl(window.top), @@ -71,6 +72,7 @@ container.register("NavigationPresenter", { }); container.register("OperationClient", { useClass: OperationClientImpl }); container.register("ScrollPresenter", { useClass: ScrollPresenterImpl }); +container.register("FindPresenter", { useClass: FindPresenterImpl }); container.register("SettingClient", { useClass: SettingClientImpl }); container.register("SettingRepository", { useClass: SettingRepositoryImpl }); container.register("URLRepository", { useClass: URLRepositoryImpl }); diff --git a/src/content/presenters/FindPresenter.ts b/src/content/presenters/FindPresenter.ts new file mode 100644 index 0000000..569f161 --- /dev/null +++ b/src/content/presenters/FindPresenter.ts @@ -0,0 +1,23 @@ +export default interface FindPresenter { + find(keyword: string, backwards: boolean): boolean; + + clearSelection(): void; +} + +export class FindPresenterImpl implements FindPresenter { + find(keyword: string, backwards: boolean): boolean { + const caseSensitive = false; + const wrapScan = false; + + // NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work + // because of same origin policy + return window.find(keyword, caseSensitive, backwards, wrapScan); + } + + clearSelection(): void { + const sel = window.getSelection(); + if (sel) { + sel.removeAllRanges(); + } + } +} diff --git a/src/content/usecases/FindUseCase.ts b/src/content/usecases/FindUseCase.ts new file mode 100644 index 0000000..9e77124 --- /dev/null +++ b/src/content/usecases/FindUseCase.ts @@ -0,0 +1,22 @@ +import { inject, injectable } from "tsyringe"; +import FindPresenter from "../presenters/FindPresenter"; + +@injectable() +export default class FindUseCase { + constructor( + @inject("FindPresenter") + private readonly findPresenter: FindPresenter + ) {} + + findNext(keyword: string): boolean { + return this.findPresenter.find(keyword, false); + } + + findPrev(keyword: string): boolean { + return this.findPresenter.find(keyword, true); + } + + clearSelection() { + this.findPresenter.clearSelection(); + } +} |