aboutsummaryrefslogtreecommitdiff
path: root/src/content/controllers/SettingController.ts
blob: f0e770b0d1e75870d6bf7543fcfe87a3e7319313 (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
import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase';
import SettingUseCase from '../usecases/SettingUseCase';
import * as blacklists from '../../shared/blacklists';

import * as messages from '../../shared/messages';

export default class SettingController {
  private addonEnabledUseCase: AddonEnabledUseCase;

  private settingUseCase: SettingUseCase;

  constructor({
    addonEnabledUseCase = new AddonEnabledUseCase(),
    settingUseCase = new SettingUseCase(),
  } = {}) {
    this.addonEnabledUseCase = addonEnabledUseCase;
    this.settingUseCase = settingUseCase;
  }

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

  async reloadSettings(_message: messages.Message): Promise<void> {
    await this.settingUseCase.reload();
  }
}