【问题标题】:DART user Agent detectionDART 用户代理检测
【发布时间】:2014-10-30 13:57:41
【问题描述】:

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

  1. Chrome 打包应用,
  2. Chrome 浏览器页面,
  3. Android 网络视图
  4. 火狐

我运行这段代码,作为开始:

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 运行它,并且拥有 Chrome/FireFox/IE11

任何想法!

【问题讨论】:

标签: android google-chrome firefox dart


【解决方案1】:

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

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

// 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 的细节。是在清单中还是在我的应用程序中!谢谢
  • @HasanAYousef 查看编辑;您只需在网络视图中调用getSettings().setUserAgentString('blah');
【解决方案2】:

来自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;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多