简体   繁体   English

区分Android Chrome和库存浏览器。 Stock浏览器的用户代理包含'Chrome'

[英]Distinguish Android Chrome from stock browser. Stock browser's user agent contains 'Chrome'

I need to tell what browser the users are using for my website. 我需要告诉用户我的网站使用的浏览器。 (edit: Users need to add a bookmarklet, which is not possible on the standard 'internet' browser. I need to know what message to show them.) (编辑:用户需要添加书签,这在标准的“互联网”浏览器上是不可能的。我需要知道要向他们展示什么消息。)

EDIT: I don't need to be able to detect any kind of browser. 编辑:我不需要能够检测任何类型的浏览器。 Specifically I need, in this case, to be able to detect whether a browser is truly a Google Chrome browser . 在这种情况下,我特别需要能够检测浏览器是否真的是谷歌Chrome浏览器

For at least one smart device, I am having trouble telling the difference between the stock 'internet' browser and Chrome; 对于至少一个智能设备,我无法分辨股票'互联网'浏览器和Chrome之间的区别; Both contain the word 'Chrome'. 两者都包含“Chrome”一词。

Samsung galaxy s5: 三星galaxy s5:

Stock browser user agent: 股票浏览器用户代理:

Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG-SM-G900A Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.6 Chrome/28.0.1500.94 Mobile Safari/537.36 Mozilla / 5.0(Linux; Android 4.4.2; zh-CN; SAMSUNG-SM-G900A Build / KOT49H)AppleWebKit / 537.36(KHTML,类似Gecko)版本/ 1.6 Chrome / 28.0.1500.94 Mobile Safari / 537.36

Chrome user agent: Chrome用户代理:

Mozilla/5.0 (Linux; Android 4.4.2; SAMSUNG-SM-G900A Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.128 Mobile Safari/537.36 Mozilla / 5.0(Linux; Android 4.4.2; SAMSUNG-SM-G900A Build / KOT49H)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 36.0.1985.128 Mobile Safari / 537.36

"Version/Xx" is different, but will that always be the case? “版本/ Xx”是不同的,但总是如此吗?

Edit: I did already check for previous answers as suggested in the comments. 编辑:我已经按照评论中的建议检查了以前的答案。 They assume that the non-Chrome browser does NOT contain the word Chrome. 他们认为非Chrome浏览器不包含Chrome一词。

So the difference is this in the user agent: 所以区别在于用户代理:

Version/ XX 版本/ XX

From https://developer.chrome.com/multidevice/user-agent#webview_user_agent : 来自https://developer.chrome.com/multidevice/user-agent#webview_user_agent

"If you're attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/ XX string in the WebView user-agent string." “如果你试图区分WebView和Android版Chrome,你应该在WebView用户代理字符串中查找Version / XX字符串的存在。”

I suppose a Chrome webview browser can still choose to leave that bit out, but if it DOES have it, then at least I know it isn't true Google Chrome! 我认为Chrome浏览器webview浏览器仍然可以选择退出,但如果它拥有它,那么至少我知道谷歌Chrome不是真的!

Please check this link: https://developer.chrome.com/multidevice/user-agent In the last paragraph we find: 请查看以下链接: https//developer.chrome.com/multidevice/user-agent在最后一段中,我们发现:

If you're attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/ XX string in the WebView user-agent string. 如果您尝试区分WebView和Android版Chrome,则应在WebView用户代理字符串中查找Version / XX字符串的存在。

Since I'm not aware of any blatant feature differences between the stock browser and chrome, playing 'spot the difference' with the user agent string reveals that the Chrome version stated in the stock browser is quite a few versions back. 由于我不知道股票浏览器和Chrome之间存在任何明显的功能差异,因此与用户代理字符串“发现差异”显示,在浏览器浏览器中显示的Chrome版本有很多版本。

Whilst its not the best difference and the stock browser could always update, at least at the moment, this may work ok for you. 虽然它不是最好的区别,并且股票浏览器可以随时更新,至少在目前,这可能对您有用。

... you haven't really said how you will be using this. ......你还没有真正说过你将如何使用它。

(I'm not allowed to comment, so I write an answer instead.) (我不允许发表评论,所以我写了一个答案。)

User984003 so, if I understand you right (from your comment), you want essentially a test to distinguish, on mobile devices, between stock browsers and an browser installed by the user. User984003所以,如果我理解你(从你的评论中),你基本上想要一个测试,以便在移动设备上区分股票浏览器和用户安装的浏览器。 You want this because there is a difference between them in the availableness of respectively the permission to install bookmarklets, right? 你想要这个,因为它们分别在安装bookmarklet的权限的可用性方面存在差异,对吧?

Then this topic is discussed in this thread: How to detect the stock Android browser (The short and unsatisfactory answer in this thread was: “There is no reliable and universal way to detect only stock browsers.”) 然后在这个主题中讨论这个话题: 如何检测股票Android浏览器 (这个帖子中的简短且不尽如人意的答案是:“没有可靠且通用的方法来仅检测股票浏览器。”)

Additionally here some informations on ways to install bookmarklets on android: http://smallbusiness.chron.com/making-bookmarklets-work-android-50685.html (Maybe here are some alternatives you are not aware of, which makes the attempts to distinguish the browser versions obsolete.) 另外这里有关于在an​​droid上安装bookmarklet的方法的一些信息: http//smallbusiness.chron.com/making-bookmarklets-work-android-50685.html (也许这里有一些你不知道的替代品,这使得尝试区分浏览器版本已过时。)

Read a lot of stuff, and it seems that they all don't do much and it's still a problem to see the difference. 阅读了很多东西,似乎它们都做得不多,看到差异仍然是一个问题。 I searched the net, no solution. 我在网上搜索,没有解决方案。

I went to check this on my own, and found this: 我自己去检查一下,发现了这个:

原生和Chrome之间的差异

So I found out that native browser (on Samsung S4) did append SamsungBrowser after "Gecko)". 所以我发现原生浏览器(在三星S4上)确实在“Gecko”之后添加了SamsungBrowser。

On Chrome, its "like Gecko) Chrome". 在Chrome上,它的“像Gecko”Chrome“。

So immediately after "Gecko)", it's " Chrome". 因此,在“Gecko”之后,它就是“Chrome”。

Maybe that could do the trick since Samsung appends it's own string into the user agent. 也许这可以解决问题,因为三星将自己的字符串附加到用户代理中。

Is it possible that other devices could again cause problems ? 是否有可能其他设备再次出现问题?

You can use JavaScript eg 你可以使用JavaScript,例如

var chromeVersion = /^Google/.test(navigator.vendor) && !/ OPR/.test(navigator.userAgent) && /Chrome\/([0-9]+|$)/.exec(navigator.userAgent)[1];
var isChromeWebview = chromeVersion >= 30 && !('chrome' in window);

window.chrome is defined in the Chrome Browser (from at least Chrome 10) but it is not defined in Chrome WebView (Chrome was first used for the WebView on Android 4.4.x; AOSP was used for the WebView for Android 4.3 or less). window.chrome在Chrome浏览器中定义(至少来自Chrome 10),但Chrome WebView中未定义(Chrome首先用于Android 4.4.x上的WebView; AOSP用于Android 4.3或更低版本的WebView) 。

I personally wouldn't use that test in production (it will be unreliable for different browsers and versions), but it does what you want if you use it for a purpose that isn't critical. 我个人不会在生产中使用该测试(对于不同的浏览器和版本,它将是不可靠的),但是如果您将其用于非关键目的,它会执行您想要的操作。

I'd recommend something like this: 我推荐这样的东西:

function testForAndroidChrome() {
    var version = navigator.appVersion;
    if (version.indexOf('(KHTML, like Gecko) Chrome' != -1) {
        return true; // it's Chrome
    } else {
        return false; // it's the stock browser
    }
}

var myTest = testForAndroidChrome(); // returns true or false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM