aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/top-content/index.js
blob: f6afbfab3725f0933a1793784f01b45bc0592734 (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
import CommonComponent from '../common';
import FollowController from './follow-controller';
import * as consoleFrames from '../../console-frames';
import * as addonActions from '../../actions/addon';
import messages from 'shared/messages';
import * as re from 'shared/utils/re';

export default class TopContent {

  constructor(win, store) {
    this.win = win;
    this.store = store;
    this.prevBlacklist = undefined;

    new CommonComponent(win, store); // eslint-disable-line no-new
    new FollowController(win, store); // eslint-disable-line no-new

    // TODO make component
    consoleFrames.initialize(this.win.document);

    messages.onMessage(this.onMessage.bind(this));
  }

  update() {
    let blacklist = this.store.getState().setting.blacklist;
    if (JSON.stringify(this.prevBlacklist) !== JSON.stringify(blacklist)) {
      this.disableIfBlack(blacklist);
      this.prevBlacklist = blacklist;
    }
  }

  disableIfBlack(blacklist) {
    let loc = this.win.location;
    let partial = loc.host + loc.pathname;
    let matched = blacklist
      .map((item) => {
        let pattern = item.includes('/') ? item : item + '/*';
        return re.fromWildcard(pattern);
      })
      .some(regex => regex.test(partial));
    if (matched) {
      this.store.dispatch(addonActions.disable());
    }
  }

  onMessage(message) {
    switch (message.type) {
    case messages.CONSOLE_HIDE_COMMAND:
      this.win.focus();
      consoleFrames.blur(window.document);
      return Promise.resolve();
    }
  }
}