session-desktop/preload.js

268 lines
7.5 KiB
JavaScript
Raw Normal View History

const { clipboard, ipcRenderer, webFrame } = require('electron/main');
2022-04-01 06:42:50 +02:00
const { Storage } = require('./ts/util/storage');
const url = require('url');
const config = url.parse(window.location.toString(), true).query;
2022-04-01 00:58:24 +02:00
const configAny = config;
2022-04-01 06:42:50 +02:00
let title = config.name;
if (config.environment !== 'production') {
2022-04-01 06:42:50 +02:00
title += ` - ${config.environment}`;
}
if (config.appInstance) {
2022-04-01 06:42:50 +02:00
title += ` - ${config.appInstance}`;
}
2022-04-01 06:42:50 +02:00
// tslint:disable: no-require-imports no-var-requires
window.platform = process.platform;
window.getTitle = () => title;
2022-04-01 00:58:24 +02:00
window.getEnvironment = () => configAny.environment;
window.getAppInstance = () => configAny.appInstance;
window.getVersion = () => configAny.version;
window.isDev = () => config.environment === 'development';
2022-04-01 00:58:24 +02:00
window.getCommitHash = () => configAny.commitHash;
window.getNodeVersion = () => configAny.node_version;
2022-04-01 06:42:50 +02:00
2022-02-18 03:03:47 +01:00
window.sessionFeatureFlags = {
2022-04-01 06:42:50 +02:00
useOnionRequests: true,
useTestNet: Boolean(
process.env.NODE_APP_INSTANCE && process.env.NODE_APP_INSTANCE.includes('testnet')
),
useSettingsThemeSwitcher: true,
debug: {
debugFileServerRequests: false,
debugNonSnodeRequests: false,
debugOnionRequests: false,
},
};
2022-04-01 06:42:50 +02:00
2020-01-22 05:57:58 +01:00
window.versionInfo = {
2022-04-01 06:42:50 +02:00
environment: window.getEnvironment(),
version: window.getVersion(),
commitHash: window.getCommitHash(),
appInstance: window.getAppInstance(),
2020-01-22 05:57:58 +01:00
};
2022-04-01 06:42:50 +02:00
const ipc = ipcRenderer;
const localeMessages = ipc.sendSync('locale-data');
2022-04-01 06:42:50 +02:00
2020-02-26 03:30:56 +01:00
window.updateZoomFactor = () => {
2022-04-01 06:42:50 +02:00
const zoomFactor = window.getSettingValue('zoom-factor-setting') || 100;
window.setZoomFactor(zoomFactor / 100);
};
2022-04-01 06:42:50 +02:00
2020-02-26 03:30:56 +01:00
window.setZoomFactor = number => {
2022-04-01 06:42:50 +02:00
webFrame.setZoomFactor(number);
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
// Set the password for the database
window.setPassword = async (passPhrase, oldPhrase) =>
new Promise((resolve, reject) => {
2022-04-01 00:58:24 +02:00
ipc.once('set-password-response', (_event, error) => {
2022-04-01 06:42:50 +02:00
if (error) {
reject(error);
2022-04-01 00:58:24 +02:00
return;
2022-04-01 06:42:50 +02:00
}
resolve(undefined);
return;
2019-01-16 05:44:13 +01:00
});
ipc.send('set-password', passPhrase, oldPhrase);
2022-04-01 06:42:50 +02:00
});
window.setStartInTray = async startInTray =>
new Promise((resolve, reject) => {
ipc.once('start-in-tray-on-start-response', (_event, error) => {
2022-04-01 06:42:50 +02:00
if (error) {
reject(error);
2022-04-01 00:58:24 +02:00
return;
2022-04-01 06:42:50 +02:00
}
resolve();
return;
});
ipc.send('start-in-tray-on-start', startInTray);
2022-04-01 06:42:50 +02:00
});
2022-04-01 00:58:24 +02:00
window.getStartInTray = async () => {
2022-04-01 06:42:50 +02:00
return new Promise(resolve => {
ipc.once('get-start-in-tray-response', (_event, value) => {
resolve(value);
2022-04-01 00:58:24 +02:00
});
2022-04-01 06:42:50 +02:00
ipc.send('get-start-in-tray');
});
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
window.getOpengroupPruning = async () => {
return new Promise(resolve => {
ipc.once('get-opengroup-pruning-response', (_event, value) => {
resolve(value);
});
ipc.send('get-opengroup-pruning');
});
};
window.setOpengroupPruning = async opengroupPruning =>
new Promise((resolve, reject) => {
ipc.once('set-opengroup-pruning-response', (_event, error) => {
if (error) {
reject(error);
return;
}
resolve();
return;
});
ipc.send('set-opengroup-pruning', opengroupPruning);
});
2022-01-18 05:21:36 +01:00
window._ = require('lodash');
2022-04-01 06:42:50 +02:00
// We never do these in our code, so we'll prevent it everywhere
window.open = () => null;
2022-04-01 06:42:50 +02:00
// eslint-disable-next-line no-eval, no-multi-assign
2018-05-24 18:32:18 +02:00
window.eval = global.eval = () => null;
2022-04-01 06:42:50 +02:00
2018-04-03 21:03:57 +02:00
window.drawAttention = () => {
2022-04-01 06:42:50 +02:00
// window.log.debug('draw attention');
ipc.send('draw-attention');
2018-04-03 21:03:57 +02:00
};
2018-04-03 21:03:57 +02:00
window.showWindow = () => {
2022-04-01 06:42:50 +02:00
window.log.info('show window');
ipc.send('show-window');
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
2022-04-01 00:58:24 +02:00
window.setAutoHideMenuBar = autoHide => {
2022-04-01 06:42:50 +02:00
ipc.send('set-auto-hide-menu-bar', autoHide);
2022-04-01 00:58:24 +02:00
};
window.setMenuBarVisibility = visibility => {
2022-04-01 06:42:50 +02:00
ipc.send('set-menu-bar-visibility', visibility);
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
2018-04-03 21:03:57 +02:00
window.restart = () => {
2022-04-01 06:42:50 +02:00
window.log.info('restart');
ipc.send('restart');
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
2022-04-01 00:58:24 +02:00
window.closeAbout = () => {
2022-04-01 06:42:50 +02:00
ipc.send('close-about');
2022-04-01 00:58:24 +02:00
};
window.readyForUpdates = () => {
2022-04-01 06:42:50 +02:00
ipc.send('ready-for-updates');
2018-04-03 21:03:57 +02:00
};
2022-04-01 06:42:50 +02:00
ipc.on('get-theme-setting', () => {
2022-04-01 06:42:50 +02:00
const theme = window.Events.getThemeSetting();
ipc.send('get-success-theme-setting', theme);
});
2022-04-01 06:42:50 +02:00
2020-02-27 23:49:03 +01:00
window.getSettingValue = (settingID, comparisonValue = null) => {
2022-04-01 06:42:50 +02:00
// Comparison value allows you to pull boolean values from any type.
// Eg. window.getSettingValue('theme', 'classic-dark')
// returns 'false' when the value is 'classic-light'.
2022-04-01 06:42:50 +02:00
// We need to get specific settings from the main process
if (settingID === 'media-permissions') {
return window.getMediaPermissions();
} else if (settingID === 'call-media-permissions') {
return window.getCallMediaPermissions();
} else if (settingID === 'auto-update') {
return window.getAutoUpdateEnabled();
}
const settingVal = Storage.get(settingID);
return comparisonValue ? !!settingVal === comparisonValue : settingVal;
};
2022-04-01 00:58:24 +02:00
window.setSettingValue = async (settingID, value) => {
2022-04-01 06:42:50 +02:00
// For auto updating we need to pass the value to the main process
if (settingID === 'auto-update') {
window.setAutoUpdateEnabled(value);
return;
}
await Storage.put(settingID, value);
};
2020-03-10 01:05:26 +01:00
window.getMediaPermissions = () => ipc.sendSync('get-media-permissions');
2022-04-01 00:58:24 +02:00
window.setMediaPermissions = value => {
2022-04-01 06:42:50 +02:00
ipc.send('set-media-permissions', !!value);
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
window.getCallMediaPermissions = () => ipc.sendSync('get-call-media-permissions');
2022-04-01 00:58:24 +02:00
window.setCallMediaPermissions = value => {
2022-04-01 06:42:50 +02:00
ipc.send('set-call-media-permissions', !!value);
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
2022-04-01 00:58:24 +02:00
window.askForMediaAccess = () => {
2022-04-01 06:42:50 +02:00
ipc.send('media-access');
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
// Auto update setting
2020-03-10 04:58:44 +01:00
window.getAutoUpdateEnabled = () => ipc.sendSync('get-auto-update-setting');
2022-04-01 00:58:24 +02:00
window.setAutoUpdateEnabled = value => {
2022-04-01 06:42:50 +02:00
ipc.send('set-auto-update-setting', !!value);
2022-04-01 00:58:24 +02:00
};
2022-04-01 06:42:50 +02:00
ipc.on('get-ready-for-shutdown', async () => {
2022-04-01 06:42:50 +02:00
const { shutdown } = window.Events || {};
if (!shutdown) {
window.log.error('preload shutdown handler: shutdown method not found');
ipc.send('now-ready-for-shutdown');
return;
}
try {
await shutdown();
ipc.send('now-ready-for-shutdown');
} catch (error) {
ipc.send('now-ready-for-shutdown', error && error.stack ? error.stack : error);
}
});
2022-04-01 06:42:50 +02:00
// We pull these dependencies in now, from here, because they have Node.js dependencies
require('./ts/util/logging');
if (config.proxyUrl) {
2022-04-01 06:42:50 +02:00
window.log.info('Using provided proxy url');
2018-04-03 21:03:57 +02:00
}
window.nodeSetImmediate = setImmediate;
2022-04-01 06:42:50 +02:00
const data = require('./ts/data/dataInit');
const { setupi18n } = require('./ts/util/i18n');
window.Signal = data.initData();
const { getConversationController } = require('./ts/session/conversations/ConversationController');
window.getConversationController = getConversationController;
2022-04-01 06:42:50 +02:00
// Linux seems to periodically let the event loop stop, so this is a global workaround
2018-04-03 21:03:57 +02:00
setInterval(() => {
2022-04-01 06:42:50 +02:00
// tslint:disable-next-line: no-empty
window.nodeSetImmediate(() => {});
2018-04-03 21:03:57 +02:00
}, 1000);
2022-04-01 06:42:50 +02:00
window.React = require('react');
window.ReactDOM = require('react-dom');
2022-04-01 06:42:50 +02:00
window.clipboard = clipboard;
window.getSeedNodeList = () =>
window.sessionFeatureFlags.useTestNet
? ['http://public.loki.foundation:38157']
: [
'https://storage.seed1.loki.network:4433/',
'https://storage.seed3.loki.network:4433/',
'https://public.loki.foundation:4433/',
];
2022-04-01 06:42:50 +02:00
2021-03-19 03:51:03 +01:00
const { locale: localFromEnv } = config;
window.i18n = setupi18n(localFromEnv || 'en', localeMessages);
2022-04-01 06:42:50 +02:00
2020-03-27 06:43:19 +01:00
window.addEventListener('contextmenu', e => {
2022-04-06 08:35:21 +02:00
const editable = e && e.target.closest('textarea, input, [contenteditable="true"]');
const link = e && e.target.closest('a');
const selection = Boolean(window && window.getSelection() && window.getSelection().toString());
2022-04-01 06:42:50 +02:00
if (!editable && !selection && !link) {
e.preventDefault();
}
});