Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Files and directories created by pub
.packages
.pub/
build/
coverage/
packages
pubspec.lock

# Directory created by dartdoc
doc/api/

# JetBrains IDEs
.idea/
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2015 Workiva Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# platform_detect

A library for detecting browser and platform type and version.

## Usage

A simple usage example:

```dart
import 'package:platform_detect/platform_detect.dart';

main() {
if (browser.isChrome) {
print('thank you for being a friend');
}

if (operatingSystem.isMac) {
print('');
}
}
```
58 changes: 58 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Platform Detect example</title>
</head>
<body>

<div>
<h1>Current platform</h1>
<h2>Your Browser: <span id="current-browser"></span></h2>
<ul>
<li><label>vendor: <span id="current-vendor"></span></label></li>
<li><label>appVersion: <span id="current-appVersion"></span></label></li>
<li><label>appName: <span id="current-appName"></span></label></li>
<li><label>userAgent: <span id="current-userAgent"></span></label></li>
<li><input type="checkbox" disabled="true" id="current-is-chrome"> Is Chrome</li>
<li><input type="checkbox" disabled="true" id="current-is-firefox"> Is Firefox</li>
<li><input type="checkbox" disabled="true" id="current-is-ie"> Is Internet Explorer</li>
<li><input type="checkbox" disabled="true" id="current-is-safari"> Is Safari</li>
</ul>

<div>
Version: <span id="current-version"></span>
</div>
<h2>Your Operating System: <span id="current-os"></span></h2>

<h1>Test other values</h1>
<table>
<tr>
<td>vendor</td>
<td><input id="test-vendor" style="width: 400px"/></td>
</tr>
<tr>
<td>appVersion</td>
<td><input id="test-appVersion" style="width: 400px"/></td>
</tr>
<tr>
<td>appName</td>
<td><input id="test-appName" style="width: 400px"/></td>
</tr>
<tr>
<td>userAgent</td>
<td><input id="test-userAgent" style="width: 400px"/></td>
</tr>
</table>
<button id="evaluate-test">Evaluate Test Values</button>
<h2>Evaluates to:</h2>
<ul>
<li><label>Browser: <span id="test-browser-name"></span></label></li>
<li><label>Version: <span id="test-browser-version"></span></label></li>
<li><label>Operating System: <span id="test-os-name"></span></label></li>
</ul>
</div>

<script type="application/dart" src="main.dart"></script>
<script src="/packages/browser/dart.js"></script>
</body>
</html>
53 changes: 53 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'dart:html';
import 'package:platform_detect/src/navigator.dart';
import 'package:platform_detect/src/browser.dart';
import 'package:platform_detect/src/operating_system.dart';
import 'package:platform_detect/platform_detect.dart';

main() {
_parseCurrentBrowser();
ButtonElement evaluate = querySelector('#evaluate-test');
evaluate.onClick.listen((_) => _parseTestValues());
}

void _parseCurrentBrowser() {
document.querySelector('#current-browser').text = browser.name;
document.querySelector('#current-vendor').text = window.navigator.vendor;
document.querySelector('#current-appVersion').text = window.navigator.appVersion;
document.querySelector('#current-appName').text = window.navigator.appName;
document.querySelector('#current-userAgent').text = window.navigator.userAgent;

CheckboxInputElement isChrome = document.querySelector('#current-is-chrome');
isChrome.checked = browser.isChrome;

CheckboxInputElement isFirefox = document.querySelector('#current-is-firefox');
isFirefox.checked = browser.isFirefox;

CheckboxInputElement isSafari = document.querySelector('#current-is-safari');
isSafari.checked = browser.isSafari;

CheckboxInputElement isInternetExplorer = document.querySelector('#current-is-ie');
isInternetExplorer.checked = browser.isInternetExplorer;

document.querySelector('#current-version').text = browser.version.toString();

document.querySelector('#current-os').text = operatingSystem.name;
}

void _parseTestValues() {
InputElement testVendor = querySelector('#test-vendor');
InputElement testAppVersion = querySelector('#test-appVersion');
InputElement testAppName = querySelector('#test-appName');
InputElement testUserAgent = querySelector('#test-userAgent');
var navigator = new TestNavigator();
navigator.vendor = testVendor.value.trim();
navigator.appVersion = testAppVersion.value.trim();
navigator.appName = testAppName.value.trim();
navigator.userAgent = testUserAgent.value.trim();
Browser.navigator = navigator;
OperatingSystem.navigator = navigator;

querySelector('#test-browser-name').text = browser.name;
querySelector('#test-browser-version').text = browser.version.toString();
querySelector('#test-os-name').text = operatingSystem.name;
}
41 changes: 41 additions & 0 deletions lib/platform_detect.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// Enables detection of browser type and version and operating system
///
/// Use browser.isChrome or operatingSystem.isMac
library platform_detect;

import 'dart:html';

import 'package:platform_detect/src/browser.dart';
import 'package:platform_detect/src/navigator.dart';
import 'package:platform_detect/src/operating_system.dart';

Browser _browser;

/// Current browser info
Browser get browser {
if (_browser == null) {
Browser.navigator = new _HtmlNavigator();
_browser = Browser.getCurrentBrowser();
}

return _browser;
}

OperatingSystem _operatingSystem;

/// Current operating system info
OperatingSystem get operatingSystem {
if (_operatingSystem == null) {
OperatingSystem.navigator = new _HtmlNavigator();
_operatingSystem = OperatingSystem.getCurrentOperatingSystem();
}

return _operatingSystem;
}

class _HtmlNavigator implements NavigatorProvider {
String get vendor => window.navigator.vendor;
String get appVersion => window.navigator.appVersion;
String get appName => window.navigator.appName;
String get userAgent => window.navigator.userAgent;
}
146 changes: 146 additions & 0 deletions lib/src/browser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
library platform_detect.browser;

import 'package:pub_semver/pub_semver.dart';
import 'package:platform_detect/src/navigator.dart';

/// Matches a browser name with how it is represented in window.navigator
class Browser {
static NavigatorProvider navigator;

static Browser getCurrentBrowser() {
return _knownBrowsers.firstWhere(
(browser) => browser._matchesNavigator(navigator),
orElse: () => UnknownBrowser);
}

static Browser UnknownBrowser = new Browser('Unknown', null, null);

Browser(this.name, bool matchesNavigator(NavigatorProvider navigator),
Version parseVersion(NavigatorProvider navigator))
: this._matchesNavigator = matchesNavigator,
this._parseVersion = parseVersion;

final String name;
final Function _matchesNavigator;
final Function _parseVersion;

Version _version;

Version get version {
if (_version == null) {
if (_parseVersion != null) {
_version = _parseVersion(Browser.navigator);
} else {
_version = new Version(0, 0, 0);
}
}

return _version;
}

static List<Browser> _knownBrowsers = [
_chrome,
_firefox,
_safari,
_internetExplorer
];

bool get isChrome => this == _chrome;
bool get isFirefox => this == _firefox;
bool get isSafari => this == _safari;
bool get isInternetExplorer => this == _internetExplorer;
}

Browser _chrome = new _Chrome();
Browser _firefox = new _Firefox();
Browser _safari = new _Safari();
Browser _internetExplorer = new _InternetExplorer();

class _Chrome extends Browser {
_Chrome() : super('Chrome', _isChrome, _getVersion);

static bool _isChrome(NavigatorProvider navigator) {
var vendor = navigator.vendor;
return vendor != null && vendor.contains('Google');
}

static Version _getVersion(NavigatorProvider navigator) {
Match match = new RegExp(r"Chrome/(\d+)\.(\d+)\.(\d+)\.(\d+)\s")
.firstMatch(navigator.appVersion);
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
var patch = int.parse(match.group(3));
var build = match.group(4);
return new Version(major, minor, patch, build: build);
}
}

class _Firefox extends Browser {
_Firefox() : super('Firefox', _isFirefox, _getVersion);

static bool _isFirefox(NavigatorProvider navigator) {
return navigator.userAgent.contains('Firefox');
}

static Version _getVersion(NavigatorProvider navigator) {
Match match =
new RegExp(r'rv:(\d+)\.(\d+)\)').firstMatch(navigator.userAgent);
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
return new Version(major, minor, 0);
}
}

class _Safari extends Browser {
_Safari() : super('Safari', _isSafari, _getVersion);

static bool _isSafari(NavigatorProvider navigator) {
return navigator.vendor.contains('Apple');
}

static Version _getVersion(NavigatorProvider navigator) {
Match match = new RegExp(r'Version/(\d+)\.(\d+)\.(\d+)')
.firstMatch(navigator.appVersion);
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
var patch = int.parse(match.group(3));
return new Version(major, minor, patch);
}
}

class _InternetExplorer extends Browser {
_InternetExplorer()
: super('Internet Explorer', _isInternetExplorer, _getVersion);

static bool _isInternetExplorer(NavigatorProvider navigator) {
return navigator.appName.contains('Microsoft') ||
navigator.appVersion.contains('Trident') ||
navigator.appVersion.contains('Edge');
}

static Version _getVersion(NavigatorProvider navigator) {
Match match =
new RegExp(r'MSIE (\d+)\.(\d+);').firstMatch(navigator.appVersion);
if (match != null) {
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
return new Version(major, minor, 0);
}

match = new RegExp(r'rv[: ](\d+)\.(\d+)').firstMatch(navigator.appVersion);
if (match != null) {
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
return new Version(major, minor, 0);
}

match = new RegExp(r'Edge/(\d+)\.(\d+)$').firstMatch(navigator.appVersion);
if (match != null) {
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
return new Version(major, minor, 0);
}

return new Version(0, 0, 0);
}
}
17 changes: 17 additions & 0 deletions lib/src/navigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library platform_detect.navigator;

/// Abstraction over window.navigator so we can run tests in the VM
abstract class NavigatorProvider {
String get vendor;
String get appVersion;
String get appName;
String get userAgent;
}

/// Simple implementation that enables ease of unit testing
class TestNavigator implements NavigatorProvider {
String vendor = '';
String appVersion = '';
String appName = '';
String userAgent = '';
}
Loading