/
nightmare-upload.js
51 lines (50 loc) · 1.91 KB
/
nightmare-upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var Nightmare = require('nightmare');
Nightmare.action('upload',
function (name, options, parent, win, renderer, done) {
parent.on('upload', function (selector, path) {
console.log(arguments);
path = Array.isArray(path) ? path : [path];
var wc = win.webContents;
try {
wc.debugger.attach("1.1");
} catch (err) {
console.error("Debugger attach failed : ", err);
return parent.emit('upload', err);
}
wc.debugger.sendCommand("DOM.getDocument", {}, function (err, res) {
if (err.code) {
return parent.emit('upload', err);
}
console.log(arguments);
wc.debugger.sendCommand("DOM.querySelector", {
nodeId: res.root.nodeId,
selector: selector
}, function (err, res) {
if (err.code) {
return parent.emit('upload', err);
}
console.log(arguments);
wc.debugger.sendCommand("DOM.setFileInputFiles", {
nodeId: res.nodeId,
files: path // Actual list of paths
}, function (err, res) {
if (err.code) {
return parent.emit('upload', err);
}
console.log(arguments);
wc.debugger.detach();
parent.emit('upload');
});
});
});
});
done();
return this;
},
function (selector, path, done) {
this.child.once('upload', function (err, result) {
if (err) return done(err);
done(null, result);
});
this.child.emit('upload', selector, path);
});