|
1 | 1 | window.onload = () => {
|
2 |
| - const createCORSRequest = (url) => { |
3 |
| - let xhr = new XMLHttpRequest(); |
4 |
| - if (`withCredentials` in xhr) { |
| 2 | + const createCORSRequest = (url) => { |
| 3 | + let xhr = new XMLHttpRequest(); |
| 4 | + if (`withCredentials` in xhr) { |
5 | 5 |
|
6 |
| - // Check if the XMLHttpRequest object has a "withCredentials" property. |
7 |
| - // "withCredentials" only exists on XMLHTTPRequest2 objects. |
8 |
| - xhr.open(`GET`, url, true); |
| 6 | + // Check if the XMLHttpRequest object has a "withCredentials" property. |
| 7 | + // "withCredentials" only exists on XMLHTTPRequest2 objects. |
| 8 | + xhr.open(`GET`, url, true); |
9 | 9 |
|
10 |
| - } else if (typeof XDomainRequest != `undefined`) { |
| 10 | + } else if (typeof XDomainRequest != `undefined`) { |
11 | 11 |
|
12 |
| - // Otherwise, check if XDomainRequest. |
13 |
| - // XDomainRequest only exists in IE, and is IE's way of making CORS requests. |
14 |
| - xhr = new XDomainRequest(); |
15 |
| - xhr.open(`GET`, url); |
| 12 | + // Otherwise, check if XDomainRequest. |
| 13 | + // XDomainRequest only exists in IE, and is IE's way of making CORS requests. |
| 14 | + xhr = new XDomainRequest(); |
| 15 | + xhr.open(`GET`, url); |
16 | 16 |
|
17 |
| - } else { |
| 17 | + } else { |
18 | 18 |
|
19 |
| - // Otherwise, CORS is not supported by the browser. |
20 |
| - xhr = null; |
| 19 | + // Otherwise, CORS is not supported by the browser. |
| 20 | + xhr = null; |
21 | 21 |
|
22 |
| - } |
23 |
| - return xhr; |
24 |
| - }; |
| 22 | + } |
| 23 | + return xhr; |
| 24 | + }; |
25 | 25 |
|
26 |
| - const start = mode => { |
27 |
| - const container = document.getElementById(`iwsy-container`); |
28 |
| - const scriptElement = document.getElementById(`iwsy-script`); |
29 |
| - const path = scriptElement.innerText; |
30 |
| - if (scriptElement) { |
31 |
| - const request = createCORSRequest(`${path}?v=${Math.floor(Date.now())}`); |
32 |
| - if (!request) { |
33 |
| - throw Error(`Unable to access the JSON script`); |
34 |
| - } |
| 26 | + const start = mode => { |
| 27 | + const container = document.getElementById(`iwsy-container`); |
| 28 | + const scriptElement = document.getElementById(`iwsy-script`); |
| 29 | + const path = scriptElement.innerText; |
| 30 | + if (scriptElement) { |
| 31 | + const request = createCORSRequest(`${path}?v=${Math.floor(Date.now())}`); |
| 32 | + if (!request) { |
| 33 | + throw Error(`Unable to access the JSON script`); |
| 34 | + } |
35 | 35 |
|
36 |
| - request.onload = () => { |
37 |
| - if (200 <= request.status && request.status < 400) { |
38 |
| - const script = JSON.parse(request.responseText); |
39 |
| - const iwsy = IWSY(container, script); |
40 |
| - const slash = path.lastIndexOf(`/`); |
41 |
| - iwsy.setPath(path.slice(0, slash + 1)); |
42 |
| - iwsy.onStep(index => { |
43 |
| - }); |
44 |
| - iwsy.run(`normal`, mode, () => { |
45 |
| - console.log(`All done`); |
46 |
| - }); |
47 |
| - } else { |
48 |
| - throw Error(`Unable to access the JSON script`); |
49 |
| - } |
50 |
| - }; |
| 36 | + request.onload = () => { |
| 37 | + if (200 <= request.status && request.status < 400) { |
| 38 | + const script = JSON.parse(request.responseText); |
| 39 | + const iwsy = IWSY(container, script); |
| 40 | + const slash = path.lastIndexOf(`/`); |
| 41 | + iwsy.setPath(path.slice(0, slash + 1)); |
| 42 | + iwsy.onStep(() => { |
| 43 | + }); |
| 44 | + iwsy.run(`normal`, mode, () => { |
| 45 | + console.log(`All done`); |
| 46 | + }); |
| 47 | + } else { |
| 48 | + throw Error(`Unable to access the JSON script`); |
| 49 | + } |
| 50 | + }; |
51 | 51 |
|
52 |
| - request.onerror = () => { |
53 |
| - throw Error(`Unable to access the JSON script`); |
54 |
| - }; |
| 52 | + request.onerror = () => { |
| 53 | + throw Error(`Unable to access the JSON script`); |
| 54 | + }; |
55 | 55 |
|
56 |
| - request.send(); |
57 |
| - } |
58 |
| - }; |
| 56 | + request.send(); |
| 57 | + } |
| 58 | + }; |
59 | 59 |
|
60 |
| - // Wait for a click/tap or a keypress to start |
61 |
| - document.addEventListener(`click`, () => { |
62 |
| - document.removeEventListener(`click`); |
63 |
| - start(`auto`); |
64 |
| - }); |
65 |
| - document.onkeydown = event => { |
66 |
| - document.onkeydown = null; |
67 |
| - if (event.code === `Enter`) { |
68 |
| - start(`auto`); |
69 |
| - } |
70 |
| - else { |
71 |
| - start(`manual`); |
72 |
| - } |
73 |
| - return true; |
74 |
| - }; |
| 60 | + // Wait for a click/tap or a keypress to start |
| 61 | + const listener = document.addEventListener(`click`, () => { |
| 62 | + document.removeEventListener(`click`, listener); |
| 63 | + start(`auto`); |
| 64 | + }); |
| 65 | + document.onkeydown = event => { |
| 66 | + document.onkeydown = null; |
| 67 | + if (event.code === `Enter`) { |
| 68 | + start(`auto`); |
| 69 | + } |
| 70 | + else { |
| 71 | + start(`manual`); |
| 72 | + } |
| 73 | + return true; |
| 74 | + }; |
75 | 75 | };
|
0 commit comments