aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/lib/Userscript.ts
blob: f02a8f135c6595b26d6e7c24a2fa34520403e754 (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
type Recipe = {
  setBadge: (direct: number, indirect: number) => void;
  setDialogTitle: (title: string) => void;
  injectCSS: (css: string | string[]) => void;
};

export default class Userscript {
  // Current ./lib/RecipeWebview instance
  recipe: Recipe | null = null;

  // Current ./recipe.js instance
  controller = null;

  // Service configuration
  config = {};

  // Ferdium and service settings
  settings = {};

  constructor(recipe, controller, config) {
    this.recipe = recipe;
    this.controller = controller;
    this.internal_setSettings(controller.settings);
    this.config = config;
  }

  /**
   * Set internal copy of Ferdium's settings.
   * This is only used internally and can not be used to change any settings
   *
   * @param {*} settings
   */
  // eslint-disable-next-line camelcase
  internal_setSettings(settings: any) {
    // This is needed to get a clean JS object from the settings itself to provide better accessibility
    // Otherwise this will be a mobX instance
    this.settings = JSON.parse(JSON.stringify(settings));
  }

  /**
   * Set badge count for the current service
   * @param {number} direct Direct messages
   * @param {number} indirect Indirect messages
   */
  setBadge(direct: number = 0, indirect: number = 0) {
    if (this.recipe && this.recipe.setBadge) {
      this.recipe.setBadge(direct, indirect);
    }
  }

  /**
   * Set active dialog title to the app title
   * @param {*} title Dialog title
   */
  setDialogTitle(title: string) {
    if (this.recipe && this.recipe.setDialogTitle) {
      this.recipe.setDialogTitle(title);
    }
  }

  /**
   * Inject CSS files into the current page
   *
   * @param  {...string} files
   */
  injectCSSFiles(...files: string[]) {
    if (this.recipe && this.recipe.injectCSS) {
      // @ts-expect-error A spread argument must either have a tuple type or be passed to a rest parameter.
      this.recipe.injectCSS(...files);
    }
  }

  /**
   * Inject a CSS string into the page
   *
   * @param {string} css
   */
  injectCSS(css: string) {
    const style = document.createElement('style');
    style.textContent = css;
    document.head.append(style);
  }

  /**
   * Set or update value in storage
   *
   * @param {string} key
   * @param {any} value
   */
  set(key: string, value: string) {
    window.localStorage.setItem(`ferdium-user-${key}`, JSON.stringify(value));
  }

  /**
   * Get value from storage
   *
   * @param {string} key
   * @return Value of the key
   */
  get(key: string) {
    const ferdiumUserKey = window.localStorage.getItem(`ferdium-user-${key}`);

    if (ferdiumUserKey) {
      return JSON.parse(ferdiumUserKey);
    }
  }
}