-
Notifications
You must be signed in to change notification settings - Fork 4k
/
getScreenId.html
158 lines (137 loc) · 5.57 KB
/
getScreenId.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!--
This script is a hack used to support single chrome extension usage on any domain.
This script has issues, though.
It uses "postMessage" mechanism which fails to work if someone is using it from inside an <iframe>.
The only solution for such cases is, use WebSockets or external servers to pass "source-ids".
Note: If you want to host it yourself:
1) open ScreenId.js
2) replace following with "getScreenId.html" link:
iframe.src = 'https://www.webrtc-experiment.com/getSourceId/'; // replace it with below line
iframe.src = 'https://www.yourdomain.com/getScreenId.html';
-->
<script>
// https://github.com/muaz-khan/DetectRTC
var DetectRTC = {};
var screenCallback;
DetectRTC.screen = {
chromeMediaSource: 'screen',
getSourceId: function(callback) {
if (!callback) throw '"callback" parameter is mandatory.';
screenCallback = callback;
window.postMessage('get-sourceId', '*');
},
getCustomSourceId: function(arr, callback) {
if (!arr || !arr.forEach) throw '"arr" parameter is mandatory and it must be an array.';
if (!callback) throw '"callback" parameter is mandatory.';
screenCallback = callback;
window.postMessage({
'get-custom-sourceId': arr
}, '*');
},
getSourceIdWithAudio: function(callback) {
if (!callback) throw '"callback" parameter is mandatory.';
screenCallback = callback;
window.postMessage('audio-plus-tab', '*');
},
onMessageCallback: function(data) {
// "cancel" button is clicked
if (data == 'PermissionDeniedError') {
DetectRTC.screen.chromeMediaSource = 'PermissionDeniedError';
if (screenCallback) return screenCallback('PermissionDeniedError');
else throw new Error('PermissionDeniedError');
}
// extension notified his presence
if (data == 'rtcmulticonnection-extension-loaded') {
DetectRTC.screen.chromeMediaSource = 'desktop';
}
// extension shared temp sourceId
if (data.sourceId) {
DetectRTC.screen.sourceId = data.sourceId;
if (screenCallback) {
screenCallback(DetectRTC.screen.sourceId, data.canRequestAudioTrack === true);
}
}
},
getChromeExtensionStatus: function(callback) {
// https://chrome.google.com/webstore/detail/screen-capturing/ajhifddimkapgcifgcodmmfdlknahffk
var extensionid = 'ajhifddimkapgcifgcodmmfdlknahffk';
var image = document.createElement('img');
image.src = 'chrome-extension://' + extensionid + '/icon.png';
image.onload = function() {
if (!DetectRTC.screen) DetectRTC.screen = {};
if (!DetectRTC.screen.chromeMediaSource) DetectRTC.screen.chromeMediaSource = '';
if (DetectRTC.screen.chromeMediaSource === 'desktop') {
callback('installed-enabled');
return;
}
DetectRTC.screen.chromeMediaSource = 'screen';
window.postMessage('are-you-there', '*');
setTimeout(function() {
if (DetectRTC.screen.chromeMediaSource == 'screen') {
callback('installed-disabled');
} else {
callback('installed-enabled');
}
}, 2000);
};
image.onerror = function() {
callback('not-installed');
};
}
};
window.addEventListener('message', function(event) {
if (!event.data || !(typeof event.data == 'string' || event.data.sourceId || event.data.captureSourceId || event.data.captureCustomSourceId || event.data.getChromeExtensionStatus || event.data.captureSourceIdWithAudio)) return;
if (event.data.getChromeExtensionStatus) {
DetectRTC.screen.getChromeExtensionStatus(function(status) {
window.parent.postMessage({
chromeExtensionStatus: status
}, '*');
});
return;
}
if(event.data.captureCustomSourceId) {
captureSourceId(null, event.data.captureCustomSourceId);
}
if (event.data.captureSourceId) {
captureSourceId();
}
if (event.data.captureSourceIdWithAudio) {
captureSourceId('system_audio');
}
DetectRTC.screen.onMessageCallback(event.data);
});
function captureSourceId(system_audio, custom_sourceId) {
// check if desktop-capture extension installed.
DetectRTC.screen.getChromeExtensionStatus(function(status) {
if (status != 'installed-enabled') {
window.parent.postMessage({
chromeExtensionStatus: status
}, '*');
return;
}
if(!!custom_sourceId) {
DetectRTC.screen.getCustomSourceId(custom_sourceId, function(sourceId, canRequestAudioTrack) {
window.parent.postMessage({
chromeMediaSourceId: sourceId,
canRequestAudioTrack: !!canRequestAudioTrack
}, '*');
});
}
else if(!system_audio && !custom_sourceId) {
DetectRTC.screen.getSourceId(function(sourceId) {
window.parent.postMessage({
chromeMediaSourceId: sourceId
}, '*');
});
}
else {
DetectRTC.screen.getSourceIdWithAudio(function(sourceId, canRequestAudioTrack) {
window.parent.postMessage({
chromeMediaSourceId: sourceId,
canRequestAudioTrack: !!canRequestAudioTrack
}, '*');
});
}
});
}
</script>