DART用户代理检测
我希望以便检测我的应用程序正在运行,主要是我想看看它是否正在运行,并且用户代理:DART用户代理检测
- Chrome封装应用,
- Chrome浏览器页面,
- Android的web视图
- 火狐
我运行此代码,为出发:
var ua = window.navigator.userAgent;
print(ua);
,并得到了这个输出线
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.76 (Dart) Safari/537.36
但实际上我用DARTIUM运行它,并有镀铬/火狐/ IE11
任何想法!
从https://chromium.googlesource.com/external/dart/+/master/dart/sdk/lib/html/html_common/device.dart
/**
* Utils for device detection.
*/
class Device {
static bool _isOpera;
static bool _isIE;
static bool _isFirefox;
static bool _isWebKit;
static String _cachedCssPrefix;
static String _cachedPropertyPrefix;
/**
* Gets the browser's user agent. Using this function allows tests to inject
* the user agent.
* Returns the user agent.
*/
static String get userAgent => window.navigator.userAgent;
/**
* Determines if the current device is running Opera.
*/
static bool get isOpera {
if (_isOpera == null) {
_isOpera = userAgent.contains("Opera", 0);
}
return _isOpera;
}
/**
* Determines if the current device is running Internet Explorer.
*/
static bool get isIE {
if (_isIE == null) {
_isIE = !isOpera && userAgent.contains("Trident/", 0);
}
return _isIE;
}
/**
* Determines if the current device is running Firefox.
*/
static bool get isFirefox {
if (_isFirefox == null) {
_isFirefox = userAgent.contains("Firefox", 0);
}
return _isFirefox;
}
/**
* Determines if the current device is running WebKit.
*/
static bool get isWebKit {
if (_isWebKit == null) {
_isWebKit = !isOpera && userAgent.contains("WebKit", 0);
}
return _isWebKit;
}
/**
* Gets the CSS property prefix for the current platform.
*/
static String get cssPrefix {
String prefix = _cachedCssPrefix;
if (prefix != null) return prefix;
if (isFirefox) {
prefix = '-moz-';
} else if (isIE) {
prefix = '-ms-';
} else if (isOpera) {
prefix = '-o-';
} else {
prefix = '-webkit-';
}
return _cachedCssPrefix = prefix;
}
/**
* Prefix as used for JS property names.
*/
static String get propertyPrefix {
String prefix = _cachedPropertyPrefix;
if (prefix != null) return prefix;
if (isFirefox) {
prefix = 'moz';
} else if (isIE) {
prefix = 'ms';
} else if (isOpera) {
prefix = 'o';
} else {
prefix = 'webkit';
}
return _cachedPropertyPrefix = prefix;
}
/**
* Checks to see if the event class is supported by the current platform.
*/
static bool isEventTypeSupported(String eventType) {
// Browsers throw for unsupported event names.
try {
var e = new Event.eventType(eventType, '');
return e is Event;
} catch (_) { }
return false;
}
}
Dartium将自己标识为Chrome,但在用户代理中包含'(Dart)'。
,如果你的代码运行的浏览器扩展程序/应用程序通过检查chrome.runtime.id
,你可以告诉例如:
// You'll likely need to reference this package:
// https://pub.dartlang.org/packages/chrome
if (window.chrome && window.chrome.runtime && window.chrome.runtime.id) {
window.alert('App!');
}
如果您在Android应用程序的控制的时候,你可以用setUserAgentString
来调整用户代理,所以你可以在你的网络应用中回应它:
myWebView.getSettings().setUserAgentString('Danny/1.0 not Chrome or IE or Webkit');
其他浏览器应该相对容易使用他们的用户代理字符串。
您可以添加有关Android的setUserAgentString的一些细节。它是在清单中还是在我的应用程序中!谢谢 – 2014-10-11 19:44:09
@HasanAYousef见编辑;你只需在web视图中调用getSettings()。setUserAgentString('blah');'。 – 2014-10-11 19:47:47
这是预期。您应该阅读[用户代理字符串](http://en.wikipedia.org/wiki/User_agent)。 – 2014-09-05 15:11:34
我自己还没有使用它,但https://pub.dartlang.org/packages/browser_detect可能会有所帮助。 – 2014-09-05 15:15:03