Created
October 19, 2016 19:18
-
-
Save svbatalov/316da55cb0fb03e30eb1be1b49afd734 to your computer and use it in GitHub Desktop.
Sample code demonstrating how to set list of files for <input type="file"/> without user interaction in electron.js.
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> | |
<body> | |
<input type="file" id="fileinput"/> | |
</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
const { app, BrowserWindow } = require('electron'); | |
app.on('ready', () => { | |
let win = new BrowserWindow({width: 800, height: 600}); | |
win.loadURL(`file://${__dirname}/index.html`); | |
let wc = win.webContents; | |
try { | |
wc.debugger.attach("1.1"); | |
} catch (err) { | |
console.error("Debugger attach failed : ", err); | |
}; | |
wc.once('did-finish-load', () => { | |
wc.debugger.sendCommand("DOM.getDocument", {}, function (err, res) { | |
wc.debugger.sendCommand("DOM.querySelector", { | |
nodeId: res.root.nodeId, | |
selector: "#fileinput" // CSS selector of input[type=file] element | |
}, function (err, res) { | |
wc.debugger.sendCommand("DOM.setFileInputFiles", { | |
nodeId: res.nodeId, | |
files: ['/tmp/test.txt'] // Actual list of paths | |
}, function (err, res) { | |
wc.debugger.detach(); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment