aboutsummaryrefslogtreecommitdiff
path: root/src/background/components/operation.js
blob: ce93270e7212e84e18828cb047e5c1b47d606eff (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
import messages from 'shared/messages';
import operations from 'shared/operations';
import * as tabs from '../shared//tabs';
import * as zooms from '../shared/zooms';
import * as consoleActions from '../actions/console';

export default class BackgroundComponent {
  constructor(store) {
    this.store = store;

    browser.runtime.onMessage.addListener((message, sender) => {
      try {
        return this.onMessage(message, sender);
      } catch (e) {
        return browser.tabs.sendMessage(sender.tab.id, {
          type: messages.CONSOLE_SHOW_ERROR,
          text: e.message,
        });
      }
    });
  }

  onMessage(message, sender) {
    switch (message.type) {
    case messages.BACKGROUND_OPERATION:
      return this.store.dispatch(
        this.exec(message.operation, sender.tab));
    }
  }

  // eslint-disable-next-line complexity, max-lines-per-function
  async exec(operation, tab) {
    let tabState = this.store.getState().tab;

    switch (operation.type) {
    case operations.TAB_CLOSE:
      await tabs.closeTab(tab.id);
      break;
    case operations.TAB_CLOSE_FORCE:
      await tabs.closeTabForce(tab.id);
      break;
    case operations.TAB_REOPEN:
      await tabs.reopenTab();
      break;
    case operations.TAB_PREV:
      await tabs.selectPrevTab(tab.index, operation.count);
      break;
    case operations.TAB_NEXT:
      await tabs.selectNextTab(tab.index, operation.count);
      break;
    case operations.TAB_FIRST:
      await tabs.selectFirstTab();
      break;
    case operations.TAB_LAST:
      await tabs.selectLastTab();
      break;
    case operations.TAB_PREV_SEL:
      if (tabState.previousSelected > 0) {
        await tabs.selectTab(tabState.previousSelected);
      }
      break;
    case operations.TAB_RELOAD:
      await tabs.reload(tab, operation.cache);
      break;
    case operations.TAB_PIN:
      await tabs.updateTabPinned(tab, true);
      break;
    case operations.TAB_UNPIN:
      await tabs.updateTabPinned(tab, false);
      break;
    case operations.TAB_TOGGLE_PINNED:
      await tabs.toggleTabPinned(tab);
      break;
    case operations.TAB_DUPLICATE:
      await tabs.duplicate(tab.id);
      break;
    case operations.ZOOM_IN:
      await zooms.zoomIn();
      break;
    case operations.ZOOM_OUT:
      await zooms.zoomOut();
      break;
    case operations.ZOOM_NEUTRAL:
      await zooms.neutral();
      break;
    case operations.COMMAND_SHOW:
      return consoleActions.showCommand(tab, '');
    case operations.COMMAND_SHOW_OPEN:
      if (operation.alter) {
        // alter url
        return consoleActions.showCommand(tab, 'open ' + tab.url);
      }
      return consoleActions.showCommand(tab, 'open ');
    case operations.COMMAND_SHOW_TABOPEN:
      if (operation.alter) {
        // alter url
        return consoleActions.showCommand(tab, 'tabopen ' + tab.url);
      }
      return consoleActions.showCommand(tab, 'tabopen ');
    case operations.COMMAND_SHOW_WINOPEN:
      if (operation.alter) {
        // alter url
        return consoleActions.showCommand(tab, 'winopen ' + tab.url);
      }
      return consoleActions.showCommand(tab, 'winopen ');
    case operations.COMMAND_SHOW_BUFFER:
      return consoleActions.showCommand(tab, 'buffer ');
    case operations.COMMAND_SHOW_ADDBOOKMARK:
      if (operation.alter) {
        return consoleActions.showCommand(tab, 'addbookmark ' + tab.title);
      }
      return consoleActions.showCommand(tab, 'addbookmark  ');
    case operations.FIND_START:
      return consoleActions.showFind(tab);
    case operations.CANCEL:
      return consoleActions.hide(tab);
    case operations.PAGE_SOURCE:
      await browser.tabs.create({
        url: 'view-source:' + tab.url,
        index: tab.index + 1,
        openerTabId: tab.id,
      });
      break;
    }
    return { type: '' };
  }
}