Skip to content

Instantly share code, notes, and snippets.

@svbatalov
Created October 19, 2016 19:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svbatalov/316da55cb0fb03e30eb1be1b49afd734 to your computer and use it in GitHub Desktop.
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.
<!DOCTYPE html>
<html>
<body>
<input type="file" id="fileinput"/>
</body>
</html>
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