Last active
May 29, 2018 11:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
channel: onload | |
channel: pong |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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