aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/todos/preload.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/todos/preload.ts')
-rw-r--r--src/features/todos/preload.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/features/todos/preload.ts b/src/features/todos/preload.ts
new file mode 100644
index 000000000..31d473051
--- /dev/null
+++ b/src/features/todos/preload.ts
@@ -0,0 +1,59 @@
1import { ipcRenderer } from 'electron';
2import { IPC } from './constants';
3
4const debug = require('debug')('Ferdi:feature:todos:preload');
5
6debug('Preloading Todos Webview');
7
8let hostMessageListener = ({ action }) => {
9 switch (action) {
10 case 'todos:initialize-as-service':
11 ipcRenderer.sendToHost('hello');
12 break;
13 default:
14 }
15};
16
17window['ferdi'] = {
18 onInitialize(ipcHostMessageListener) {
19 hostMessageListener = ipcHostMessageListener;
20 ipcRenderer.sendToHost(IPC.TODOS_CLIENT_CHANNEL, {
21 action: 'todos:initialized',
22 });
23 },
24 sendToHost(message) {
25 ipcRenderer.sendToHost(IPC.TODOS_CLIENT_CHANNEL, message);
26 },
27};
28
29ipcRenderer.on(IPC.TODOS_HOST_CHANNEL, (event, message) => {
30 debug('Received host message', event, message);
31 hostMessageListener(message);
32});
33
34if (window.location.href === 'https://app.franztodos.com/login/') {
35 // Insert info element informing about Franz accounts
36 const infoElement = document.createElement('p');
37 infoElement.textContent = `You are using Franz's official Todo Service.
38This service will only work with accounts registered with Franz - no Ferdi accounts will work here!
39If you do not have a Franz account you can change the Todo service by going into Ferdi's settings and changing the "Todo server".
40You can choose any service as this Todo server, e.g. Todoist or Apple Notes.`;
41
42 // Franz Todos uses React. Because of this we can't directly insert the element into the page
43 // but we have to wait for React to finish rendering the login page
44 let numChecks = 0;
45 const waitForReact = setInterval(() => {
46 const textElement = document.querySelector('p');
47 if (textElement) {
48 clearInterval(waitForReact);
49 textElement.parentElement?.insertBefore(infoElement, textElement);
50 } else {
51 numChecks += 1;
52
53 // Stop after ~10 seconds. We are probably not on the login page
54 if (numChecks > 1000) {
55 clearInterval(waitForReact);
56 }
57 }
58 }, 10);
59}