I'm trying to detect with javascript if my website is running on a kindle fire mobile device. I've tried with navigator.userAgent and navigator.appVersion but I get this results on kindle :
5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16
and
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16
What can I use form those strings to know that I'm on a kindle and not on other device?
The User Agent String for Kindle Fire is:
Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Kindle Fire Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
In Silk mode:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true
in Javascript ,
var ua = navigator.userAgent;
var isKindle = /Kindle/i.test(ua) || /Silk/i.test(ua) || /KFTT/i.test(ua) || /KFOT/i.test(ua) || /KFJWA/i.test(ua) || /KFJWI/i.test(ua) || /KFSOWI/i.test(ua) || /KFTHWA/i.test(ua) || /KFTHWI/i.test(ua) || /KFAPWA/i.test(ua) || /KFAPWI/i.test(ua);
if(isKindle) {
//Your code here
}
there are two things you should check for 1/ Silk (or Silk-Accelerated) 2/ "Kindle", "KFOT", "KFTT" or others from the table at https://developer.amazon.com/sdk/fire/specifications.html
In Silk or pass-through #1 should give you confirmation, if the web page is being accessed from a WebView then #2 will catch it
One problem is that Amazon changes the strings for every new model. You could check only for Kindle, Silk and KF* but that could potentially lead to false positives. I have altered the code a bit from one of the examples above to make a bit more readable and easy to maintain.
As of November 18, 2015, the below code should work.
Check https://developer.amazon.com/sdk/fire/specifications.html for new models.
This is the code I wrote to redirect people to my game Luna Puma from my website for both Kindle Fire and Android phones:
<script type="text/javascript"> // <![CDATA[
var ua = navigator.userAgent;
var kindleStrings = [
"Kindle",
"Silk",
"KFTT",
"KFOT",
"KFJWA",
"KFJWI",
"KFSOWI",
"KFTHWA",
"KFTHWI",
"KFAPWA",
"KFAPWI",
"KFASWI",
"KFTBWI",
"KFMEWI",
"KFFOWI",
"KFSAWA",
"KFSAWI",
"KFARWI" ];
var isKindle = false;
for (index = 0; index < kindleStrings.length; index++) {
var matchRegExp = new RegExp (kindleStrings[index]);
if (matchRegExp.test (ua)) {
isKindle = true;
break;
}
}
if (isKindle) {
document.location = "amzn://apps/android?asin=B01859LRE0";
}
var isAndroid = /Android/i.test (ua);
if (isAndroid && !isKindle) {
document.location = "https://play.google.com/store/apps/details?id=com.xanamania.lunapuma";
} // ]]>
</script>
The Silk User-Agent and example JavaScript code to detect Silk can be found on the blog: http://amazonsilk.wordpress.com/useful-bits/silk-user-agent/
来源:https://stackoverflow.com/questions/9063427/how-can-i-detect-kindle-fire-with-javascript