javascript 如何设置导航器 userAgent?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18960908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:51:03  来源:igfitidea点击:

How to set navigator userAgent?

javascriptbrowseruser-agent

提问by Rajat Sharma

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:

好的,这是我设法解决的边缘情况。我正在新的 Tizen 操作系统上测试我的应用程序。我的 JS 代码有数千个导航器检查。就像是:

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:

现在,我的测试设备上的 Tizen OS 的 userAgent 没有这些字符串中的任何一个。结果,我的很多 css 和 JS 都崩溃了。我现在处于 POC 模式,不想花时间为所有这些条件添加额外的检查。有没有办法以编程方式设置 userAgent ?类似的东西:

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

MDNsays 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.

MDN说它是一个读写属性。虽然我无法修改它。帮我解决这个问题。欺骗 userAgent 的另一种聪明方法或设置它的正确方法。谢谢。

回答by Spudley

My JS code has thousands of navigator checks

我的 JS 代码有数千个导航器检查

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

在这种情况下,是您的 JS 代码有问题。

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

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

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.

简而言之,如果您的代码看起来像描述的那样,那么您做错了。检测 UA 字符串的正当理由很少,而且这些理由都​​不适用于在客户端本身上运行的代码。

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.

有没有办法以编程方式设置 userAgent?MDN 说它是一个读写属性。

The userAgent string is a read-only value.

userAgent 字符串是只读值。

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

但是,您可以覆盖 getter,因此有多种方法可以使其可写。丑陋,但可能。

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.

即使浏览器供应商没有故意改变他们的 UA 字符串来破坏这种代码(他们这样做了),你的任务基本上是永无止境的;每次发布新设备/浏览器/版本时,您都必须不断重新访问此代码,而您的数千行代码只会越来越大。

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

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

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.

如果必须,请立即使用 hack 修复它,但请注意,这会占用您越来越多的时间。您确实需要考虑切换到更好的编码实践,例如特征检测而不是 UA 检测。

回答by Philipp M?hler

I recently created a gist to make readonly propertys writable (You can find an example to overwrite the navigator.userAgent). Like Spudley said, it is ugly and not recommended but I used it for unit tests to change user agents on the fly, so be careful.

我最近创建了一个要点来使只读属性可写(您可以找到一个覆盖 navigator.userAgent 的示例)。就像 Spudley 所说的那样,它很丑,不推荐,但我将它用于单元测试以动态更改用户代理,所以要小心。

Gist: https://gist.github.com/moehlone/bed7dd6cb38fc55bd640

要点:https: //gist.github.com/moehlon/bed7dd6cb38fc55bd640