getScreenId.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Last time updated at Oct 24, 2015, 08:32:23
  2. // Latest file can be found here: https://cdn.webrtc-experiment.com/getScreenId.js
  3. // Muaz Khan - www.MuazKhan.com
  4. // MIT License - www.WebRTC-Experiment.com/licence
  5. // Documentation - https://github.com/muaz-khan/getScreenId.
  6. // ______________
  7. // getScreenId.js
  8. /*
  9. getScreenId(function (error, sourceId, screen_constraints) {
  10. // error == null || 'permission-denied' || 'not-installed' || 'installed-disabled' || 'not-chrome'
  11. // sourceId == null || 'string' || 'firefox'
  12. if(sourceId == 'firefox') {
  13. navigator.mozGetUserMedia(screen_constraints, onSuccess, onFailure);
  14. }
  15. else navigator.webkitGetUserMedia(screen_constraints, onSuccess, onFailure);
  16. });
  17. */
  18. (function() {
  19. window.getScreenId = function(callback) {
  20. // for Firefox:
  21. // sourceId == 'firefox'
  22. // screen_constraints = {...}
  23. if (!!navigator.mozGetUserMedia) {
  24. callback(null, 'firefox', {
  25. video: {
  26. mozMediaSource: 'window',
  27. mediaSource: 'window'
  28. }
  29. });
  30. return;
  31. }
  32. postMessage();
  33. window.addEventListener('message', onIFrameCallback);
  34. function onIFrameCallback(event) {
  35. if (!event.data) return;
  36. if (event.data.chromeMediaSourceId) {
  37. if (event.data.chromeMediaSourceId === 'PermissionDeniedError') {
  38. callback('permission-denied');
  39. } else callback(null, event.data.chromeMediaSourceId, getScreenConstraints(null, event.data.chromeMediaSourceId));
  40. }
  41. if (event.data.chromeExtensionStatus) {
  42. callback(event.data.chromeExtensionStatus, null, getScreenConstraints(event.data.chromeExtensionStatus));
  43. }
  44. // this event listener is no more needed
  45. window.removeEventListener('message', onIFrameCallback);
  46. }
  47. };
  48. function getScreenConstraints(error, sourceId) {
  49. var screen_constraints = {
  50. audio: false,
  51. video: {
  52. mandatory: {
  53. chromeMediaSource: error ? 'screen' : 'desktop',
  54. maxWidth: window.screen.width > 1920 ? window.screen.width : 1920,
  55. maxHeight: window.screen.height > 1080 ? window.screen.height : 1080
  56. },
  57. optional: []
  58. }
  59. };
  60. if (sourceId) {
  61. screen_constraints.video.mandatory.chromeMediaSourceId = sourceId;
  62. }
  63. return screen_constraints;
  64. }
  65. function postMessage() {
  66. if (!iframe) {
  67. loadIFrame(postMessage);
  68. return;
  69. }
  70. if (!iframe.isLoaded) {
  71. setTimeout(postMessage, 100);
  72. return;
  73. }
  74. iframe.contentWindow.postMessage({
  75. captureSourceId: true
  76. }, '*');
  77. }
  78. function loadIFrame(loadCallback) {
  79. if (iframe) {
  80. loadCallback();
  81. return;
  82. }
  83. iframe = document.createElement('iframe');
  84. iframe.onload = function() {
  85. iframe.isLoaded = true;
  86. loadCallback();
  87. };
  88. iframe.src = 'https://www.webrtc-experiment.com/getSourceId/'; // https://wwww.yourdomain.com/getScreenId.html
  89. iframe.style.display = 'none';
  90. (document.body || document.documentElement).appendChild(iframe);
  91. }
  92. var iframe;
  93. // this function is used in v3.0
  94. window.getScreenConstraints = function(callback) {
  95. loadIFrame(function() {
  96. getScreenId(function(error, sourceId, screen_constraints) {
  97. callback(error, screen_constraints.video);
  98. });
  99. });
  100. };
  101. })();
  102. (function() {
  103. if(document.domain.indexOf('webrtc-experiment.com') === -1) {
  104. return;
  105. }
  106. window.getScreenId = function(callback) {
  107. // for Firefox:
  108. // sourceId == 'firefox'
  109. // screen_constraints = {...}
  110. if (!!navigator.mozGetUserMedia) {
  111. callback(null, 'firefox', {
  112. video: {
  113. mozMediaSource: 'window',
  114. mediaSource: 'window'
  115. }
  116. });
  117. return;
  118. }
  119. postMessage();
  120. window.addEventListener('message', onIFrameCallback);
  121. function onIFrameCallback(event) {
  122. if (!event.data) return;
  123. if (event.data.chromeMediaSourceId) {
  124. if (event.data.chromeMediaSourceId === 'PermissionDeniedError') {
  125. callback('permission-denied');
  126. } else callback(null, event.data.chromeMediaSourceId, getScreenConstraints(null, event.data.chromeMediaSourceId));
  127. }
  128. if (event.data.chromeExtensionStatus) {
  129. callback(event.data.chromeExtensionStatus, null, getScreenConstraints(event.data.chromeExtensionStatus));
  130. }
  131. // this event listener is no more needed
  132. window.removeEventListener('message', onIFrameCallback);
  133. }
  134. };
  135. function getScreenConstraints(error, sourceId) {
  136. var screen_constraints = {
  137. audio: false,
  138. video: {
  139. mandatory: {
  140. chromeMediaSource: error ? 'screen' : 'desktop',
  141. maxWidth: window.screen.width > 1920 ? window.screen.width : 1920,
  142. maxHeight: window.screen.height > 1080 ? window.screen.height : 1080
  143. },
  144. optional: []
  145. }
  146. };
  147. if (sourceId) {
  148. screen_constraints.video.mandatory.chromeMediaSourceId = sourceId;
  149. }
  150. return screen_constraints;
  151. }
  152. function postMessage() {
  153. if (!iframe) {
  154. loadIFrame(postMessage);
  155. return;
  156. }
  157. if (!iframe.isLoaded) {
  158. setTimeout(postMessage, 100);
  159. return;
  160. }
  161. iframe.contentWindow.postMessage({
  162. captureSourceId: true
  163. }, '*');
  164. }
  165. function loadIFrame(loadCallback) {
  166. if (iframe) {
  167. loadCallback();
  168. return;
  169. }
  170. iframe = document.createElement('iframe');
  171. iframe.onload = function() {
  172. iframe.isLoaded = true;
  173. loadCallback();
  174. };
  175. iframe.src = 'https://www.webrtc-experiment.com/getSourceId/'; // https://wwww.yourdomain.com/getScreenId.html
  176. iframe.style.display = 'none';
  177. (document.body || document.documentElement).appendChild(iframe);
  178. }
  179. var iframe;
  180. // this function is used in v3.0
  181. window.getScreenConstraints = function(callback) {
  182. loadIFrame(function() {
  183. getScreenId(function(error, sourceId, screen_constraints) {
  184. callback(error, screen_constraints.video);
  185. });
  186. });
  187. };
  188. })();