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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. function setupListeners() {
  122. xmpp.addListener(XMPPEvents.MUC_ENTER, function (from) {
  123. API.triggerEvent("participantJoined", {jid: from});
  124. });
  125. xmpp.addListener(XMPPEvents.MESSAGE_RECEIVED, function (from, nick, txt, myjid) {
  126. if (from != myjid)
  127. API.triggerEvent("incomingMessage",
  128. {"from": from, "nick": nick, "message": txt});
  129. });
  130. xmpp.addListener(XMPPEvents.MUC_LEFT, function (jid) {
  131. API.triggerEvent("participantLeft", {jid: jid});
  132. });
  133. }
  134. var API = {
  135. /**
  136. * Check whether the API should be enabled or not.
  137. * @returns {boolean}
  138. */
  139. isEnabled: function () {
  140. var hash = location.hash;
  141. if(hash && hash.indexOf("external") > -1 && window.postMessage)
  142. return true;
  143. return false;
  144. },
  145. /**
  146. * Initializes the APIConnector. Setups message event listeners that will
  147. * receive information from external applications that embed Jitsi Meet.
  148. * It also sends a message to the external application that APIConnector
  149. * is initialized.
  150. */
  151. init: function () {
  152. if (window.addEventListener)
  153. {
  154. window.addEventListener('message',
  155. processMessage, false);
  156. }
  157. else
  158. {
  159. window.attachEvent('onmessage', processMessage);
  160. }
  161. sendMessage({type: "system", loaded: true});
  162. setupListeners();
  163. },
  164. /**
  165. * Checks whether the event is enabled ot not.
  166. * @param name the name of the event.
  167. * @returns {*}
  168. */
  169. isEventEnabled: function (name) {
  170. return events[name];
  171. },
  172. /**
  173. * Sends event object to the external application that has been subscribed
  174. * for that event.
  175. * @param name the name event
  176. * @param object data associated with the event
  177. */
  178. triggerEvent: function (name, object) {
  179. if(this.isEnabled() && this.isEventEnabled(name))
  180. sendMessage({
  181. type: "event", action: "result", event: name, result: object});
  182. },
  183. /**
  184. * Removes the listeners.
  185. */
  186. dispose: function () {
  187. if(window.removeEventListener)
  188. {
  189. window.removeEventListener("message",
  190. processMessage, false);
  191. }
  192. else
  193. {
  194. window.detachEvent('onmessage', processMessage);
  195. }
  196. }
  197. };
  198. module.exports = API;