diff options
Diffstat (limited to 'src/webview/lib')
-rw-r--r-- | src/webview/lib/RecipeWebview.js | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js index b8fe7dc52..96caa125e 100644 --- a/src/webview/lib/RecipeWebview.js +++ b/src/webview/lib/RecipeWebview.js | |||
@@ -1,14 +1,12 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { ipcRenderer } from 'electron'; |
2 | import { pathExistsSync, readFile } from 'fs-extra'; | 2 | import { exists, pathExistsSync, readFileSync } from 'fs-extra'; |
3 | 3 | ||
4 | const debug = require('debug')('Ferdi:Plugin:RecipeWebview'); | 4 | const debug = require('debug')('Ferdi:Plugin:RecipeWebview'); |
5 | 5 | ||
6 | class RecipeWebview { | 6 | class RecipeWebview { |
7 | constructor() { | 7 | constructor(badgeHandler, notificationsHandler) { |
8 | this.countCache = { | 8 | this.badgeHandler = badgeHandler; |
9 | direct: 0, | 9 | this.notificationsHandler = notificationsHandler; |
10 | indirect: 0, | ||
11 | }; | ||
12 | 10 | ||
13 | ipcRenderer.on('poll', () => { | 11 | ipcRenderer.on('poll', () => { |
14 | this.loopFunc(); | 12 | this.loopFunc(); |
@@ -45,24 +43,7 @@ class RecipeWebview { | |||
45 | * me directly to me eg. in a channel | 43 | * me directly to me eg. in a channel |
46 | */ | 44 | */ |
47 | setBadge(direct = 0, indirect = 0) { | 45 | setBadge(direct = 0, indirect = 0) { |
48 | if (this.countCache.direct === direct | 46 | this.badgeHandler.setBadge(direct, indirect); |
49 | && this.countCache.indirect === indirect) return; | ||
50 | |||
51 | // Parse number to integer | ||
52 | // This will correct errors that recipes may introduce, e.g. | ||
53 | // by sending a String instead of an integer | ||
54 | const directInt = parseInt(direct, 10); | ||
55 | const indirectInt = parseInt(indirect, 10); | ||
56 | |||
57 | const count = { | ||
58 | direct: Math.max(directInt, 0), | ||
59 | indirect: Math.max(indirectInt, 0), | ||
60 | }; | ||
61 | |||
62 | ipcRenderer.sendToHost('message-counts', count); | ||
63 | Object.assign(this.countCache, count); | ||
64 | |||
65 | debug('Sending badge count to host', count); | ||
66 | } | 47 | } |
67 | 48 | ||
68 | /** | 49 | /** |
@@ -74,9 +55,8 @@ class RecipeWebview { | |||
74 | injectCSS(...files) { | 55 | injectCSS(...files) { |
75 | files.forEach(async (file) => { | 56 | files.forEach(async (file) => { |
76 | if (pathExistsSync(file)) { | 57 | if (pathExistsSync(file)) { |
77 | const data = await readFile(file, 'utf8'); | ||
78 | const styles = document.createElement('style'); | 58 | const styles = document.createElement('style'); |
79 | styles.innerHTML = data; | 59 | styles.innerHTML = readFileSync(file, 'utf8'); |
80 | 60 | ||
81 | document.querySelector('head').appendChild(styles); | 61 | document.querySelector('head').appendChild(styles); |
82 | 62 | ||
@@ -85,6 +65,22 @@ class RecipeWebview { | |||
85 | }); | 65 | }); |
86 | } | 66 | } |
87 | 67 | ||
68 | injectJSUnsafe(...files) { | ||
69 | Promise.all(files.map(async (file) => { | ||
70 | if (await exists(file)) { | ||
71 | return readFileSync(file, 'utf8'); | ||
72 | } | ||
73 | debug('Script not found', file); | ||
74 | return null; | ||
75 | })).then(async (scripts) => { | ||
76 | const scriptsFound = scripts.filter((script) => script !== null); | ||
77 | if (scriptsFound.length > 0) { | ||
78 | debug('Inject scripts to main world', scriptsFound); | ||
79 | ipcRenderer.sendToHost('inject-js-unsafe', ...scriptsFound); | ||
80 | } | ||
81 | }); | ||
82 | } | ||
83 | |||
88 | /** | 84 | /** |
89 | * Set a custom handler for turning on and off dark mode | 85 | * Set a custom handler for turning on and off dark mode |
90 | * | 86 | * |
@@ -96,7 +92,7 @@ class RecipeWebview { | |||
96 | 92 | ||
97 | onNotify(fn) { | 93 | onNotify(fn) { |
98 | if (typeof fn === 'function') { | 94 | if (typeof fn === 'function') { |
99 | window.Notification.prototype.onNotify = fn; | 95 | this.notificationsHandler.onNotify = fn; |
100 | } | 96 | } |
101 | } | 97 | } |
102 | 98 | ||