diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-19 15:59:05 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 15:59:05 +0900 |
commit | 3f4bc62ed515f1c5da90ee1c3e42f3d435ea6e39 (patch) | |
tree | 8af9f8e5b12d007ce9628b40f3046b73f18e29f8 /src/content/presenters/FindPresenter.ts | |
parent | 6ec560bca33e774ff7e363270c423c919fdcf4ce (diff) | |
parent | c4dcdff9844e2404e3bc035f4cea9fce2f7770ab (diff) |
Merge pull request #587 from ueokande/refactor-content
Refactor content scripts
Diffstat (limited to 'src/content/presenters/FindPresenter.ts')
-rw-r--r-- | src/content/presenters/FindPresenter.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/content/presenters/FindPresenter.ts b/src/content/presenters/FindPresenter.ts new file mode 100644 index 0000000..d9bc835 --- /dev/null +++ b/src/content/presenters/FindPresenter.ts @@ -0,0 +1,52 @@ + +export default interface FindPresenter { + find(keyword: string, backwards: boolean): boolean; + + clearSelection(): void; + + // eslint-disable-next-line semi +} + +// window.find(aString, aCaseSensitive, aBackwards, aWrapAround, +// aWholeWord, aSearchInFrames); +// +// NOTE: window.find is not standard API +// https://developer.mozilla.org/en-US/docs/Web/API/Window/find +interface MyWindow extends Window { + find( + aString: string, + aCaseSensitive?: boolean, + aBackwards?: boolean, + aWrapAround?: boolean, + aWholeWord?: boolean, + aSearchInFrames?: boolean, + aShowDialog?: boolean): boolean; +} + +// eslint-disable-next-line no-var, vars-on-top, init-declarations +declare var window: MyWindow; + +export class FindPresenterImpl implements FindPresenter { + find(keyword: string, backwards: boolean): boolean { + let caseSensitive = false; + let wrapScan = true; + + + // NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work + // because of same origin policy + let found = window.find(keyword, caseSensitive, backwards, wrapScan); + if (found) { + return found; + } + this.clearSelection(); + + return window.find(keyword, caseSensitive, backwards, wrapScan); + } + + clearSelection(): void { + let sel = window.getSelection(); + if (sel) { + sel.removeAllRanges(); + } + } +} |