aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/lib/Userscript.js
blob: bed2b1ff81d368b94394d774e47e2db1d023af64 (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
128
import { ipcRenderer } from 'electron';

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

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

  // Service configuration
  config = {};

  // Ferdi and service settings
  settings = {};

  settingsUpdateHandler = null;

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

  /**
   * Set internal copy of Ferdi'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) {
    // 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));

    if (typeof this.settingsUpdateHandler === 'function') {
      this.settingsUpdateHandler();
    }
  }

  /**
   * Register a settings handler to be executed when the settings change
   *
   * @param {function} handler
   */
  onSettingsUpdate(handler) {
    this.settingsUpdateHandler = handler;
  }

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

  /**
   * Inject CSS files into the current page
   *
   * @param  {...string} files
   */
  injectCSSFiles(...files) {
    if (this.recipe && this.recipe.injectCSS) {
      this.recipe.injectCSS(...files);
    }
  }

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

  /**
   * Open "Find in Page" popup
   */
  openFindInPage() {
    this.controller.openFindInPage();
  }

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

  /**
   * Get value from storage
   *
   * @param {*} key
   * @return Value of the key
   */
  get(key) {
    return JSON.parse(window.localStorage.getItem(`ferdi-user-${key}`));
  }

  /**
   * Open a URL in an external browser
   *
   * @param {*} url
   */
  externalOpen(url) {
    ipcRenderer.sendToHost('new-window', url);
  }

  /**
   * Open a URL in the current service
   *
   * @param {*} url
   */
  internalOpen(url) {
    window.location.href = url;
  }
}