diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-07 21:16:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-07 21:16:47 +0900 |
commit | 05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch) | |
tree | 2c7708ca91ac2b462cc86aa28612e3d3943496f3 /src/content/actions/find.ts | |
parent | 457d954e08923b4accd28a919c72d0b61db1bb98 (diff) | |
parent | 27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff) |
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'src/content/actions/find.ts')
-rw-r--r-- | src/content/actions/find.ts | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/content/actions/find.ts b/src/content/actions/find.ts new file mode 100644 index 0000000..53e03ae --- /dev/null +++ b/src/content/actions/find.ts @@ -0,0 +1,100 @@ +// +// 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 + +import * as messages from '../../shared/messages'; +import * as actions from './index'; +import * as consoleFrames from '../console-frames'; + +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; + +const find = (str: 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 + + // eslint-disable-next-line no-extra-parens + let found = window.find(str, caseSensitive, backwards, wrapScan); + if (found) { + return found; + } + let sel = window.getSelection(); + if (sel) { + sel.removeAllRanges(); + } + + // eslint-disable-next-line no-extra-parens + return window.find(str, caseSensitive, backwards, wrapScan); +}; + +// eslint-disable-next-line max-statements +const findNext = async( + currentKeyword: string, reset: boolean, backwards: boolean, +): Promise<actions.FindAction> => { + if (reset) { + let sel = window.getSelection(); + if (sel) { + sel.removeAllRanges(); + } + } + + let keyword = currentKeyword; + if (currentKeyword) { + browser.runtime.sendMessage({ + type: messages.FIND_SET_KEYWORD, + keyword: currentKeyword, + }); + } else { + keyword = await browser.runtime.sendMessage({ + type: messages.FIND_GET_KEYWORD, + }); + } + if (!keyword) { + await consoleFrames.postError('No previous search keywords'); + return { type: actions.NOOP }; + } + let found = find(keyword, backwards); + if (found) { + consoleFrames.postInfo('Pattern found: ' + keyword); + } else { + consoleFrames.postError('Pattern not found: ' + keyword); + } + + return { + type: actions.FIND_SET_KEYWORD, + keyword, + found, + }; +}; + +const next = ( + currentKeyword: string, reset: boolean, +): Promise<actions.FindAction> => { + return findNext(currentKeyword, reset, false); +}; + +const prev = ( + currentKeyword: string, reset: boolean, +): Promise<actions.FindAction> => { + return findNext(currentKeyword, reset, true); +}; + +export { next, prev }; |