|
1
|
|
|
/* |
|
2
|
|
|
* Copyright 2017 Google LLC |
|
3
|
|
|
* |
|
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5
|
|
|
* you may not use this file except in compliance with the License. |
|
6
|
|
|
* You may obtain a copy of the License at |
|
7
|
|
|
* |
|
8
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
* |
|
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
* See the License for the specific language governing permissions and |
|
14
|
|
|
* limitations under the License. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will |
|
19
|
|
|
* load <script nomodule> anyway. This snippet solve this problem, but only for script |
|
20
|
|
|
* tags that load external code, e.g.: <script nomodule src="nomodule.js"></script> |
|
21
|
|
|
* |
|
22
|
|
|
* Again: this will **not** prevent inline script, e.g.: |
|
23
|
|
|
* <script nomodule>alert('no modules');</script>. |
|
24
|
|
|
* |
|
25
|
|
|
* This workaround is possible because Safari supports the non-standard 'beforeload' event. |
|
26
|
|
|
* This allows us to trap the module and nomodule load. |
|
27
|
|
|
* |
|
28
|
|
|
* Note also that `nomodule` is supported in later versions of Safari - it's just 10.1 that |
|
29
|
|
|
* omits this attribute. |
|
30
|
|
|
*/ |
|
31
|
|
|
(function() { |
|
|
|
|
|
|
32
|
|
|
var check = document.createElement('script'); |
|
33
|
|
|
if (!('noModule' in check) && 'onbeforeload' in check) { |
|
34
|
|
|
var support = false; |
|
35
|
|
|
document.addEventListener('beforeload', function(e) { |
|
|
|
|
|
|
36
|
|
|
if (e.target === check) { |
|
37
|
|
|
support = true; |
|
38
|
|
|
} else if (!e.target.hasAttribute('nomodule') || !support) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
e.preventDefault(); |
|
42
|
|
|
}, true); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
check.type = 'module'; |
|
45
|
|
|
check.src = '.'; |
|
46
|
|
|
document.head.appendChild(check); |
|
47
|
|
|
check.remove(); |
|
48
|
|
|
} |
|
49
|
|
|
}()); |
|
50
|
|
|
|