2
0

getScreenId.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Last time updated at Sep 07, 2014, 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/WebRTC-Experiment/tree/master/getScreenId.js
  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.isLoaded) {
  67. setTimeout(postMessage, 100);
  68. return;
  69. }
  70. iframe.contentWindow.postMessage({
  71. captureSourceId: true
  72. }, '*');
  73. }
  74. var iframe = document.createElement('iframe');
  75. iframe.onload = function() {
  76. iframe.isLoaded = true;
  77. };
  78. iframe.src = 'https://www.webrtc-experiment.com/getSourceId/';
  79. iframe.style.display = 'none';
  80. (document.body || document.documentElement).appendChild(iframe);
  81. })();