Skip to content

Commit

Permalink
preload: Use IPC for logout, shortcut, showNotificationSettings.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
  • Loading branch information
andersk committed Mar 31, 2020
1 parent c843e17 commit 20a6c5d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
8 changes: 4 additions & 4 deletions app/renderer/js/components/webview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { remote } from 'electron';
import { ipcRenderer, remote } from 'electron';

import path from 'path';
import fs from 'fs';
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class WebView extends BaseComponent {
}

showNotificationSettings(): void {
this.$el.executeJavaScript('showNotificationSettings()');
ipcRenderer.sendTo(this.$el.getWebContentsId(), 'show-notification-settings');
}

show(): void {
Expand Down Expand Up @@ -245,11 +245,11 @@ export default class WebView extends BaseComponent {
}

logOut(): void {
this.$el.executeJavaScript('logout()');
ipcRenderer.sendTo(this.$el.getWebContentsId(), 'logout');
}

showShortcut(): void {
this.$el.executeJavaScript('shortcut()');
ipcRenderer.sendTo(this.$el.getWebContentsId(), 'shortcut');
}

openDevTools(): void {
Expand Down
24 changes: 5 additions & 19 deletions app/renderer/js/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import * as ConfigUtil from './utils/config-util';

import * as NetworkError from './pages/network';

interface PatchedGlobal extends NodeJS.Global {
logout: () => void;
shortcut: () => void;
showNotificationSettings: () => void;
}

const globalPatched = global as PatchedGlobal;

// eslint-disable-next-line import/no-unassigned-import
import './notification';

Expand All @@ -29,16 +21,16 @@ declare let window: ZulipWebWindow;
import electron_bridge from './electron-bridge';
window.electron_bridge = electron_bridge;

const logout = (): void => {
ipcRenderer.on('logout', () => {
// Create the menu for the below
const dropdown: HTMLElement = document.querySelector('.dropdown-toggle');
dropdown.click();

const nodes: NodeListOf<HTMLElement> = document.querySelectorAll('.dropdown-menu li:last-child a');
nodes[nodes.length - 1].click();
};
});

const shortcut = (): void => {
ipcRenderer.on('shortcut', () => {
// Create the menu for the below
const node: HTMLElement = document.querySelector('a[data-overlay-trigger=keyboard-shortcuts]');
// Additional check
Expand All @@ -49,9 +41,9 @@ const shortcut = (): void => {
const dropdown: HTMLElement = document.querySelector('.dropdown-toggle');
dropdown.click();
}
};
});

const showNotificationSettings = (): void => {
ipcRenderer.on('show-notification-settings', () => {
// Create the menu for the below
const dropdown: HTMLElement = document.querySelector('.dropdown-toggle');
dropdown.click();
Expand All @@ -65,12 +57,6 @@ const showNotificationSettings = (): void => {
setTimeout(() => {
notificationItem[2].click();
}, 100);
};

process.once('loaded', (): void => {
globalPatched.logout = logout;
globalPatched.shortcut = shortcut;
globalPatched.showNotificationSettings = showNotificationSettings;
});

// To prevent failing this script on linux we need to load it after the document loaded
Expand Down

0 comments on commit 20a6c5d

Please sign in to comment.