aboutsummaryrefslogtreecommitdiffstats
path: root/docs/backend_api.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/backend_api.md')
-rw-r--r--docs/backend_api.md90
1 files changed, 90 insertions, 0 deletions
diff --git a/docs/backend_api.md b/docs/backend_api.md
new file mode 100644
index 0000000..dfc9f22
--- /dev/null
+++ b/docs/backend_api.md
@@ -0,0 +1,90 @@
1# Backend API
2
3Provides a set of helper functions to integrate the recipe into [Ferdi](https://getferdi.com).
4
5## Ferdi Backend Class Methods
6* [validateUrl](#user-content-validateurl)
7* [overrideUserAgent](#user-content-overrideuseragent)
8
9## Events
10* [webview events](#user-content-events)
11
12### validateUrl(URL)
13Validate if the given URL is a valid service instance.
14
15#### Arguments
161. `string` URL
17
18#### Returns
19[`Promise`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)
20
21#### Usage
22
23```js
24// RocketChat integration
25module.exports = Ferdi => class RocketChat extends Ferdi {
26 async validateUrl(url) {
27 try {
28 const resp = await window.fetch(`${url}/api/info`, {
29 method: 'GET',
30 headers: {
31 'Content-Type': 'application/json',
32 },
33 });
34 const data = await resp.json();
35
36 return Object.hasOwnProperty.call(data, 'version');
37 } catch (err) {
38 console.error(err);
39 }
40
41 return false;
42 }
43};
44```
45
46### overrideUserAgent()
47Validate if the given URL is a valid service instance.
48
49#### Returns
50`Boolean`
51
52#### Usage
53
54```js
55// Discord integration
56module.exports = Ferdi => class Discord extends Ferdi {
57 overrideUserAgent() {
58 const useragent = window.navigator.userAgent;
59
60 // Quick and dirty hackfix
61 const parts = useragent.split('(KHTML, like Gecko)');
62
63 return parts.join('(KHTML, like Gecko) discord/0.0.248').replace('Electron', 'Discord').replace('Ferdi', 'Discord');
64 }
65};
66
67```
68
69### Events
70Ferdi recipes can hook into the [electron webview events](https://electron.atom.io/docs/api/webview-tag/#dom-events) to trigger custom functions.
71
72This is necessary for services like TweetDeck where custom URL forwarding is needed during login.
73
74#### Usage
75```js
76module.exports = Ferdi => class Tweetdeck extends Ferdi {
77 events = {
78 'did-get-redirect-request': '_redirectFix',
79 }
80
81 _redirectFix(event) {
82 if (event.newURL !== undefined && event.oldURL !== undefined && event.isMainFrame) {
83 if (event.isMainFrame) {
84 setTimeout(() => this.send('redirect-url', event.newURL), 100);
85 event.preventDefault();
86 }
87 }
88 }
89};
90```