如何设置导航器的userAgent? [英] How to set navigator userAgent?

查看:129
本文介绍了如何设置导航器的userAgent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这是一个边缘的情况下我也设法在降落。我测试我对新的Tizen OS的应用程序。我的JS code有成千上万导航仪检查。是这样的:

  navigator.userAgent.toLocaleLowerCase()的indexOf(机器人)|| navigator.userAgent.toLocaleLowerCase()的indexOf(iPad是)

现在Tizen OS的我的测试设备上的userAgent没有两者的字符串。我的很多CSS和JS的是打破结果。我在POC模式,现在,不想花时间在增加一个额外的检查,所有这些条件。有没有一种方法以编程方式设置的userAgent?沿着线的东西:

  navigator.userAgent的+ =Tizen//​​不起作用。

MDN 说,它的读写性能。我不能够,虽然修改。帮助我。无论是另一种聪​​明的办法来欺骗的userAgent或正确的方法来设置此。谢谢你。


解决方案

  

我的JS code有成千上万导航仪检查


在这种情况下,它是你的JS code这是错误的。

您已经基本上刚刚发现的终极原因做UA字符串检测被认为是非常糟糕的做法。

总之,如果所描述的code相,那么你做错了什么。有用于检测UA字符串很少正当理由,并没有这些理由适用于code中的客户端本身上运行的。

这东西在浏览器行千????这只能做坏事到您的网站的性能。


  

有没有以编程方式设置的userAgent的方法吗?
  MDN说,它的读写性能。


用户代理字符串是只读值。

不过,你的可以覆盖吸气,所以有使它可写的方式。难看,但可能的。

我真的建议避免这样做虽然。

即使没有浏览器厂商刻意改变自己的UA字符串,以打破这种code的(这种事情),你的任务是从根本上永无止境;你将必须保持每一个新的设备/浏览器/版本发布时间重新审视这个code和你的数千code的行只是会越来越大。

另外,当然,如果用户修改了浏览器的UA字符串,这将是完全不可靠的。

现在有一个黑客,如果你必须修复它,但要知道,这将继续吃越来越多的时间。你真的需要考虑切换到一个更好的编码习惯,如特征检测,而不是检测UA

Ok this is an edge case I have managed to land in. I am testing my app on the new Tizen OS. My JS code has thousands of navigator checks. Something like:

navigator.userAgent.toLocaleLowerCase().indexOf("android") || navigator.userAgent.toLocaleLowerCase().indexOf("iPad")

Now Tizen OS's userAgent on my test device does not have either of those strings. A lot of my css and JS is breaking as a result. I am in the POC mode right now and do not want to spend time in adding an additional check to all those conditions. Is there a way to programmatically set userAgent ? Something along the lines of:

navigator.userAgent += " Tizen" //does not work.

MDN says its a read-write property. I am not able to modify it though. Help me with this. Either another smart way to spoof userAgent or the correct way to set this. Thanks.

解决方案

My JS code has thousands of navigator checks

In that case it is your JS code that is at fault.

You've basically just discovered the ultimate reason why doing UA string detection is considered really bad practice.

In short, if your code looks as described, then you're doing something wrong. There are very few legitimate reasons for detecting the UA string, and none of those reasons apply to code running on the client itself.

And thousands of lines of this stuff in the browser???? That's can only be doing bad things to the performance of your site.

Is there a way to programmatically set userAgent? MDN says its a read-write property.

The userAgent string is a read-only value.

However, you can override the getter, so there are ways of making it writable. Ugly, but possible.

I'd really recommend avoiding doing this though.

Even without browser vendors deliberately changing their UA strings to break this kind of code (which they do), your task is fundamentally never-ending; you're going to have to keep revisiting this code every time a new device/browser/version is released, and your thousands of lines of code just will keep getting bigger and bigger.

Plus of course, it will be completely unreliable if a user modifies his browser's UA string.

Fix it now with a hack if you must, but be aware that this will keep eating more and more of your time. You really need to consider switching to a better coding practice such as feature detection instead of UA detection.

这篇关于如何设置导航器的userAgent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆