Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 1.35 KB

README.md

File metadata and controls

31 lines (22 loc) · 1.35 KB

electron-window.ipfs

This is a test repo to see if we can add window.ipfs to a web page bundled in electron.

In this repo we're using the <webview> tag in the renderer process to load up a web app from a URL.

<webview
  src="https://ipfs.io/ipfs/QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn/"
  preload="./webview.js"></webview>

The web app makes use of window.ipfs if it is available.

The preload attribute allows us to run a script on the <webview> page before other scripts run. We use preload to create the window.ipfs object that the app can use.

webview.js

const { ipcRenderer } = require('electron')
const { createProxyClient } = require('ipfs-postmsg-proxy')

window.ipfs = createProxyClient({
  postMessage: data => ipcRenderer.send('ipfs-postmsg-proxy:message', data),
  addListener: (_, handler) => ipcRenderer.on('ipfs-postmsg-proxy:message', handler),
  removeListener: (_, handler) => ipcRenderer.removeListener('ipfs-postmsg-proxy:message', handler),
  getMessageData: (_, data) => data
})

The window.ipfs object is a proxy that uses IPC messaging to communicate with the IPFS node running in the main process. We're using ipfs-postmsg-proxy for this.