forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-command-to-frame.js
More file actions
72 lines (65 loc) · 2.16 KB
/
send-command-to-frame.js
File metadata and controls
72 lines (65 loc) · 2.16 KB
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
import getSelector from './get-selector';
import respondable from './respondable';
import log from '../log';
/**
* Sends a command to an instance of axe in the specified frame
* @param {Element} node The frame element to send the message to
* @param {Object} parameters Parameters to pass to the frame
* @param {Function} callback Function to call when results from the frame has returned
*/
export default function sendCommandToFrame(node, parameters, resolve, reject) {
const win = node.contentWindow;
const pingWaitTime = parameters.options?.pingWaitTime ?? 500;
if (!win) {
log('Frame does not have a content window', node);
resolve(null);
return;
}
// Skip ping
if (pingWaitTime === 0) {
callAxeStart(node, parameters, resolve, reject);
return;
}
// give the frame .5s to respond to 'axe.ping', else log failed response
let timeout = setTimeout(() => {
// This double timeout is important for allowing iframes to respond
// DO NOT REMOVE
timeout = setTimeout(() => {
if (!parameters.debug) {
resolve(null);
} else {
reject(err('No response from frame', node));
}
}, 0);
}, pingWaitTime);
// send 'axe.ping' to the frame
respondable(win, 'axe.ping', null, undefined, () => {
clearTimeout(timeout);
callAxeStart(node, parameters, resolve, reject);
});
}
function callAxeStart(node, parameters, resolve, reject) {
// Give axe 60s (or user-supplied value) to respond to 'axe.start'
const frameWaitTime = parameters.options?.frameWaitTime ?? 60000;
const win = node.contentWindow;
const timeout = setTimeout(function collectResultFramesTimeout() {
reject(err('Axe in frame timed out', node));
}, frameWaitTime);
// send 'axe.start' and send the callback if it responded
respondable(win, 'axe.start', parameters, undefined, data => {
clearTimeout(timeout);
if (data instanceof Error === false) {
resolve(data);
} else {
reject(data);
}
});
}
function err(message, node) {
let selector;
// TODO: es-modules_tree
if (axe._tree) {
selector = getSelector(node);
}
return new Error(message + ': ' + (selector || node));
}