1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const names = {
|
14 | GET_USER_MEDIA: 'navigator.getUserMedia',
|
15 | MEDIA_RECORDER: 'MediaRecorder',
|
16 | WEBCAM: 'webcam',
|
17 | DOCUMENT_ALL: 'document.all',
|
18 | DOCUMENT_QUERY_SELECTOR: 'document.querySelector',
|
19 | HTTP: 'http',
|
20 | HTTPS: 'https',
|
21 | LOCALHOST: 'localhost',
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | const tests = [
|
29 | {
|
30 | capabilityName: names.GET_USER_MEDIA,
|
31 |
|
32 | test() {
|
33 | return !!(navigator.getUserMedia
|
34 | || navigator.webkitGetUserMedia
|
35 | || navigator.mozGetUserMedia
|
36 | || navigator.msGetUserMedia
|
37 | || (navigator.mediaDevices && navigator.mediaDevices.getUserMedia));
|
38 | },
|
39 | },
|
40 | {
|
41 | capabilityName: names.MEDIA_RECORDER,
|
42 |
|
43 | test() {
|
44 | return typeof window.MediaRecorder === 'function';
|
45 | },
|
46 | },
|
47 | {
|
48 | capabilityName: names.DOCUMENT_ALL,
|
49 |
|
50 | test() {
|
51 | return document.querySelector !== undefined;
|
52 | },
|
53 | },
|
54 | {
|
55 | capabilityName: names.DOCUMENT_QUERY_SELECTOR,
|
56 |
|
57 | test() {
|
58 | return typeof document.querySelector === 'function';
|
59 | },
|
60 | },
|
61 | {
|
62 | capabilityName: names.HTTP,
|
63 |
|
64 | test() {
|
65 | return document.location.protocol === 'http:';
|
66 | },
|
67 | },
|
68 | {
|
69 | capabilityName: names.HTTPS,
|
70 |
|
71 | test() {
|
72 | return document.location.protocol === 'https:';
|
73 | },
|
74 | },
|
75 | {
|
76 | capabilityName: names.LOCALHOST,
|
77 |
|
78 | test() {
|
79 | return document.location.hostname === 'localhost' || document.location.hostname === '127.0.0.1';
|
80 | },
|
81 | },
|
82 | ];
|
83 |
|
84 |
|
85 | export default {
|
86 | names,
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 | detect() {
|
94 | const results = [];
|
95 |
|
96 | for (let i = 0; i < tests.length; i++) {
|
97 | if (tests[i].test()) {
|
98 | results.push(tests[i].capabilityName);
|
99 | }
|
100 | }
|
101 |
|
102 | return results;
|
103 | },
|
104 | };
|