DART User Agent Discovery

I want to detect the user agent my application is running on, basically I want to see if it works like:

  • Chrome packaged app
  • Chrome browser page,
  • Android webView
  • Firefox

I am running this code starting from:

var ua = window.navigator.userAgent;
print(ua);

      

and got this output line

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.76 (Dart) Safari/537.36

      

but actually I run it with DARTIUM and have Chrome / FireFox / IE11

any thought!

+3


source to share


2 answers


From 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 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


source


Dartium identifies itself as Chrome, but contains "(Dart)" in the user agent.

You can find out if your code works as a chrome extension / app by checking chrome.runtime.id

for example:

// 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!');
}

      



If you are in control of an Android application, you can use setUserAgentString

to set up a user agent so that you can then respond to it in your web application:

myWebView.getSettings().setUserAgentString('Danny/1.0 not Chrome or IE or Webkit');

      

Other browsers should be relatively lightweight using their user agent strings.

+3


source







All Articles