aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/common/index.ts
blob: 899953d00df318cb1835d93b2a3402b2dadfdbd1 (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
import InputComponent from './input';
import FollowComponent from './follow';
import MarkComponent from './mark';
import KeymapperComponent from './keymapper';
import * as messages from '../../../shared/messages';
import MessageListener from '../../MessageListener';
import * as blacklists from '../../../shared/blacklists';
import * as keys from '../../../shared/utils/keys';

import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
import SettingUseCase from '../../usecases/SettingUseCase';

let addonEnabledUseCase = new AddonEnabledUseCase();
let settingUseCase = new SettingUseCase();

export default class Common {
  constructor(win: Window, store: any) {
    const input = new InputComponent(win.document.body);
    const follow = new FollowComponent(win);
    const mark = new MarkComponent(store);
    const keymapper = new KeymapperComponent(store);

    input.onKey((key: keys.Key) => follow.key(key));
    input.onKey((key: keys.Key) => mark.key(key));
    input.onKey((key: keys.Key) => keymapper.key(key));

    this.reloadSettings();

    new MessageListener().onBackgroundMessage(this.onMessage.bind(this));
  }

  onMessage(message: messages.Message) {
    switch (message.type) {
    case messages.SETTINGS_CHANGED:
      return this.reloadSettings();
    case messages.ADDON_TOGGLE_ENABLED:
      return addonEnabledUseCase.toggle();
    }
    return undefined;
  }

  async reloadSettings() {
    try {
      let current = await settingUseCase.reload();
      let disabled = blacklists.includes(
        current.blacklist, window.location.href,
      );
      if (disabled) {
        addonEnabledUseCase.disable();
      } else {
        addonEnabledUseCase.enable();
      }
    } catch (e) {
      // Sometime sendMessage fails when background script is not ready.
      console.warn(e);
      setTimeout(() => this.reloadSettings(), 500);
    }
  }
}