diff options
author | vantezzen <hello@vantezzen.io> | 2020-03-08 11:10:17 +0100 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2020-03-08 11:10:17 +0100 |
commit | 9d715597a600710c20f75412d3dcd8cdb7b3c39e (patch) | |
tree | 65ab812834d486aaccf35e2a31a7a628a7100422 /docs/backend_api.md | |
parent | Revert to using the standart file comparison function (diff) | |
download | ferdium-recipes-9d715597a600710c20f75412d3dcd8cdb7b3c39e.tar.gz ferdium-recipes-9d715597a600710c20f75412d3dcd8cdb7b3c39e.tar.zst ferdium-recipes-9d715597a600710c20f75412d3dcd8cdb7b3c39e.zip |
Add documentation
Diffstat (limited to 'docs/backend_api.md')
-rw-r--r-- | docs/backend_api.md | 90 |
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 | |||
3 | Provides 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) | ||
13 | Validate if the given URL is a valid service instance. | ||
14 | |||
15 | #### Arguments | ||
16 | 1. `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 | ||
25 | module.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() | ||
47 | Validate if the given URL is a valid service instance. | ||
48 | |||
49 | #### Returns | ||
50 | `Boolean` | ||
51 | |||
52 | #### Usage | ||
53 | |||
54 | ```js | ||
55 | // Discord integration | ||
56 | module.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 | ||
70 | Ferdi recipes can hook into the [electron webview events](https://electron.atom.io/docs/api/webview-tag/#dom-events) to trigger custom functions. | ||
71 | |||
72 | This is necessary for services like TweetDeck where custom URL forwarding is needed during login. | ||
73 | |||
74 | #### Usage | ||
75 | ```js | ||
76 | module.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 | ``` | ||