首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DART用户代理检测

DART用户代理检测
EN

Stack Overflow用户
提问于 2014-09-05 15:02:57
回答 2查看 3.4K关注 0票数 4

我希望检测我的应用程序正在运行的用户代理,主要是想看看它是否运行为:

  1. Chrome打包应用程序
  2. Chrome浏览器页面
  3. Android webView
  4. Firefox

我运行这段代码,作为启动:

代码语言:javascript
复制
var ua = window.navigator.userAgent;
print(ua);

得到了这个输出线

代码语言:javascript
复制
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.76 (Dart) Safari/537.36

但实际上我使用DARTIUM运行它,并拥有Chrome/FireFox/IE11

任何想法!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-12 12:14:06

来自common/device.dart

代码语言:javascript
复制
/**
 * 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;
  }
}
票数 0
EN

Stack Overflow用户

发布于 2014-10-11 19:39:43

Dartium自称为Chrome,但在用户代理中包含“(Dart)”。

您可以通过检查chrome.runtime.id来判断您的代码是否正在作为chrome扩展/应用程序运行,例如:

代码语言:javascript
复制
// 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来调整用户代理,这样你就可以在你的web应用程序中响应它:

代码语言:javascript
复制
myWebView.getSettings().setUserAgentString('Danny/1.0 not Chrome or IE or Webkit');

其他浏览器应该相对容易地使用它们的用户代理字符串。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25688834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档