You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

API.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Implements API class that communicates with external api class
  3. * and provides interface to access Jitsi Meet features by external
  4. * applications that embed Jitsi Meet
  5. */
  6. /**
  7. * List of the available commands.
  8. * @type {{
  9. * displayName: inputDisplayNameHandler,
  10. * muteAudio: toggleAudio,
  11. * muteVideo: toggleVideo,
  12. * filmStrip: toggleFilmStrip
  13. * }}
  14. */
  15. var commands =
  16. {
  17. displayName: UI.inputDisplayNameHandler,
  18. muteAudio: UI.toggleAudio,
  19. muteVideo: UI.toggleVideo,
  20. toggleFilmStrip: UI.toggleFilmStrip,
  21. toggleChat: UI.toggleChat,
  22. toggleContactList: UI.toggleContactList
  23. };
  24. /**
  25. * Maps the supported events and their status
  26. * (true it the event is enabled and false if it is disabled)
  27. * @type {{
  28. * incomingMessage: boolean,
  29. * outgoingMessage: boolean,
  30. * displayNameChange: boolean,
  31. * participantJoined: boolean,
  32. * participantLeft: boolean
  33. * }}
  34. */
  35. var events =
  36. {
  37. incomingMessage: false,
  38. outgoingMessage:false,
  39. displayNameChange: false,
  40. participantJoined: false,
  41. participantLeft: false
  42. };
  43. /**
  44. * Processes commands from external applicaiton.
  45. * @param message the object with the command
  46. */
  47. function processCommand(message)
  48. {
  49. if(message.action != "execute")
  50. {
  51. console.error("Unknown action of the message");
  52. return;
  53. }
  54. for(var key in message)
  55. {
  56. if(commands[key])
  57. commands[key].apply(null, message[key]);
  58. }
  59. }
  60. /**
  61. * Processes events objects from external applications
  62. * @param event the event
  63. */
  64. function processEvent(event) {
  65. if(!event.action)
  66. {
  67. console.error("Event with no action is received.");
  68. return;
  69. }
  70. var i = 0;
  71. switch(event.action)
  72. {
  73. case "add":
  74. for(; i < event.events.length; i++)
  75. {
  76. events[event.events[i]] = true;
  77. }
  78. break;
  79. case "remove":
  80. for(; i < event.events.length; i++)
  81. {
  82. events[event.events[i]] = false;
  83. }
  84. break;
  85. default:
  86. console.error("Unknown action for event.");
  87. }
  88. }
  89. /**
  90. * Sends message to the external application.
  91. * @param object
  92. */
  93. function sendMessage(object) {
  94. window.parent.postMessage(JSON.stringify(object), "*");
  95. }
  96. /**
  97. * Processes a message event from the external application
  98. * @param event the message event
  99. */
  100. function processMessage(event)
  101. {
  102. var message;
  103. try {
  104. message = JSON.parse(event.data);
  105. } catch (e) {}
  106. if(!message.type)
  107. return;
  108. switch (message.type)
  109. {
  110. case "command":
  111. processCommand(message);
  112. break;
  113. case "event":
  114. processEvent(message);
  115. break;
  116. default:
  117. console.error("Unknown type of the message");
  118. return;
  119. }
  120. }
  121. var API = {
  122. /**
  123. * Check whether the API should be enabled or not.
  124. * @returns {boolean}
  125. */
  126. isEnabled: function () {
  127. var hash = location.hash;
  128. if(hash && hash.indexOf("external") > -1 && window.postMessage)
  129. return true;
  130. return false;
  131. },
  132. /**
  133. * Initializes the APIConnector. Setups message event listeners that will
  134. * receive information from external applications that embed Jitsi Meet.
  135. * It also sends a message to the external application that APIConnector
  136. * is initialized.
  137. */
  138. init: function () {
  139. if (window.addEventListener)
  140. {
  141. window.addEventListener('message',
  142. processMessage, false);
  143. }
  144. else
  145. {
  146. window.attachEvent('onmessage', processMessage);
  147. }
  148. sendMessage({type: "system", loaded: true});
  149. },
  150. /**
  151. * Checks whether the event is enabled ot not.
  152. * @param name the name of the event.
  153. * @returns {*}
  154. */
  155. isEventEnabled: function (name) {
  156. return events[name];
  157. },
  158. /**
  159. * Sends event object to the external application that has been subscribed
  160. * for that event.
  161. * @param name the name event
  162. * @param object data associated with the event
  163. */
  164. triggerEvent: function (name, object) {
  165. if(this.isEnabled() && this.isEventEnabled(name))
  166. sendMessage({
  167. type: "event", action: "result", event: name, result: object});
  168. },
  169. /**
  170. * Removes the listeners.
  171. */
  172. dispose: function () {
  173. if(window.removeEventListener)
  174. {
  175. window.removeEventListener("message",
  176. processMessage, false);
  177. }
  178. else
  179. {
  180. window.detachEvent('onmessage', processMessage);
  181. }
  182. }
  183. };
  184. module.exports = API;