aboutsummaryrefslogtreecommitdiff
path: root/src/background/index.js
blob: 4d75b332cc9484c201b040a2b78e0110c93b8328 (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
import * as tabs from './tabs';
import * as keys from './keys';
import * as inputActions from '../actions/input';
import backgroundReducers from '../reducers/background';
import inputReducers from '../reducers/input';

let inputState = inputReducers(undefined, {});

const normalizeUrl = (string) => {
  try {
    return new URL(string).href
  } catch (e) {
    return 'http://' + string;
  }
}

const cmdBuffer = (sender, arg) => {
  if (isNaN(arg)) {
    return tabs.selectByKeyword(sender.tab, arg);
  } else {
    let index = parseInt(arg, 10) - 1;
    return tabs.selectAt(index);
  }
}

const cmdEnterHandle = (request, sender) => {
  let words = request.text.split(' ').filter((s) => s.length > 0);
  switch (words[0]) {
  case 'open':
    return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
  case 'tabopen':
    return browser.tabs.create({ url: normalizeUrl(words[1]) });
  case 'b':
  case 'buffer':
    return cmdBuffer(sender, words[1]);
  }
  throw new Error(words[0] + ' command is not defined');
};

browser.runtime.onMessage.addListener((request, sender) => {
  switch (request.type) {
  case 'event.cmd.enter':
    return cmdEnterHandle(request, sender);
  default:
    return browser.tabs.sendMessage(sender.tab.id, request);
  }
});

const keyQueueChanged = (sender, prevState, state) => {
  if (state.keys.length === 0) {
    return Promise.resolve();
  }

  let prefix = keys.asKeymapChars(state.keys);
  let matched = Object.keys(keys.defaultKeymap).filter((keys) => {
    return keys.startsWith(prefix);
  });
  if (matched.length == 0) {
    return handleMessage(inputActions.clearKeys(), sender);
  } else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) {
    return Promise.resolve();
  }
  let action = keys.defaultKeymap[matched];
  return handleMessage(inputActions.clearKeys(), sender).then(() => {
    return backgroundReducers(undefined, action, sender).then(() => {
      return browser.tabs.sendMessage(sender.tab.id, action);
    });
  });
};

const handleMessage = (action, sender) => {
  let nextInputState = inputReducers(inputState, action);
  if (JSON.stringify(nextInputState) !== JSON.stringify(inputState)) {
    let prevState = inputState;
    inputState = nextInputState;
    return keyQueueChanged(sender, prevState, inputState);
  }
  return backgroundReducers(undefined, action, sender);
};

browser.runtime.onMessage.addListener(handleMessage);