aboutsummaryrefslogtreecommitdiff
path: root/src/content/index.js
blob: fdc7e89c583c7cbcf1e5bd5eb7c3f11ae60e6fa0 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as scrolls from './scrolls';
import * as histories from './histories';
import * as actions from '../shared/actions';
import * as messages from '../shared/messages';
import ConsoleFrame from '../console/console-frame';
import Follow from './follow';

let vvConsole = new ConsoleFrame(window);

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

  switch (action[0]) {
  case actions.CMD_OPEN:
    vvConsole.showCommand('');
    break;
  case actions.CMD_TABS_OPEN:
    if (action[1] || false) {
      // alter url
      vvConsole.showCommand('open ' + window.location.href);
    } else {
      vvConsole.showCommand('open ');
    }
    break;
  case actions.CMD_BUFFER:
    vvConsole.showCommand('buffer ');
    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);
    break;
  case actions.SCROLL_BOTTOM:
    scrolls.scrollBottom(window);
    break;
  case actions.SCROLL_LEFT:
    scrolls.scrollLeft(window);
    break;
  case actions.SCROLL_RIGHT:
    scrolls.scrollRight(window);
    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;
  }
}

const handleResponse = (response) => {
  if (!response) {
    return;
  }

  switch(response.type) {
  case 'response.action':
    doAction(response.action);
    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(handleResponse)
    .catch((err) => {
      console.error("Vim Vixen:", err);
      vvConsole.showError(err.message);
    });
});

const doCompletion = (line) => {
  if (line.startsWith('buffer ')) {
    let keyword = line.replace('buffer ', '');

    browser.runtime.sendMessage({
      type: 'event.cmd.tabs.completion',
      text: keyword
    }).then((completions) => {
      vvConsole.setCompletions([completions]);
    }).catch((err) => {
      console.error("Vim Vixen:", err);
      vvConsole.showError(err.message);
    });
  }
};

messages.receive(window, (message) => {
  switch (message.type) {
  case 'vimvixen.command.blur':
    if (!vvConsole.isErrorShown()) {
      vvConsole.hide();
    }
    break;
  case 'vimvixen.command.enter':
    browser.runtime.sendMessage({
      type: 'event.cmd.enter',
      text: message.value
    }).catch((err) => {
      console.error("Vim Vixen:", err);
      vvConsole.showError(err.message);
    });
    break;
  case 'vimvixen.command.change':
    doCompletion(message.value);
    break;
  default:
    return;
  }
});