aboutsummaryrefslogtreecommitdiff
path: root/src/content/index.js
blob: 6bf5d5708dd5b6c0a35f0726e4cb506ba428c725 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as scrolls from './scrolls';
import * as histories from './histories';
import * as actions from '../shared/actions';
import FooterLine from './footer-line';
import Follow from './follow';

var footer = null;

const createFooterLine = (initial = '') => {
  footer = new FooterLine(document, initial);
  footer.onPromptChange((e) => {
    let request = {
      type: 'event.cmd.suggest',
      text: e.target.value
    };
    browser.runtime.sendMessage(request);
  });
  footer.onEntered((e) => {
    let request = {
      type: 'event.cmd.enter',
      text: e.target.value
    };
    browser.runtime.sendMessage(request);
  });
  footer.focus();
}

const invokeEvent = (action) => {
  if (typeof action === 'undefined' || action === null) {
    return;
  }

  switch (action[0]) {
  case actions.CMD_OPEN:
    createFooterLine();
    break;
  case actions.CMD_TABS_OPEN:
    if (action[1] || false) {
      // alter url
      createFooterLine('open ' + window.location.href);
    } else {
      createFooterLine('open ');
    }
    break;
  case actions.SCROLL_LINES:
    scrolls.scrollLines(window, action[1]);
    break;
  case actions.SCROLL_PAGES:
    scrolls.scrollPages(window, action[1]);
    break;
  case actions.SCROLL_TOP:
    scrolls.scrollTop(window, action[1]);
    break;
  case actions.SCROLL_BOTTOM:
    scrolls.scrollBottom(window, action[1]);
    break;
  case actions.FOLLOW_START:
    new Follow(window.document, action[1] || false);
    break;
  case actions.HISTORY_PREV:
    histories.prev(window);
    break;
  case actions.HISTORY_NEXT:
    histories.next(window);
    break;
  }
}

window.addEventListener("keypress", (e) => {
  if (e.target instanceof HTMLInputElement) {
    return;
  }

  let request = {
    type: 'event.keypress',
    code: e.which,
    ctrl: e.ctrlKey,
  }

  browser.runtime.sendMessage(request)
    .then(invokeEvent,
      (err) => {
        console.log(`Vim Vixen: ${err}`);
      });
});