-
Notifications
You must be signed in to change notification settings - Fork 580
/
RecipeWebview.js
112 lines (91 loc) · 2.7 KB
/
RecipeWebview.js
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
import { ipcRenderer } from 'electron';
import fs from 'fs-extra';
const debug = require('debug')('Franz:Plugin:RecipeWebview');
class RecipeWebview {
constructor() {
this.countCache = {
direct: 0,
indirect: 0,
};
ipcRenderer.on('poll', () => {
this.loopFunc();
debug('Poll event');
});
window.FranzAPI = {
clearCache: RecipeWebview.clearCache,
};
}
loopFunc = () => null;
/**
* Initialize the loop
*
* @param {Function} Function that will be executed
*/
loop(fn) {
this.loopFunc = fn;
}
/**
* Set the unread message badge
*
* @param {int} direct Set the count of direct messages
* eg. Slack direct mentions, or a
* message to @channel
* @param {int} 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) {
if (this.countCache.direct === direct
&& this.countCache.indirect === indirect) return;
const count = {
direct: direct > 0 ? direct : 0,
indirect: indirect > 0 ? indirect : 0,
};
ipcRenderer.send('messages', count);
Object.assign(this.countCache, count);
debug('Sending badge count to host', count);
}
/**
* 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) {
files.forEach((file) => {
const data = fs.readFileSync(file);
const styles = document.createElement('style');
styles.innerHTML = data.toString();
document.querySelector('head').appendChild(styles);
debug('Append styles', styles);
});
}
/**
* Set the thumbnail for the service
*
* @param {int} direct Set the count of direct messages
* eg. Slack direct mentions, or a
* message to @channel
* @param {int} indirect Set a badge that defines there are
* new messages but they do not involve
* me directly to me eg. in a channel
*/
setServiceIcon(url) {
ipcRenderer.send('avatar', url);
debug('Sending avatar url to host', url);
}
onNotify(fn) {
if (typeof fn === 'function') {
window.Notification.prototype.onNotify = fn;
}
}
initialize(fn) {
if (typeof fn === 'function') {
fn();
}
}
static clearCache() {
ipcRenderer.invoke('clearServiceCache');
}
}
module.exports = RecipeWebview;