preload.js 638 B

12345678910111213141516171819
  1. // preload.js
  2. // 所有的 Node.js API接口 都可以在 preload 进程中被调用.
  3. // 它拥有与Chrome扩展一样的沙盒。
  4. const {contextBridge, ipcRenderer} = require('electron')
  5. window.addEventListener('DOMContentLoaded', () => {
  6. const replaceText = (selector, text) => {
  7. const element = document.getElementById(selector)
  8. if (element) element.innerText = text
  9. }
  10. for (const dependency of ['chrome', 'node', 'electron']) {
  11. replaceText(`${dependency}-version`, process.versions[dependency])
  12. }
  13. })
  14. contextBridge.exposeInMainWorld('electronAPI', {
  15. setTitle: (title) => ipcRenderer.send('set-title', title)
  16. })