aboutsummaryrefslogtreecommitdiff
path: root/src/actions/command.js
blob: dd755dc84c462b257ecadf7bdccc61f6accd919e (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as tabs from '../background/tabs';
import * as histories from '../background/histories';
import * as consoleActions from './console';

const DEFAULT_SEARCH_ENGINES = {
  default: 'google',
  engines: {
    'google': 'https://google.com/search?q={}',
    'yahoo': 'https://search.yahoo.com/search?p={}',
    'bing': 'https://www.bing.com/search?q={}',
    'duckduckgo': 'https://duckduckgo.com/?q={}',
    'twitter': 'https://twitter.com/search?q={}',
    'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}'
  }
};

const normalizeUrl = (string, searchConfig) => {
  try {
    return new URL(string).href;
  } catch (e) {
    if (string.includes('.') && !string.includes(' ')) {
      return 'http://' + string;
    }
    let query = encodeURI(string);
    let template = searchConfig.engines[
      searchConfig.default
    ];
    for (let key in searchConfig.engines) {
      if (string.startsWith(key + ' ')) {
        query = encodeURI(string.replace(key + ' ', ''));
        template = searchConfig.engines[key];
      }
    }
    return template.replace('{}', query);
  }
};

const openCommand = (url) => {
  return browser.tabs.query({
    active: true, currentWindow: true
  }).then((gotTabs) => {
    if (gotTabs.length > 0) {
      return browser.tabs.update(gotTabs[0].id, { url: url });
    }
  });
};

const tabopenCommand = (url) => {
  return browser.tabs.create({ url: url });
};

const bufferCommand = (keywords) => {
  return browser.tabs.query({
    active: true, currentWindow: true
  }).then((gotTabs) => {
    if (gotTabs.length > 0) {
      if (isNaN(keywords)) {
        return tabs.selectByKeyword(gotTabs[0], keywords);
      }
      let index = parseInt(keywords, 10) - 1;
      return tabs.selectAt(index);
    }
  });
};

const getOpenCompletions = (command, keywords) => {
  return histories.getCompletions(keywords).then((pages) => {
    let historyItems = pages.map((page) => {
      return {
        caption: page.title,
        content: command + ' ' + page.url,
        url: page.url
      };
    });
    let engineNames = Object.keys(DEFAULT_SEARCH_ENGINES.engines);
    let engineItems = engineNames.filter(name => name.startsWith(keywords))
      .map(name => ({
        caption: name,
        content: command + ' ' + name
      }));

    let completions = [];
    if (engineItems.length > 0) {
      completions.push({
        name: 'Search Engines',
        items: engineItems
      });
    }
    if (historyItems.length > 0) {
      completions.push({
        name: 'History',
        items: historyItems
      });
    }
    return completions;
  });
};

const doCommand = (name, remaining) => {
  switch (name) {
  case 'o':
  case 'open':
    // TODO use search engined and pass keywords to them
    return openCommand(normalizeUrl(remaining, DEFAULT_SEARCH_ENGINES));
  case 't':
  case 'tabopen':
    return tabopenCommand(normalizeUrl(remaining, DEFAULT_SEARCH_ENGINES));
  case 'b':
  case 'buffer':
    return bufferCommand(remaining);
  }
  throw new Error(name + ' command is not defined');
};

const getCompletions = (command, keywords) => {
  switch (command) {
  case 'o':
  case 'open':
  case 't':
  case 'tabopen':
    return getOpenCompletions(command, keywords);
  case 'b':
  case 'buffer':
    return tabs.getCompletions(keywords).then((gotTabs) => {
      let items = gotTabs.map((tab) => {
        return {
          caption: tab.title,
          content: command + ' ' + tab.title,
          url: tab.url,
          icon: tab.favIconUrl
        };
      });
      return [
        {
          name: 'Buffers',
          items: items
        }
      ];
    });
  }
  return Promise.resolve([]);
};

const exec = (line) => {
  let name = line.split(' ')[0];
  let remaining = line.replace(name + ' ', '');
  return doCommand(name, remaining).then(() => {
    return consoleActions.hide();
  });
};

const complete = (line) => {
  let command = line.split(' ', 1)[0];
  let keywords = line.replace(command + ' ', '');
  return getCompletions(command, keywords).then(consoleActions.setCompletions);
};

export { exec, complete };