Skip to content

Instantly share code, notes, and snippets.

@k-yamada
Last active May 29, 2018 11:03
Show Gist options
  • Save k-yamada/dcf448b006983b667185ba46dbe1e13c to your computer and use it in GitHub Desktop.
Save k-yamada/dcf448b006983b667185ba46dbe1e13c to your computer and use it in GitHub Desktop.
electronのwebviewとipcで通信する方法 ref: https://qiita.com/k-yamada-github/items/31f87475b68d63badcfb
channel: onload
channel: pong
<template>
<webview
src="http://localhost:3000/"
partition="persist:hoge"
:preload="preload"
autosize
allowpopups
/>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
name: "hoge-page",
data () {
return {
preload: `file:${require('path').resolve(__dirname, '../path/to/preload.js')}`
}
},
mounted () {
const webview: any = document.querySelector('webview')
webview.addEventListener("ipc-message", (event: any) => {
console.log("channel: " + event.channel)
})
webview.addEventListener('dom-ready', () => {
webview.openDevTools() // webview側のデベロッパーツールを表示する。うざかったら消していい。
webview.send("ping")
})
}
});
</script>
const {ipcRenderer} = require('electron')
ipcRenderer.on('ping', (e, data) => {
ipcRenderer.sendToHost('pong');
});
window.onload = function(event) {
ipcRenderer.sendToHost("onload")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment