Last active
September 18, 2021 15:45
-
-
Save decors/7b1cb44a2b9e18df58e8 to your computer and use it in GitHub Desktop.
Electron open-file
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>Electron</title> | |
</head> | |
<body> | |
<script> | |
var ipc = require('ipc'); | |
ipc.on('open-file', function(filepath) { | |
alert(filepath); | |
}); | |
</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
'use strict'; | |
var app = require('app'), | |
BrowserWindow = require('browser-window'); | |
require('crash-reporter').start(); | |
var mainWindow; | |
var filepath; | |
var ready = false; | |
app.on('ready', function() { | |
mainWindow = new BrowserWindow({ 'width': 800, 'height': 600 }); | |
mainWindow.loadUrl('file://' + __dirname + '/index.html'); | |
mainWindow.focus(); | |
mainWindow.on('closed', function() { | |
mainWindow = null; | |
}); | |
mainWindow.webContents.on('did-finish-load', function() { | |
if (filepath) { | |
mainWindow.webContents.send('open-file', filepath); | |
filepath = null; | |
} | |
}); | |
ready = true; | |
}); | |
app.on("open-file", function(event, path) { | |
event.preventDefault(); | |
filepath = path; | |
if (ready) { | |
mainWindow.webContents.send('open-file', filepath); | |
filepath = null; | |
return; | |
} | |
}); | |
app.on('window-all-closed', function() { | |
app.quit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment