preload.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const fs = require('fs')
  2. const path = require('path')
  3. const os = require('os')
  4. const { ipcRenderer, contextBridge, shell, clipboard, webFrame } = require('electron')
  5. const IS_MAC = process.platform === 'darwin'
  6. const IS_WIN32 = process.platform === 'win32'
  7. const ALLOWED_EXTERNAL_PROTOCOLS = ['https:', 'http:', 'mailto:', 'zotero:', 'file:']
  8. function getFilePathFromClipboard () {
  9. if (IS_WIN32) {
  10. const rawFilePath = clipboard.read('FileNameW')
  11. return rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '')
  12. } else if (IS_MAC) {
  13. return clipboard.read('public.file-url').replace('file://', '')
  14. } else {
  15. return clipboard.readText()
  16. }
  17. }
  18. /**
  19. * Read the contents of the clipboard for a custom format.
  20. * @param {string} format The custom format to read.
  21. * @returns Buffer containing the contents of the clipboard for the specified format, or null if not available.
  22. */
  23. function getClipboardData (format) {
  24. if (clipboard.has(format, "clipboard")) {
  25. return clipboard.readBuffer(format)
  26. }
  27. else {
  28. return null;
  29. }
  30. }
  31. contextBridge.exposeInMainWorld('apis', {
  32. doAction: async (arg) => {
  33. return await ipcRenderer.invoke('main', arg)
  34. },
  35. invoke: async (channel, args) => {
  36. return await ipcRenderer.invoke(channel, ...args)
  37. },
  38. addListener: ipcRenderer.on.bind(ipcRenderer),
  39. removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
  40. removeAllListeners: ipcRenderer.removeAllListeners.bind(ipcRenderer),
  41. on: (channel, callback) => {
  42. const newCallback = (_, data) => callback(data)
  43. ipcRenderer.on(channel, newCallback)
  44. },
  45. off: (channel, callback) => {
  46. if (!callback) {
  47. ipcRenderer.removeAllListeners(channel)
  48. } else {
  49. ipcRenderer.removeListener(channel, callback)
  50. }
  51. },
  52. once: (channel, callback) => {
  53. ipcRenderer.on(channel, callback)
  54. },
  55. checkForUpdates: async (...args) => {
  56. await ipcRenderer.invoke('check-for-updates', ...args)
  57. },
  58. setUpdatesCallback (cb) {
  59. if (typeof cb !== 'function') return
  60. const channel = 'updates-callback'
  61. ipcRenderer.removeAllListeners(channel)
  62. ipcRenderer.on(channel, cb)
  63. },
  64. installUpdatesAndQuitApp () {
  65. ipcRenderer.invoke('install-updates', true)
  66. },
  67. async openExternal (url, options) {
  68. const protocol = new URL(url).protocol
  69. if (!ALLOWED_EXTERNAL_PROTOCOLS.includes(protocol)) {
  70. throw new Error('illegal protocol')
  71. }
  72. await shell.openExternal(url, options)
  73. },
  74. async openPath (relativePath) {
  75. const absolutePath = path.resolve(
  76. relativePath.startsWith('~') ? path.join(os.homedir(), relativePath.slice(1)) : relativePath
  77. );
  78. await shell.openPath(absolutePath)
  79. },
  80. /**
  81. * save all publish assets to disk
  82. *
  83. * @param {string} html html file with embedded state
  84. */
  85. exportPublishAssets (html, customCSSPath, exportCSSPath, repoPath, assetFilenames, outputDir) {
  86. ipcRenderer.invoke(
  87. 'export-publish-assets',
  88. html,
  89. customCSSPath,
  90. exportCSSPath,
  91. repoPath,
  92. assetFilenames,
  93. outputDir
  94. )
  95. },
  96. toggleMaxOrMinActiveWindow (isToggleMin = false) {
  97. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  98. },
  99. /**
  100. * internal
  101. * @param type
  102. * @param args
  103. * @private
  104. */
  105. async _callApplication (type, ...args) {
  106. return await ipcRenderer.invoke('call-application', type, ...args)
  107. },
  108. /**
  109. * internal
  110. * @param type
  111. * @param args
  112. * @private
  113. */
  114. async _callMainWin (type, ...args) {
  115. return await ipcRenderer.invoke('call-main-win', type, ...args)
  116. },
  117. getFilePathFromClipboard,
  118. getClipboardData,
  119. setZoomFactor (factor) {
  120. webFrame.setZoomFactor(factor)
  121. },
  122. setZoomLevel (level) {
  123. webFrame.setZoomLevel(level)
  124. },
  125. isAbsolutePath: path.isAbsolute.bind(path)
  126. })