Skip to content

Instantly share code, notes, and snippets.

@onetdev
Last active December 12, 2022 19:15
Show Gist options
  • Save onetdev/97a6e28f2479711a279a00c8ae298b1e to your computer and use it in GitHub Desktop.
Save onetdev/97a6e28f2479711a279a00c8ae298b1e to your computer and use it in GitHub Desktop.
VideoJS + XHR mod : Adding XHR interceptor
<link media="all" rel="stylesheet" href="https://unpkg.com/[email protected]/dist/video-js.css">
<script src="https://unpkg.com/[email protected]/dist/video.js"></script>
<video-js id="player">
<source src="//video/index.m3u8" type="application/x-mpegURL" />
</video-js>
<script>
var player = videojs("player");
var prefix = "key://";
var urlTpl = "https://domain.com/path/{key}";
// player.ready
player.on("loadstart", function (e) {
player.tech().hls.xhr.beforeRequest = function(options) {
// required for detecting only the key requests
if (!options.uri.startsWith(keyPrefix)) { return; }
options.headers = options.headers || {};
optopns.headers["Custom-Header"] = "value";
options.uri = urlTpl.replace("{key}", options.uri.substring(keyPrefix.length));
};
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment