Skip to content

Commit e8d6330

Browse files
committed
Disallow Obs usage as an external or async-inline script
1 parent 68038db commit e8d6330

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

obs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
* `obs-speedcurve.js`.
88
*/
99

10+
// Immediately disallow the inclusion of Obs.js as an external script—or as an
11+
// inline `type=module`—except on localhost. This file cannot be run
12+
// asynchronously, which means it should not be placed externally either: that
13+
// would kill performance and that’s the exact opposite of what we’re trying
14+
// to achieve.
15+
const obsSrc = document.currentScript;
16+
17+
if (!obsSrc || obsSrc.src || (obsSrc.type && obsSrc.type.toLowerCase() === 'module')) {
18+
if (/^(localhost|127\.0\.0\.1)$/.test(location.hostname) === false) {
19+
console.warn(
20+
'[obs] Skipping: must be an inline, classic <script> in <head>.',
21+
obsSrc ? (obsSrc.src ? 'src=' + obsSrc.src : 'type=' + obsSrc.type) : 'type=module'
22+
);
23+
return;
24+
}
25+
}
26+
1027
// Attach classes to the document.
1128
const html = document.documentElement;
1229

0 commit comments

Comments
 (0)