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
|
import { ipcRenderer } from 'electron';
import { pathExistsSync, readFileSync, existsSync } from 'fs-extra';
const debug = require('debug')('Ferdi:Plugin:RecipeWebview');
class RecipeWebview {
constructor(badgeHandler, notificationsHandler) {
this.badgeHandler = badgeHandler;
this.notificationsHandler = notificationsHandler;
ipcRenderer.on('poll', () => {
this.loopFunc();
debug('Poll event');
// This event is for checking if the service recipe is still actively
// communicating with the client
ipcRenderer.sendToHost('alive');
});
}
loopFunc = () => null;
darkModeHandler = false;
/**
* Initialize the loop
*
* @param {Function} Function that will be executed
*/
loop(fn) {
this.loopFunc = fn;
}
/**
* Set the unread message badge
*
* @param {string | number | undefined | null} direct Set the count of direct messages
* eg. Slack direct mentions, or a
* message to @channel
* @param {string | number | undefined | null} indirect Set a badge that defines there are
* new messages but they do not involve
* me directly to me eg. in a channel
*/
setBadge(direct = 0, indirect = 0) {
this.badgeHandler.setBadge(direct, indirect);
}
/**
* Safely parse the given text into an integer
*
* @param {string | number | undefined | null} text to be parsed
*/
safeParseInt(text) {
return this.badgeHandler.safeParseInt(text);
}
/**
* Injects the contents of a CSS file into the current webview
*
* @param {Array} files CSS files that should be injected. This must
* be an absolute path to the file
*/
injectCSS(...files) {
// eslint-disable-next-line unicorn/no-array-for-each
files.forEach(file => {
if (pathExistsSync(file)) {
const styles = document.createElement('style');
styles.innerHTML = readFileSync(file, 'utf8');
document.querySelector('head').append(styles);
debug('Append styles', styles);
}
});
}
injectJSUnsafe(...files) {
Promise.all(
files.map(file => {
if (existsSync(file)) {
return readFileSync(file, 'utf8');
}
debug('Script not found', file);
return null;
}),
).then(scripts => {
const scriptsFound = scripts.filter(script => script !== null);
if (scriptsFound.length > 0) {
debug('Inject scripts to main world', scriptsFound);
ipcRenderer.sendToHost('inject-js-unsafe', ...scriptsFound);
}
});
}
/**
* Set a custom handler for turning on and off dark mode
*
* @param {function} handler
*/
handleDarkMode(handler) {
this.darkModeHandler = handler;
}
onNotify(fn) {
if (typeof fn === 'function') {
this.notificationsHandler.onNotify = fn;
}
}
initialize(fn) {
if (typeof fn === 'function') {
fn();
}
}
}
export default RecipeWebview;
|