aboutsummaryrefslogtreecommitdiff
path: root/src/content/actions/find.js
blob: b26621694d2b52a17f59540146421b7b44b26a7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// 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 actions from 'content/actions';
import * as consoleFrames from '../console-frames';

const postPatternNotFound = (pattern) => {
  return consoleFrames.postError(
    window.document,
    'Pattern not found: ' + pattern);
};

const postPatternFound = (pattern) => {
  return consoleFrames.postInfo(
    window.document,
    'Pattern found: ' + pattern,
  );
};

const find = (string, backwards) => {
  let caseSensitive = false;
  let wrapScan = true;


  // NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work
  // because of same origin policy
  return window.find(string, caseSensitive, backwards, wrapScan);
};

const findNext = (keyword, reset, backwards) => {
  if (reset) {
    window.getSelection().removeAllRanges();
  }

  let found = find(keyword, backwards);
  if (!found) {
    window.getSelection().removeAllRanges();
    found = find(keyword, backwards);
  }
  if (found) {
    postPatternFound(keyword);
  } else {
    postPatternNotFound(keyword);
  }

  return {
    type: actions.FIND_SET_KEYWORD,
    keyword,
    found,
  };
};

const next = (keyword, reset) => {
  return findNext(keyword, reset, false);
};

const prev = (keyword, reset) => {
  return findNext(keyword, reset, true);
};

export { next, prev };