Created
July 26, 2022 07:34
-
-
Save terry2010/54a04d2587098e70dc9f3958697d63d0 to your computer and use it in GitHub Desktop.
test
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>IPC Example</title> | |
</head> | |
<body> | |
<h1>IPC Example</h1> | |
<button onclick="fullscreen(true)">click me to set fullscreen</button> | | |
<button onclick="fullscreen(false)">click me to cancel fullscreen</button> | |
<p>main window</p> | |
<button onclick="send()">click me to test IPC</button> | |
<pre> | |
<script> | |
const { ipcRenderer } = require('electron') | |
function send() { | |
ipcRenderer.send('test-message', 'papapa') | |
} | |
function fullscreen(iff) { | |
ipcRenderer.send('action-message', {act:'fullscreen',value:iff}) | |
} | |
</script> | |
</body> | |
</html> |
This file contains 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
// The ipcMain and ipcRenderer modules allow communication between the mai | |
// process and the renderer processes. | |
// | |
// For more info, see: | |
// https://electronjs.org/docs/api/ipc-main | |
// https://electronjs.org/docs/api/ipc-renderer | |
const { app, BrowserWindow, ipcMain } = require('electron') | |
const path = require('path') | |
var mainWindow | |
app.whenReady().then(() => { | |
mainWindow = new BrowserWindow({ | |
height: 800, | |
width: 1000, | |
webPreferences: { | |
nodeIntegration: true, // default in Electron >= 5 | |
contextIsolation: false, // default in Electron >= 12 | |
} | |
}) | |
let hiddenWindowID = 1 | |
ipcMain.on('test-message', (event, arg) => { | |
console.log(arg) // prints "ping" | |
var hiddenWindow = new BrowserWindow({ | |
height: 600, | |
width: 600, | |
webPreferences: { | |
nodeIntegration: true, | |
enableRemoteModule: true, | |
contextIsolation: false, | |
webSecurity: false, | |
preload: path.join(__dirname, 'preload.js'), | |
}, | |
fullscreen:undefined, | |
simpleFullscreen:true, | |
transparent: true, | |
frame: false, | |
movable: false, | |
resizable: false, | |
enableLargerThanScreen: true, | |
hasShadow: false, | |
title: "hiddenWindow", | |
show: false, | |
roundedCorners: false | |
}) | |
hiddenWindow.loadFile('hidden.html') | |
setTimeout(()=>{ | |
console.log('send hidden-message2') | |
hiddenWindow.webContents.send('hidden-message2','msg2') | |
},3000) | |
}) | |
ipcMain.on('hidden-message', (event, arg) => { | |
console.log(arg) | |
mainWindow.webContents.executeJavaScript(` | |
document.body.append("`+arg+`") | |
`).catch(e=>{ | |
console.log(e) | |
}) | |
}) | |
ipcMain.on('action-message', (event, arg) => { | |
if(arg.act === 'fullscreen' ) { | |
mainWindow.setSimpleFullScreen(arg.value) | |
} | |
}) | |
mainWindow.loadFile('index.html') | |
mainWindow.on('close',()=>{ | |
app.exit(0) | |
}) | |
}) |
This file contains 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
{ | |
"name": "didactic-anxiety-intensify-ocsqg", | |
"productName": "didactic-anxiety-intensify-ocsqg", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "terry", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "19.0.6" | |
} | |
} |
This file contains 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') | |
// prints "pong" | |
ipcRenderer.send('hidden-message', ' hiddenWindow-ping-1') | |
setTimeout( ()=>{ | |
ipcRenderer.send('hidden-message', ' hiddenWindow-ping-2') | |
},1000) | |
ipcRenderer.on('hidden-message2',()=>{ | |
ipcRenderer.send('hidden-message', ' hiddenWindow-ping-3') | |
},1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment