Last active
December 30, 2021 18:44
-
-
Save vmars316/65e29a008de46fe46e5a7bbade32fcb1 to your computer and use it in GitHub Desktop.
BrowserView & One Button
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> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello World!</title> | |
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
</head> | |
<body> | |
<p> | |
<a href="https://gist.github.com/vmars316/65e29a008de46fe46e5a7bbade32fcb1">https://gist.github.com/vmars316/65e29a008de46fe46e5a7bbade32fcb1</a> | |
</p> | |
<p> | |
We are using Node.js <span id="node-version"></span>, | |
Chromium <span id="chrome-version"></span>, | |
and Electron <span id="electron-version"></span>. | |
</p> | |
<p> | |
<input id="url" type="url" value="https://google.com"/> | |
<!-- <button id="go">Go</button> --> | |
<button id="go">Go</button> | |
<!-- <button id="go" onclick="goButtonClicked()">Go</button> --> | |
</p> | |
<script> | |
// alert("Click OK Button to require ./renderer.js"); | |
require('./renderer.js'); | |
</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
// BrowserView-&-One-Button: //https://gist.github.com/vmars316/65e29a008de46fe46e5a7bbade32fcb1 | |
const path = require('path') | |
const {app, contextBridge, BrowserWindow, BrowserView, ipcMain} = require('electron') | |
function createWindows () { | |
const win = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
nodeIntegration: false , | |
contextIsolation: true, | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
const view = new BrowserView({ | |
}) | |
win.setBrowserView(view) | |
view.setBounds({ x: 0, y: 120, width: 800, height: 500 }) | |
// view.webContents.loadURL("https://duckduckgo.com") | |
view.setAutoResize({width: true, height: true}) | |
win.loadFile("index.html") | |
view.webContents.openDevTools({ mode: 'detach' }); | |
ipcMain.handle('load-url', (event, url) => { | |
console.log("main: load url", {url}) | |
view.webContents.loadURL(url) | |
}) | |
win.webContents.openDevTools({ mode: 'detach' }); | |
view.webContents.openDevTools({ mode: 'detach' }); | |
} | |
app.on('ready', () => { | |
createWindows() ; | |
}) | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) |
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": "consistent-scene-race-ax2fx", | |
"productName": "consistent-scene-race-ax2fx", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "vmars", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "16.0.5" | |
} | |
} |
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
console.log("Beginning preload.js") | |
const { contextBridge, ipcRenderer } = require('electron') | |
contextBridge.exposeInMainWorld('testAPI', { | |
loadUrl: (url) => ipcRenderer.invoke('load-url', url), | |
// loadUrl: (url) => ipcRenderer.invoke('load-url', "www.duckduclgo.com"), | |
}) |
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
document.getElementById('go').addEventListener('click', async () => { | |
const url = document.getElementById('url').value | |
console.log("renderer: go button clicked", url) | |
alert("renderer: GO Button Clicked url = " + url) | |
window.testAPI.loadUrl(url) | |
}) |
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
/* styles.css */ | |
/* Add styles here to customize the appearance of your app */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment