-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure-context.js
33 lines (31 loc) · 904 Bytes
/
secure-context.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
(function() {
'use strict';
if (! ('isSecureContext' in globalThis)) {
const hostnames = ['localhost', '127.0.0.1'];
const HTTPS = 'https:';
const protocols = [HTTPS, 'file:', 'wss:'];
const hasSecureScripts = (document = globalThis.document) => {
return [...document.scripts].every(({ src }) => {
if (src.length === 0) {
return true;
} else {
const { protocol, hostname } = new URL(src, document.baseURI);
return protocol === HTTPS || hostname === location.hostname;
}
});
};
Object.defineProperty(globalThis, 'isSecureContext', {
enumerable: true,
configurable: true,
get: function isSecureContext() {
if (window.parent !== window) {
return false;
} else if (protocols.includes(location.protocol) || hostnames.includes(location.hostname)) {
return hasSecureScripts();
} else {
return false;
}
}
});
}
})();