Skip to content

Commit

Permalink
ignoring "document" object if not available. maybe fixed #44 ?
Browse files Browse the repository at this point in the history
"window" object is still being set (forced) and used. Need a better
solution to ignore "window" object as well.

Should we add "DetectRTC.version" to detect library's own version
number?
  • Loading branch information
muaz-khan committed May 21, 2017
1 parent 052b6c1 commit 58d395e
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 64 deletions.
49 changes: 23 additions & 26 deletions DetectRTC.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// Last time updated: 2017-04-29 7:05:22 AM UTC
'use strict';

// Latest file can be found here: https://cdn.webrtc-experiment.com/DetectRTC.js
// Last Updated On: 2017-05-21 3:58:52 AM UTC

// ________________
// DetectRTC v1.3.4

// Open-Sourced: https://github.com/muaz-khan/DetectRTC

// --------------------------------------------------
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Documentation - github.com/muaz-khan/DetectRTC
// ____________
// DetectRTC.js

// DetectRTC.hasWebcam (has webcam device!)
// DetectRTC.hasMicrophone (has microphone device!)
// DetectRTC.hasSpeakers (has speakers!)
// --------------------------------------------------

(function() {

'use strict';

var browserFakeUserAgent = 'Fake/5.0 (FakeOS) AppleWebKit/123 (KHTML, like Gecko) Fake/12.3.4567.89 Fake/123.45';

var isNodejs = typeof process === 'object' && typeof process.versions === 'object' && process.versions.node;
var isNodejs = typeof process === 'object' && typeof process.versions === 'object' && process.versions.node && /*node-process*/ !process.browser;
if (isNodejs) {
var version = process.versions.node.toString().replace('v', '');
browserFakeUserAgent = 'Nodejs/' + version + ' (NodeOS) AppleWebKit/' + version + ' (KHTML, like Gecko) Nodejs/' + version + ' Nodejs/' + version
Expand All @@ -41,15 +39,6 @@
// window = this;
}

if (typeof document === 'undefined') {
/*global document:true */
that.document = {};

document.createElement = document.captureStream = document.mozCaptureStream = function() {
return {};
};
}

if (typeof location === 'undefined') {
/*global location:true */
that.location = {
Expand Down Expand Up @@ -94,7 +83,7 @@
var isFirefox = typeof window.InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
var isChrome = !!window.chrome && !isOpera;
var isIE = !!document.documentMode && !isEdge;
var isIE = typeof document !== 'undefined' && !!document.documentMode && !isEdge;

// this one can also be used:
// https://www.websocket.org/js/stuff.js (DetectBrowser.js)
Expand Down Expand Up @@ -501,6 +490,10 @@
var isCanvasSupportsStreamCapturing = false;
var isVideoSupportsStreamCapturing = false;
['captureStream', 'mozCaptureStream', 'webkitCaptureStream'].forEach(function(item) {
if (typeof document === 'undefined' || typeof document.createElement !== 'function') {
return;
}

if (!isCanvasSupportsStreamCapturing && item in document.createElement('canvas')) {
isCanvasSupportsStreamCapturing = true;
}
Expand Down Expand Up @@ -535,6 +528,10 @@

//get the IP addresses associated with an account
function getIPs(callback) {
if (typeof document === 'undefined' || typeof document.getElementById !== 'function') {
return;
}

var ipDuplicates = {};

//compatibility for firefox and chrome
Expand Down Expand Up @@ -742,7 +739,7 @@
if (!device.label) {
device.label = 'Please invoke getUserMedia once.';
if (DetectRTC.browser.isChrome && DetectRTC.browser.version >= 46 && !/^(https:|chrome-extension:)$/g.test(location.protocol || '')) {
if (document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
if (typeof document !== 'undefined' && typeof document.domain === 'string' && document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
}
}
Expand Down Expand Up @@ -854,7 +851,7 @@
}

if (!/^(https:|chrome-extension:)$/g.test(location.protocol || '')) {
if (document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
if (typeof document !== 'undefined' && typeof document.domain === 'string' && document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
// DetectRTC.browser.isChrome
isScreenCapturingSupported = false;
}
Expand Down Expand Up @@ -918,7 +915,7 @@
}

if (DetectRTC.browser.isChrome && DetectRTC.browser.version >= 46 && !/^(https:|chrome-extension:)$/g.test(location.protocol || '')) {
if (document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
if (typeof document !== 'undefined' && typeof document.domain === 'string' && document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
isGetUserMediaSupported = 'Requires HTTPs';
}
}
Expand Down Expand Up @@ -1001,7 +998,7 @@

// ------
var isSetSinkIdSupported = false;
if ('setSinkId' in document.createElement('video')) {
if (typeof document !== 'undefined' && typeof document.createElement === 'function' && 'setSinkId' in document.createElement('video')) {
isSetSinkIdSupported = true;
}
DetectRTC.isSetSinkIdSupported = isSetSinkIdSupported;
Expand Down
16 changes: 14 additions & 2 deletions DetectRTC.min.js

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ module.exports = function(grunt) {
scope: 'devDependencies'
});

var banner = '// Last time updated: <%= grunt.template.today("UTC:yyyy-mm-dd h:MM:ss TT Z") %>\n\n';
var versionNumber = grunt.file.readJSON('package.json').version;

var banner = '\'use strict\';\n\n';
banner += '// Last Updated On: <%= grunt.template.today("UTC:yyyy-mm-dd h:MM:ss TT Z") %>\n\n';

banner += '// ________________\n';
banner += '// DetectRTC v' + versionNumber + '\n\n';

banner += '// Open-Sourced: https://github.com/muaz-khan/DetectRTC\n\n';

banner += '// --------------------------------------------------\n';
banner += '// Muaz Khan - www.MuazKhan.com\n';
banner += '// MIT License - www.WebRTC-Experiment.com/licence\n';
banner += '// --------------------------------------------------\n\n';

// configure project
grunt.initConfig({
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ DetectRTC.isMediaHintsSupportsNewSyntax
```
node server.js
# or
npm start
# and open:
127.0.0.1:9001
http://127.0.0.1:9001
# or
http://localhost:9001
Expand All @@ -69,6 +72,10 @@ Or try `npm-test.js`:
```
cd node_modules
cd detectrtc
npm test
# or
node npm-test.js
```

Expand Down
2 changes: 1 addition & 1 deletion dev/CheckDeviceSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function checkDeviceSupport(callback) {
if (!device.label) {
device.label = 'Please invoke getUserMedia once.';
if (DetectRTC.browser.isChrome && DetectRTC.browser.version >= 46 && !/^(https:|chrome-extension:)$/g.test(location.protocol || '')) {
if (document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
if (typeof document !== 'undefined' && typeof document.domain === 'string' && document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
}
}
Expand Down
4 changes: 4 additions & 0 deletions dev/DetectLocalIPAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function DetectLocalIPAddress(callback) {

//get the IP addresses associated with an account
function getIPs(callback) {
if (typeof document === 'undefined' || typeof document.getElementById !== 'function') {
return;
}

var ipDuplicates = {};

//compatibility for firefox and chrome
Expand Down
6 changes: 3 additions & 3 deletions dev/DetectRTC.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if (DetectRTC.browser.isChrome && DetectRTC.browser.version >= 35) {
}

if (!/^(https:|chrome-extension:)$/g.test(location.protocol || '')) {
if (document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
if (typeof document !== 'undefined' && typeof document.domain === 'string' && document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
// DetectRTC.browser.isChrome
isScreenCapturingSupported = false;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ if (navigator.getUserMedia) {
}

if (DetectRTC.browser.isChrome && DetectRTC.browser.version >= 46 && !/^(https:|chrome-extension:)$/g.test(location.protocol || '')) {
if (document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
if (typeof document !== 'undefined' && typeof document.domain === 'string' && document.domain.search && document.domain.search(/localhost|127.0./g) === -1) {
isGetUserMediaSupported = 'Requires HTTPs';
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ DetectRTC.videoInputDevices = videoInputDevices;

// ------
var isSetSinkIdSupported = false;
if ('setSinkId' in document.createElement('video')) {
if (typeof document !== 'undefined' && typeof document.createElement === 'function' && 'setSinkId' in document.createElement('video')) {
isSetSinkIdSupported = true;
}
DetectRTC.isSetSinkIdSupported = isSetSinkIdSupported;
Expand Down
11 changes: 1 addition & 10 deletions dev/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var browserFakeUserAgent = 'Fake/5.0 (FakeOS) AppleWebKit/123 (KHTML, like Gecko) Fake/12.3.4567.89 Fake/123.45';

var isNodejs = typeof process === 'object' && typeof process.versions === 'object' && process.versions.node;
var isNodejs = typeof process === 'object' && typeof process.versions === 'object' && process.versions.node && /*node-process*/ !process.browser;
if (isNodejs) {
var version = process.versions.node.toString().replace('v', '');
browserFakeUserAgent = 'Nodejs/' + version + ' (NodeOS) AppleWebKit/' + version + ' (KHTML, like Gecko) Nodejs/' + version + ' Nodejs/' + version
Expand All @@ -23,15 +23,6 @@ if (isNodejs) {
// window = this;
}

if (typeof document === 'undefined') {
/*global document:true */
that.document = {};

document.createElement = document.captureStream = document.mozCaptureStream = function() {
return {};
};
}

if (typeof location === 'undefined') {
/*global location:true */
that.location = {
Expand Down
4 changes: 4 additions & 0 deletions dev/detectCaptureStream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var isCanvasSupportsStreamCapturing = false;
var isVideoSupportsStreamCapturing = false;
['captureStream', 'mozCaptureStream', 'webkitCaptureStream'].forEach(function(item) {
if (typeof document === 'undefined' || typeof document.createElement !== 'function') {
return;
}

if (!isCanvasSupportsStreamCapturing && item in document.createElement('canvas')) {
isCanvasSupportsStreamCapturing = true;
}
Expand Down
2 changes: 1 addition & 1 deletion dev/getBrowserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isFirefox = typeof window.InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
var isChrome = !!window.chrome && !isOpera;
var isIE = !!document.documentMode && !isEdge;
var isIE = typeof document !== 'undefined' && !!document.documentMode && !isEdge;

// this one can also be used:
// https://www.websocket.org/js/stuff.js (DetectBrowser.js)
Expand Down
14 changes: 0 additions & 14 deletions dev/head.js
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
// Latest file can be found here: https://cdn.webrtc-experiment.com/DetectRTC.js

// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Documentation - github.com/muaz-khan/DetectRTC
// ____________
// DetectRTC.js

// DetectRTC.hasWebcam (has webcam device!)
// DetectRTC.hasMicrophone (has microphone device!)
// DetectRTC.hasSpeakers (has speakers!)

(function() {

'use strict';
11 changes: 8 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ <h2 id="welcome">DetectRTC!</h2>

var labels = [];
DetectRTC.audioOutputDevices.forEach(function(device) {

if(DetectRTC.browser.name === 'Edge' && device.label === 'Please invoke getUserMedia once.') {
device.label = 'Microsoft Edge is unable to detect label for this speaker device.';
}

labels.push(device.label);
});

Expand Down Expand Up @@ -249,9 +254,9 @@ <h2 id="welcome">DetectRTC!</h2>

appendTR('Is Browser Supports Promises?', printVal(DetectRTC.isPromisesSupported), 'isPromisesSupported');

appendTR(DetectRTC.MediaStream === false ? 'Your system does NOT supports MediaStream.' : 'Your system supports MediaStream.', '<strong>MediaStream.prototype:</strong><br>' + DetectRTC.MediaStream.toString().split(',').join(', '), 'MediaStream');
appendTR(DetectRTC.MediaStreamTrack === false ? 'Your system does NOT supports MediaStreamTrack.' : 'Your system supports MediaStreamTrack.', '<strong>MediaStreamTrack.prototype:</strong><br>' + DetectRTC.MediaStreamTrack.toString().split(',').join(', '), 'MediaStreamTrack');
appendTR(DetectRTC.RTCPeerConnection === false ? 'Your system does NOT supports RTCPeerConnection API.' : 'Your system supports RTCPeerConnection API.', '<strong>RTCPeerConnection.prototype:</strong><br>' + DetectRTC.RTCPeerConnection.toString().split(',').join(', '), 'RTCPeerConnection');
// appendTR(DetectRTC.MediaStream === false ? 'Your system does NOT supports MediaStream.' : 'Your system supports MediaStream.', '<strong>MediaStream.prototype:</strong><br>' + DetectRTC.MediaStream.toString().split(',').join(', '), 'MediaStream');
// appendTR(DetectRTC.MediaStreamTrack === false ? 'Your system does NOT supports MediaStreamTrack.' : 'Your system supports MediaStreamTrack.', '<strong>MediaStreamTrack.prototype:</strong><br>' + DetectRTC.MediaStreamTrack.toString().split(',').join(', '), 'MediaStreamTrack');
// appendTR(DetectRTC.RTCPeerConnection === false ? 'Your system does NOT supports RTCPeerConnection API.' : 'Your system supports RTCPeerConnection API.', '<strong>RTCPeerConnection.prototype:</strong><br>' + DetectRTC.RTCPeerConnection.toString().split(',').join(', '), 'RTCPeerConnection');

/*
DetectRTC.DetectLocalIPAddress(function(ipAddress) {
Expand Down
11 changes: 10 additions & 1 deletion npm-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// https://tonicdev.com/npm/detectrtc

var DetectRTC = require('detectrtc');
var DetectRTC;

try {
DetectRTC = require('detectrtc');
}
catch(e) {
DetectRTC = require('./DetectRTC.js');
}

console.log(DetectRTC.browser.name + ' version ' + DetectRTC.browser.version);
console.log(DetectRTC.osName + ' version ' + DetectRTC.osVersion);
console.log(JSON.stringify(DetectRTC));
console.log(DetectRTC);

process.exit()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"url": "https://github.com/muaz-khan/DetectRTC.git"
},
"scripts": {
"start": "node server.js"
"start": "node server.js",
"test": "node npm-test.js"
},
"main": "DetectRTC.js",
"keywords": [
Expand Down

0 comments on commit 58d395e

Please sign in to comment.