-
Notifications
You must be signed in to change notification settings - Fork 0
/
blur.html
43 lines (40 loc) · 1.54 KB
/
blur.html
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
<html>
<meta charset=utf-8>
<input id="input">
<div id="div"></div>
<script>
const console = { log: msg => div.innerHTML += msg + "<br>" };
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
// This test requires the document to have focus as a starting condition.
// Whether a newly loaded page receives focus or not, seems to be somewhat
// browser-dependent and situation-dependent. For instance, Firefox appears to
// focus the page immediately if the page was loaded with the refresh button,
// but not if it was loaded from pressing ENTER in the URL bar. To ensure a
// reliable starting condition for this test, we give an extra push for focus.
if (!document.hasFocus()) {
const input = document.getElementById("input");
input.focus();
await new Promise(r => input.onfocus = r);
}
console.log(document.hasFocus());
window.onblur = () => console.log("error: BLUR");
if (!document.hasFocus()) {
await new Promise(r => window.onfocus = r);
console.log("FOCUS");
}
console.log("Opening popup.");
const popup = window.open("blur_popup.html", "otherwindow", "resizable");
const p = new Promise(r => window.onblur = () => r(console.log("BLUR")));
const msg = await new Promise(r => window.onmessage = ({data}) => r(data));
console.log(`From popup: ${msg}`);
console.log(document.hasFocus());
await p;
console.log("Closing popup.");
popup.close();
await new Promise(r => window.onfocus = r);
console.log("FOCUS");
console.log(document.hasFocus());
})();
</script>
</html>