clipboard.ts 1.1 KB

1234567891011121314151617181920212223242526
  1. import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
  2. import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
  3. const clipboard = process._linkedBinding('electron_common_clipboard');
  4. if (process.type === 'renderer') {
  5. const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;
  6. const makeRemoteMethod = function (method: keyof Electron.Clipboard): any {
  7. return (...args: any[]) => ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_CLIPBOARD_SYNC, method, ...args);
  8. };
  9. if (process.platform === 'linux') {
  10. // On Linux we could not access clipboard in renderer process.
  11. for (const method of Object.keys(clipboard) as (keyof Electron.Clipboard)[]) {
  12. clipboard[method] = makeRemoteMethod(method);
  13. }
  14. } else if (process.platform === 'darwin') {
  15. // Read/write to find pasteboard over IPC since only main process is notified of changes
  16. clipboard.readFindText = makeRemoteMethod('readFindText');
  17. clipboard.writeFindText = makeRemoteMethod('writeFindText');
  18. }
  19. }
  20. export default clipboard;