aboutsummaryrefslogtreecommitdiffstats
path: root/docs/backend_api.md
blob: a46927b111579b3d4d9e4913ed7bb4fb704cf145 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Backend API

Provides a set of helper functions to integrate the recipe into [Ferdium](https://ferdium.org).

## Ferdium Backend Class Methods

* [validateUrl](#validateurl)
* [overrideUserAgent](#overrideuseragent)
* [modifyRequestHeaders](#modifyrequestheaders)
* [knownCertificateHosts](#knownCertificateHosts)

## Events

* [webview events](#events)

### validateUrl(URL)

Validate if the given URL is a valid service instance.

#### Arguments

1. `string` URL

#### Returns

[`Promise`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)

#### Usage

```js
// RocketChat integration
module.exports = Ferdium => class RocketChat extends Ferdium {
  async validateUrl(url) {
    try {
      const resp = await window.fetch(`${url}/api/info`, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
      });
      const data = await resp.json();

      return Object.hasOwnProperty.call(data, 'version');
    } catch (err) {
      console.error(err);
    }

    return false;
  }
};
```

### overrideUserAgent()

Override the user agent used inside the service webview.

#### Returns

`String`

#### Usage

```js
module.exports = Ferdium => class Discord extends Ferdium {
  overrideUserAgent() {
    // Remove Ferdium's signature from the user agent
    return window.navigator.userAgent.replace(
    /(Ferdium|Electron)\/\S+ \([^)]+\)/g,
    ""
  );
  }
};
```

```js
module.exports = Ferdium => class Example extends Ferdium {
  overrideUserAgent() {
    // Use a completely different user agent
    return "Mozilla/2.02Gold (Win95; I)";
  }
};
```

### modifyRequestHeaders()

Modify headers of HTTP requests sent from a recipe's webview
Any standard HTTP header can be added to the requests.

#### Returns

`Array` containing objects, each of which should have two properties.

* `headers` - Object containing the header params and their values in key-value format
* `requestFilters` - Array of URL patterns used to filter requests for which the headers need to be added.
Valid URL patterns can be referred from [here](https://www.electronjs.org/docs/api/web-request#webrequestonbeforerequestfilter-listener)

#### Usage

```js
// Hangouts Chat integration
module.exports = Ferdium => class HangoutsChat extends Ferdium {
    modifyRequestHeaders() {
      return [{
        // Adding an origin header for all http requests from this recipe
        headers: { 'origin': 'https://chat.google.com' },
        requestFilters: {
          urls: ['*://*/*']
        }
      }]
    }
};
```

### knownCertificateHosts()

Specify an array of known hosts from where certificates can be issued for this service

#### Returns

`Array` containing hostnames from where certificates can be issued

#### Usage

```js
// msteams Chat integration
module.exports = Ferdium => class MicrosoftTeams extends Ferdium {
  knownCertificateHosts() {
    return [
      'aka.ms',
      'aspnetcdn.com',
      'azure.net',
      'azureedge.net',
      'live.com',
      'microsoft.com',
      'microsoftonline.com',
      'msecnd.net',
      'msedge.net',
      'mstea.ms',
      'office.net',
      'okta.com',
      'sfbassets.com',
      'skype.com',
      'skypeassets.com',
      'skypeforbusiness.com',
      'tenor.com',
      'windows.com',
      'windows.net',
    ];
  };
};
```

### Events

Ferdium recipes can hook into the [electron webview events](https://electron.atom.io/docs/api/webview-tag/#dom-events) to trigger custom functions.

This is necessary for services like TweetDeck where custom URL forwarding is needed during login.

#### Usage

```js
module.exports = Ferdium => class Tweetdeck extends Ferdium {
  events = {
    'did-get-redirect-request': '_redirectFix',
  }

  _redirectFix(event) {
    if (event.newURL !== undefined && event.oldURL !== undefined && event.isMainFrame) {
      setTimeout(() => this.send('redirect-url', event.newURL), 100);
      event.preventDefault();
    }
  }
};
```