-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
154 lines (139 loc) · 4.3 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const debug = require('debug')('nightmare:realClick');
module.exports = function realMouse(Nightmare) {
if (!Nightmare) { Nightmare = require('nightmare'); }
Nightmare.action(
'realClick',
realClickInternal,
actionOnElement('realClick'));
Nightmare.action(
'realMousedown',
realMousedownInternal,
actionOnElement('realMousedown'));
Nightmare.action(
'realMouseover',
realMouseoverInternal,
actionOnElement('realMouseover'));
}
function realMousedownInternal(name, options, parent, window, renderer, done) {
const LAST_CLICK = Symbol();
parent.respondTo('realMousedown', function(x, y, done) {
// check the last click event to see if this is a double-click
const previous = window[LAST_CLICK] || {};
const now = Date.now();
const repeat =
previous.x === x &&
previous.y === y &&
(now - previous.time) < 300;
let clickCount = repeat ? 2 : 1;
window[LAST_CLICK] = repeat ? undefined : {x: x, y: y, time: now};
// DO THE THING!
window.webContents.sendInputEvent({
type: 'mousedown',
x: x,
y: y,
clickCount: clickCount
});
setTimeout(function() { done(); }, 25);
});
done();
}
function realMouseoverInternal(name, options, parent, window, renderer, done) {
parent.respondTo('realMouseover', function(x, y, done) {
window.webContents.sendInputEvent({
// `enter` doesn't directly trigger anything, so use `move`
type: 'mousemove',
x: x,
y: y,
movementX: 1,
movementY: 1
});
setTimeout(function() { done(); }, 25);
});
done();
}
function realClickInternal(name, options, parent, window, renderer, done) {
const LAST_CLICK = Symbol();
parent.respondTo('realClick', function(x, y, done) {
// check the last click event to see if this is a double-click
const previous = window[LAST_CLICK] || {};
const now = Date.now();
const repeat =
previous.x === x &&
previous.y === y &&
(now - previous.time) < 300;
let clickCount = repeat ? 2 : 1;
window[LAST_CLICK] = repeat ? undefined : {x: x, y: y, time: now};
// CLICKITY-CLICK
window.webContents.sendInputEvent({
type: 'mousedown',
x: x,
y: y,
clickCount: clickCount
});
window.webContents.sendInputEvent({
type: 'mouseup',
x: x,
y: y
});
setTimeout(function() { done(); }, 25);
});
done();
}
// Utilities
function actionOnElement(actionName) {
return function(selector /*[, position], done */) {
const done = arguments[arguments.length - 1]
const position = arguments.length > 2 ? arguments[1] : undefined;
if (typeof selector !== 'string') {
return done(new TypeError(`${actionName}: "selector" must be a string`));
}
else if (position && (typeof position.x !== 'number' || typeof position.y !== 'number')) {
return done(new TypeError(`${actionName}: "position.x" and ".y" must be numbers`));
}
var child = this.child;
debug(`Finding "${selector}"`);
getBounds(this, selector)
.then(function(bounds) {
return position ? relativePosition(bounds, position) : center(bounds);
})
.then(function(point) {
debug(`${actionName} "${selector}" at ${point.x}, ${point.y}`);
child.call(actionName, point.x, point.y, done);
})
.catch(done);
}
}
function getBounds(nightmare, selector) {
return new Promise(function(resolve, reject) {
nightmare.evaluate_now(function(selector) {
let element = document.querySelector(selector);
if (!element) { throw new Error(`Cannot find element: "${selector}"`); }
let rect = element.getBoundingClientRect();
// sadly, Chromium ClientRects are not JSON-serializable :(
return {
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
width: rect.width,
height: rect.height
};
}, function(error, bounds) {
if (error) return reject(error);
resolve(bounds);
}, selector);
});
}
function center(bounds) {
return {
x: Math.floor(bounds.left + bounds.width / 2),
y: Math.floor(bounds.top + bounds.height / 2)
};
}
function relativePosition(bounds, position) {
return {
x: Math.floor(bounds.left + position.x),
y: Math.floor(bounds.top + position.y)
};
}