videojs / video.js

Video.js - open source HTML5 video player
https://videojs.com
Other
37.87k stars 7.43k forks source link

It works for adding other custom headers but not working for overwriting the 'User-Agent' request header. Can you please refer to some information about it. #8756

Closed rishbitsnbytes closed 3 months ago

rishbitsnbytes commented 3 months ago
          I'm not really sure where you got `videojs.Hls.xhr.headers`, but the `videojs.Hls.xhr.beforeRequest` should work according to the [docs](https://github.com/videojs/http-streaming#hlsxhr). I've tested it locally, and it seems to be working.
videojs.Hls.xhr.beforeRequest = function(options) {
  options.headers = {
    foo: 5
  };
  return options;
};

var player = videojs('video-player-id');
player.src(...);

It's possible you may need to reload the source or only set it after videojs.Hls.xhr.beforeRequest is set.

Originally posted by @gkatsev in https://github.com/videojs/video.js/issues/5724#issuecomment-459363403

welcome[bot] commented 3 months ago

👋 Thanks for opening your first issue here! 👋

If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can. To help make it easier for us to investigate your issue, please follow the contributing guidelines.

rishbitsnbytes commented 3 months ago

Hey @gkatsev videojs.Hls.xhr.beforeRequest works for adding other custom headers but not working for overwriting the 'User-Agent' request header. Can you please refer to some information about it.

mister-ben commented 3 months ago

It's not possible to change the user-agent header. Browsers don't allow it to be modified by client side javascript, only by extensions.

rishbitsnbytes commented 3 months ago

Hey @mister-ben thank you very much for a quick response. And yes I checked various places, some say it can't be changed at all and then other places even like official MDN docs says it can be changed now.

Pls have a quick look at this below highlighted text "Note" and kindly reply with your thoughts on this. https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name#:~:text=Note%3A%20The,bug%20571722).

It seems specs were changes and it's possible to change it for quite a while now. 'User-Agent' doesn't seem to be in Forbidden-Header-Name anymore. https://fetch.spec.whatwg.org/#forbidden-header-name

https://stackoverflow.com/questions/23248525/setting-a-custom-useragent-in-html-or-javascript#:~:text=These%20are%20forbidden%20so%20the,in%20full%20control%20over%20them.&text=You%20will%20never%20change%20a,ever%20deploy%20it%20in%20production.

gkatsev commented 3 months ago

Yeah, apparently it should be allowed now, but it seems like Chrome didn't make the change, though Firefox seems to have (not sure about Safari). So, if you care about Chrome, you can't do it this way. Maybe Chrome lets you modify the user agent via a service worker?

mister-ben commented 3 months ago

TIL. You can change it on Safari and Firefox, but not Chrome or Edge. (Latest versions on Sonoma).

gkatsev commented 3 months ago

So, yeah, I think this can be closed as a browser bug. https://issues.chromium.org/issues/40450316 linked from mdn. If beforeRequest is working for you to add headers, it should work the same for user agent for the browsers that support it, i.e. not chromium based ones.

rishbitsnbytes commented 3 months ago

Thank you for your quick responses guys. As you have already concluded it with browser bug. And I tested it on Chrome, Firefox and Safari. It was working only in Firefox.

Also don't know why but videojs.Hls.xhr.beforeRequest stopped working for me with error can't access Hls.xhr But I was able to make it work videojs.Vhs.xhr.beforeRequest.

gkatsev commented 3 months ago

The usage of .Hls has bees deprecated for a while and as of v8, only .Vhs is available.