You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/preload.js

245 lines
6.5 KiB
JavaScript

2 years ago
const { app, clipboard, ipcRenderer, webFrame } = require('electron/main');
const { Storage } = require('./ts/util/storage');
const url = require('url');
const path = require('path');
const config = url.parse(window.location.toString(), true).query;
const configAny = config;
2 years ago
let title = config.name;
if (config.environment !== 'production') {
2 years ago
title += ` - ${config.environment}`;
}
if (config.appInstance) {
2 years ago
title += ` - ${config.appInstance}`;
}
2 years ago
// tslint:disable: no-require-imports no-var-requires
global.dcodeIO = global.dcodeIO || {};
global.dcodeIO.ByteBuffer = require('bytebuffer');
2 years ago
window.platform = process.platform;
window.getTitle = () => title;
window.getEnvironment = () => configAny.environment;
window.getAppInstance = () => configAny.appInstance;
window.getVersion = () => configAny.version;
window.isDev = () => config.environment === 'development';
window.getCommitHash = () => configAny.commitHash;
window.getNodeVersion = () => configAny.node_version;
2 years ago
window.sessionFeatureFlags = {
2 years ago
useOnionRequests: true,
useCallMessage: true,
};
2 years ago
4 years ago
window.versionInfo = {
2 years ago
environment: window.getEnvironment(),
version: window.getVersion(),
commitHash: window.getCommitHash(),
appInstance: window.getAppInstance(),
4 years ago
};
2 years ago
const ipc = ipcRenderer;
const localeMessages = ipc.sendSync('locale-data');
2 years ago
window.updateZoomFactor = () => {
2 years ago
const zoomFactor = window.getSettingValue('zoom-factor-setting') || 100;
window.setZoomFactor(zoomFactor / 100);
};
2 years ago
window.setZoomFactor = number => {
2 years ago
webFrame.setZoomFactor(number);
};
2 years ago
// Set the password for the database
window.setPassword = async (passPhrase, oldPhrase) =>
new Promise((resolve, reject) => {
ipc.once('set-password-response', (_event, error) => {
2 years ago
if (error) {
reject(error);
return;
2 years ago
}
resolve(undefined);
return;
5 years ago
});
ipc.send('set-password', passPhrase, oldPhrase);
2 years ago
});
window.setStartInTray = async startInTray =>
new Promise((resolve, reject) => {
ipc.once('start-in-tray-on-start-response', (_event, error) => {
2 years ago
if (error) {
reject(error);
return;
2 years ago
}
resolve();
return;
});
ipc.send('start-in-tray-on-start', startInTray);
2 years ago
});
window.getStartInTray = async () => {
2 years ago
return new Promise(resolve => {
ipc.once('get-start-in-tray-response', (_event, value) => {
resolve(value);
});
2 years ago
ipc.send('get-start-in-tray');
});
};
2 years ago
window._ = require('lodash');
2 years ago
// We never do these in our code, so we'll prevent it everywhere
window.open = () => null;
2 years ago
// eslint-disable-next-line no-eval, no-multi-assign
window.eval = global.eval = () => null;
2 years ago
window.drawAttention = () => {
2 years ago
// window.log.debug('draw attention');
ipc.send('draw-attention');
};
window.showWindow = () => {
2 years ago
window.log.info('show window');
ipc.send('show-window');
};
2 years ago
window.setAutoHideMenuBar = autoHide => {
2 years ago
ipc.send('set-auto-hide-menu-bar', autoHide);
};
window.setMenuBarVisibility = visibility => {
2 years ago
ipc.send('set-menu-bar-visibility', visibility);
};
2 years ago
window.restart = () => {
2 years ago
window.log.info('restart');
ipc.send('restart');
};
2 years ago
window.closeAbout = () => {
2 years ago
ipc.send('close-about');
};
window.readyForUpdates = () => {
2 years ago
ipc.send('ready-for-updates');
};
2 years ago
ipc.on('get-theme-setting', () => {
2 years ago
const theme = window.Events.getThemeSetting();
ipc.send('get-success-theme-setting', theme);
});
2 years ago
window.getSettingValue = (settingID, comparisonValue = null) => {
2 years ago
// Comparison value allows you to pull boolean values from any type.
// Eg. window.getSettingValue('theme', 'light')
// returns 'false' when the value is 'dark'.
// 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;
};
window.setSettingValue = async (settingID, value) => {
2 years ago
// 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);
};
window.getMediaPermissions = () => ipc.sendSync('get-media-permissions');
window.setMediaPermissions = value => {
2 years ago
ipc.send('set-media-permissions', !!value);
};
2 years ago
window.getCallMediaPermissions = () => ipc.sendSync('get-call-media-permissions');
window.setCallMediaPermissions = value => {
2 years ago
ipc.send('set-call-media-permissions', !!value);
};
2 years ago
window.askForMediaAccess = () => {
2 years ago
ipc.send('media-access');
};
2 years ago
// Auto update setting
4 years ago
window.getAutoUpdateEnabled = () => ipc.sendSync('get-auto-update-setting');
window.setAutoUpdateEnabled = value => {
2 years ago
ipc.send('set-auto-update-setting', !!value);
};
2 years ago
ipc.on('get-ready-for-shutdown', async () => {
2 years ago
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);
}
});
2 years ago
// We pull these dependencies in now, from here, because they have Node.js dependencies
require('./ts/util/logging');
if (config.proxyUrl) {
2 years ago
window.log.info('Using provided proxy url');
}
window.nodeSetImmediate = setImmediate;
2 years ago
const data = require('./ts/data/dataInit');
const { setupi18n } = require('./ts/util/i18n');
window.Signal = data.initData();
// Linux seems to periodically let the event loop stop, so this is a global workaround
setInterval(() => {
2 years ago
// tslint:disable-next-line: no-empty
window.nodeSetImmediate(() => {});
}, 1000);
2 years ago
window.React = require('react');
window.ReactDOM = require('react-dom');
2 years ago
window.clipboard = clipboard;
window.getSeedNodeList = () => [
2 years ago
{
url: 'https://storage.seed1.loki.network:4433/',
},
{
url: 'https://storage.seed3.loki.network:4433/',
},
{
url: 'https://public.loki.foundation:4433/',
},
];
2 years ago
const { locale: localFromEnv } = config;
2 years ago
window.i18n = setupi18n(localFromEnv, localeMessages);
window.addEventListener('contextmenu', e => {
2 years ago
const editable = (e?.target).closest('textarea, input, [contenteditable="true"]');
const link = (e?.target).closest('a');
const selection = Boolean(window?.getSelection()?.toString());
if (!editable && !selection && !link) {
e.preventDefault();
}
});
2 years ago
// Blocking