web-frame-init.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const electron = require('electron')
  2. module.exports = () => {
  3. // Call webFrame method
  4. electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => {
  5. electron.webFrame[method](...args)
  6. })
  7. electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_SYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
  8. const result = electron.webFrame[method](...args)
  9. event.sender.send(`ELECTRON_INTERNAL_BROWSER_SYNC_WEB_FRAME_RESPONSE_${requestId}`, result)
  10. })
  11. electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
  12. const responseCallback = function (result) {
  13. Promise.resolve(result)
  14. .then((resolvedResult) => {
  15. event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, null, resolvedResult)
  16. })
  17. .catch((resolvedError) => {
  18. if (resolvedError instanceof Error) {
  19. // Errors get lost, because: JSON.stringify(new Error('Message')) === {}
  20. // Take the serializable properties and construct a generic object
  21. resolvedError = {
  22. message: resolvedError.message,
  23. stack: resolvedError.stack,
  24. name: resolvedError.name,
  25. __ELECTRON_SERIALIZED_ERROR__: true
  26. }
  27. }
  28. event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, resolvedError)
  29. })
  30. }
  31. args.push(responseCallback)
  32. electron.webFrame[method](...args)
  33. })
  34. }