aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/lib/RecipeWebview.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/webview/lib/RecipeWebview.js')
-rw-r--r--src/webview/lib/RecipeWebview.js67
1 files changed, 49 insertions, 18 deletions
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js
index 2bd6bad8d..f1d493e7c 100644
--- a/src/webview/lib/RecipeWebview.js
+++ b/src/webview/lib/RecipeWebview.js
@@ -1,12 +1,14 @@
1import { ipcRenderer } from 'electron'; 1import { ipcRenderer } from 'electron';
2import { exists, pathExistsSync, readFileSync } from 'fs-extra'; 2import { BrowserWindow } from '@electron/remote';
3import { pathExistsSync, readFileSync, existsSync } from 'fs-extra';
3 4
4const debug = require('debug')('Ferdi:Plugin:RecipeWebview'); 5const debug = require('debug')('Ferdi:Plugin:RecipeWebview');
5 6
6class RecipeWebview { 7class RecipeWebview {
7 constructor(badgeHandler, notificationsHandler) { 8 constructor(badgeHandler, notificationsHandler, sessionHandler) {
8 this.badgeHandler = badgeHandler; 9 this.badgeHandler = badgeHandler;
9 this.notificationsHandler = notificationsHandler; 10 this.notificationsHandler = notificationsHandler;
11 this.sessionHandler = sessionHandler;
10 12
11 ipcRenderer.on('poll', () => { 13 ipcRenderer.on('poll', () => {
12 this.loopFunc(); 14 this.loopFunc();
@@ -23,6 +25,16 @@ class RecipeWebview {
23 25
24 darkModeHandler = false; 26 darkModeHandler = false;
25 27
28 // TODO Remove this once we implement a proper wrapper.
29 get ipcRenderer() {
30 return ipcRenderer;
31 }
32
33 // TODO Remove this once we implement a proper wrapper.
34 get BrowserWindow() {
35 return BrowserWindow;
36 }
37
26 /** 38 /**
27 * Initialize the loop 39 * Initialize the loop
28 * 40 *
@@ -35,12 +47,12 @@ class RecipeWebview {
35 /** 47 /**
36 * Set the unread message badge 48 * Set the unread message badge
37 * 49 *
38 * @param {int} direct Set the count of direct messages 50 * @param {string | number | undefined | null} direct Set the count of direct messages
39 * eg. Slack direct mentions, or a 51 * eg. Slack direct mentions, or a
40 * message to @channel 52 * message to @channel
41 * @param {int} indirect Set a badge that defines there are 53 * @param {string | number | undefined | null} indirect Set a badge that defines there are
42 * new messages but they do not involve 54 * new messages but they do not involve
43 * me directly to me eg. in a channel 55 * me directly to me eg. in a channel
44 */ 56 */
45 setBadge(direct = 0, indirect = 0) { 57 setBadge(direct = 0, indirect = 0) {
46 this.badgeHandler.setBadge(direct, indirect); 58 this.badgeHandler.setBadge(direct, indirect);
@@ -62,12 +74,13 @@ class RecipeWebview {
62 * be an absolute path to the file 74 * be an absolute path to the file
63 */ 75 */
64 injectCSS(...files) { 76 injectCSS(...files) {
65 files.forEach(async (file) => { 77 // eslint-disable-next-line unicorn/no-array-for-each
78 files.forEach(file => {
66 if (pathExistsSync(file)) { 79 if (pathExistsSync(file)) {
67 const styles = document.createElement('style'); 80 const styles = document.createElement('style');
68 styles.innerHTML = readFileSync(file, 'utf8'); 81 styles.innerHTML = readFileSync(file, 'utf8');
69 82
70 document.querySelector('head').appendChild(styles); 83 document.querySelector('head').append(styles);
71 84
72 debug('Append styles', styles); 85 debug('Append styles', styles);
73 } 86 }
@@ -75,14 +88,16 @@ class RecipeWebview {
75 } 88 }
76 89
77 injectJSUnsafe(...files) { 90 injectJSUnsafe(...files) {
78 Promise.all(files.map(async (file) => { 91 Promise.all(
79 if (await exists(file)) { 92 files.map(file => {
80 return readFileSync(file, 'utf8'); 93 if (existsSync(file)) {
81 } 94 return readFileSync(file, 'utf8');
82 debug('Script not found', file); 95 }
83 return null; 96 debug('Script not found', file);
84 })).then(async (scripts) => { 97 return null;
85 const scriptsFound = scripts.filter((script) => script !== null); 98 }),
99 ).then(scripts => {
100 const scriptsFound = scripts.filter(script => script !== null);
86 if (scriptsFound.length > 0) { 101 if (scriptsFound.length > 0) {
87 debug('Inject scripts to main world', scriptsFound); 102 debug('Inject scripts to main world', scriptsFound);
88 ipcRenderer.sendToHost('inject-js-unsafe', ...scriptsFound); 103 ipcRenderer.sendToHost('inject-js-unsafe', ...scriptsFound);
@@ -110,6 +125,22 @@ class RecipeWebview {
110 fn(); 125 fn();
111 } 126 }
112 } 127 }
128
129 clearStorageData(serviceId, targetsToClear) {
130 ipcRenderer.send('clear-storage-data', { serviceId, targetsToClear });
131 }
132
133 releaseServiceWorkers() {
134 this.sessionHandler.releaseServiceWorkers();
135 }
136
137 setAvatarImage(avatarUrl) {
138 ipcRenderer.sendToHost('avatar', avatarUrl);
139 }
140
141 openNewWindow(url) {
142 ipcRenderer.sendToHost('new-window', url);
143 }
113} 144}
114 145
115export default RecipeWebview; 146export default RecipeWebview;