Commit e9a36337 authored by Tasso Evangelista's avatar Tasso Evangelista

Break preload scripts into smaller modules

parent f5526526
/* globals Meteor, RocketChat, Tracker, UserPresence */
import { ipcRenderer } from 'electron';
import setupEventsPreload from './preload/events';
import setupJitsiPreload from './preload/jitsi';
import setupLinksPreload from './preload/links';
import setupNotificationsPreload from './preload/notifications';
import setupSidebarPreload from './preload/sidebar';
import SpellCheck from './preload/SpellCheck';
const { ipcRenderer, shell } = require('electron');
const path = require('path');
const url = require('url');
const JitsiMeetElectron = require('./preload/JitsiMeetElectron');
const Notification = require('./preload/Notification');
const SpellCheck = require('./preload/SpellCheck');
const i18n = require('./i18n/index');
window.JitsiMeetElectron = JitsiMeetElectron;
window.Notification = Notification;
window.i18n = i18n;
setInterval(() => {
const jitsiIframe = document.querySelector('iframe[id^=jitsiConference]');
if (!jitsiIframe) {
return;
}
jitsiIframe.contentWindow.JitsiMeetElectron = JitsiMeetElectron;
}, 1000);
window.open = ((defaultWindowOpen) => (href, frameName, features) => {
if (url.parse(href).host === RocketChat.settings.get('Jitsi_Domain')) {
features = [
features,
'nodeIntegration=true',
`preload=${ path.join(__dirname, './preload.js') }`,
].filter((x) => Boolean(x)).join(',');
}
return defaultWindowOpen(href, frameName, features);
})(window.open);
setupEventsPreload(window);
setupJitsiPreload(window);
setupLinksPreload(window);
setupNotificationsPreload(window);
setupSidebarPreload(window);
window.reloadServer = () => ipcRenderer.sendToHost('reload-server');
for (const eventName of ['unread-changed', 'get-sourceId', 'user-status-manually-set']) {
window.addEventListener(eventName, (event) => ipcRenderer.sendToHost(eventName, event.detail));
}
const changeSidebarColor = () => {
const sidebar = document.querySelector('.sidebar');
if (sidebar) {
const { color, background } = window.getComputedStyle(sidebar);
const sidebarItem = sidebar.querySelector('.sidebar-item');
const itemColor = sidebarItem && window.getComputedStyle(sidebarItem).color;
ipcRenderer.sendToHost('sidebar-background', { color: itemColor || color, background });
return;
}
const fullpage = document.querySelector('.full-page');
if (fullpage) {
const { color, background } = window.getComputedStyle(fullpage);
ipcRenderer.sendToHost('sidebar-background', { color, background });
return;
}
window.requestAnimationFrame(changeSidebarColor);
};
ipcRenderer.on('request-sidebar-color', changeSidebarColor);
window.addEventListener('load', () => {
if (!Meteor) {
return;
}
Meteor.startup(() => {
Tracker.autorun(() => {
const siteName = RocketChat.settings.get('Site_Name');
if (siteName) {
ipcRenderer.sendToHost('title-changed', siteName);
}
});
});
const INTERVAL = 10000; // 10s
setInterval(() => {
try {
const idleTime = ipcRenderer.sendSync('getSystemIdleTime');
if (idleTime < INTERVAL) {
UserPresence.setOnline();
}
} catch (e) {
console.error(`Error getting system idle time: ${ e }`);
}
}, INTERVAL);
document.addEventListener('click', (event) => {
const anchorElement = event.target.closest('a');
if (!anchorElement) {
return;
}
const { href } = anchorElement;
// Check href matching Rochet.Chat URL
if (RegExp(`^${ Meteor.absoluteUrl() }`).test(href)) {
return;
}
// Check if is file upload link
if (/^\/file-upload\//.test(href) && !anchorElement.hasAttribute('download')) {
const tempElement = document.createElement('a');
tempElement.href = href;
tempElement.download = 'download';
tempElement.click();
return;
}
// Check href matching relative URL
if (!/^([a-z]+:)?\/\//.test(href)) {
return;
}
if (/^file:\/\/.+/.test(href)) {
const item = href.slice(6);
shell.showItemInFolder(item);
event.preventDefault();
return;
}
shell.openExternal(href);
event.preventDefault();
}, true);
});
// Prevent redirect to url when dragging in
document.addEventListener('dragover', (event) => event.preventDefault());
document.addEventListener('drop', (event) => event.preventDefault());
window.document.addEventListener('dragover', (e) => e.preventDefault());
window.document.addEventListener('drop', (e) => e.preventDefault());
const spellChecker = new SpellCheck();
spellChecker.enable();
import { desktopCapturer } from 'electron';
const JitsiMeetElectron = {
obtainDesktopStreams(callback, errorCallback, options = {}) {
console.log(this);
desktopCapturer.getSources(options, (error, sources) => {
if (error) {
errorCallback(error);
return;
}
callback(sources);
});
},
};
export default JitsiMeetElectron;
import { ipcRenderer } from 'electron';
const handleWindowEventTriggered = (window, eventName) => (e) => {
ipcRenderer.sendToHost(eventName, e.detail);
};
const handleTitleChange = (window) => {
const { Meteor, RocketChat, Tracker } = window;
if (!Meteor || !RocketChat || !Tracker) {
return;
}
Meteor.startup(() => {
Tracker.autorun(() => {
const siteName = RocketChat.settings.get('Site_Name');
if (siteName) {
ipcRenderer.sendToHost('title-changed', siteName);
}
});
});
};
const handleUserPresenceChange = (window) => {
const { Meteor, UserPresence } = window;
if (!Meteor || !UserPresence) {
return;
}
const idleDetectionInterval = 10000;
setInterval(() => {
try {
const idleTime = ipcRenderer.sendSync('getSystemIdleTime');
if (idleTime < idleDetectionInterval) {
UserPresence.setOnline();
}
} catch (e) {
console.error(`Error getting system idle time: ${ e }`);
}
}, idleDetectionInterval);
};
const preventUrlLoadingOnDrop = ({ document }) => {
document.addEventListener('dragover', (e) => e.preventDefault());
document.addEventListener('drop', (e) => e.preventDefault());
};
export default (window) => {
const eventsListened = ['unread-changed', 'get-sourceId', 'user-status-manually-set'];
for (const eventName of eventsListened) {
window.addEventListener(eventName, handleWindowEventTriggered(window, eventName));
}
window.addEventListener('load', () => {
handleTitleChange(window);
handleUserPresenceChange(window);
});
preventUrlLoadingOnDrop(window);
};
import { desktopCapturer } from 'electron';
import path from 'path';
import url from 'url';
const JitsiMeetElectron = {
obtainDesktopStreams(callback, errorCallback, options = {}) {
desktopCapturer.getSources(options, (error, sources) => {
if (error) {
errorCallback(error);
return;
}
callback(sources);
});
},
};
const wrapWindowOpen = (defaultWindowOpen) => (href, frameName, features) => {
const { RocketChat } = window;
if (RocketChat && url.parse(href).host === RocketChat.settings.get('Jitsi_Domain')) {
features = [
features,
'nodeIntegration=true',
`preload=${ path.join(__dirname, './preload.js') }`,
].filter((x) => Boolean(x)).join(',');
}
return defaultWindowOpen(href, frameName, features);
};
const pollJitsiIframe = ({ document }) => {
const jitsiIframe = document.querySelector('iframe[id^=jitsiConference]');
if (!jitsiIframe) {
return;
}
jitsiIframe.contentWindow.JitsiMeetElectron = JitsiMeetElectron;
};
export default (window) => {
window.JitsiMeetElectron = JitsiMeetElectron;
window.open = wrapWindowOpen(window.open);
window.addEventListener('load', () => {
setInterval(() => pollJitsiIframe(window), 1000);
});
};
import { shell } from 'electron';
const handleAnchorClick = ({ Meteor }) => (event) => {
const a = event.target.closest('a');
if (!a) {
return;
}
const href = a.getAttribute('href');
const download = a.hasAttribute('download');
const isFileUpload = /^\/file-upload\//.test(href) && !download;
if (isFileUpload) {
const clone = a.cloneNode();
clone.setAttribute('download', 'download');
clone.click();
event.preventDefault();
return;
}
const isLocalFilePath = /^file:\/\/.+/.test(href);
if (isLocalFilePath) {
const filePath = href.slice(6);
shell.showItemInFolder(filePath);
event.preventDefault();
return;
}
const isInsideDomain = Meteor && RegExp(`^${ Meteor.absoluteUrl() }`).test(href);
const isRelative = !/^([a-z]+:)?\/\//.test(href);
if (isInsideDomain || isRelative) {
return;
}
shell.openExternal(href);
event.preventDefault();
};
export default (window) => {
window.addEventListener('load', () => {
window.document.addEventListener('click', handleAnchorClick(window), true);
});
};
const { ipcRenderer, nativeImage } = require('electron');
const { EventEmitter } = require('events');
const jetpack = require('fs-jetpack');
const tmp = require('tmp');
import { ipcRenderer, nativeImage } from 'electron';
import { EventEmitter } from 'events';
import jetpack from 'fs-jetpack';
import tmp from 'tmp';
const instances = new Map();
......@@ -43,9 +43,7 @@ class Notification extends EventEmitter {
}
}
module.exports = Notification;
ipcRenderer.on('notification-shown', (event, id) => {
const handleNotificationShown = (event, id) => {
const notification = instances.get(id);
if (!notification) {
return;
......@@ -53,9 +51,9 @@ ipcRenderer.on('notification-shown', (event, id) => {
typeof notification.onshow === 'function' && notification.onshow.call(notification);
notification.emit('show');
});
};
ipcRenderer.on('notification-clicked', (event, id) => {
const handleNotificationClicked = (event, id) => {
const notification = instances.get(id);
if (!notification) {
return;
......@@ -66,9 +64,9 @@ ipcRenderer.on('notification-clicked', (event, id) => {
typeof notification.onclick === 'function' && notification.onclick.call(notification);
notification.emit('click');
});
};
ipcRenderer.on('notification-closed', (event, id) => {
const handleNotificationClosed = (event, id) => {
const notification = instances.get(id);
if (!notification) {
return;
......@@ -76,4 +74,12 @@ ipcRenderer.on('notification-closed', (event, id) => {
typeof notification.onclose === 'function' && notification.onclose.call(notification);
notification.emit('close');
});
};
export default (window) => {
window.Notification = Notification;
ipcRenderer.on('notification-shown', handleNotificationShown);
ipcRenderer.on('notification-clicked', handleNotificationClicked);
ipcRenderer.on('notification-closed', handleNotificationClosed);
};
import { ipcRenderer } from 'electron';
const requestSidebarColor = ({ document, requestAnimationFrame }) => function pollSidebarColor() {
const sidebar = document.querySelector('.sidebar');
if (sidebar) {
const { color, background } = window.getComputedStyle(sidebar);
const sidebarItem = sidebar.querySelector('.sidebar-item');
const itemColor = sidebarItem && window.getComputedStyle(sidebarItem).color;
ipcRenderer.sendToHost('sidebar-background', { color: itemColor || color, background });
return;
}
const fullpage = document.querySelector('.full-page');
if (fullpage) {
const { color, background } = window.getComputedStyle(fullpage);
ipcRenderer.sendToHost('sidebar-background', { color, background });
return;
}
requestAnimationFrame(pollSidebarColor);
};
export default (window) => {
ipcRenderer.on('request-sidebar-color', requestSidebarColor(window));
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment