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.

47 lines
1023 B

  1. // Copyright (c) 2020 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. let widgetId = null
  7. window.onmessage = event => {
  8. if (!window.parent || !event.data) {
  9. return
  10. }
  11. const request = event.data
  12. if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") {
  13. return
  14. }
  15. if (widgetId) {
  16. if (widgetId !== request.widgetId) {
  17. return
  18. }
  19. } else {
  20. widgetId = request.widgetId
  21. }
  22. window.parent.postMessage({
  23. ...request,
  24. response: request.action === "capabilities" ? {
  25. capabilities: ["m.sticker"],
  26. } : {
  27. error: { message: "Action not supported" },
  28. },
  29. }, event.origin)
  30. }
  31. export function sendSticker(content) {
  32. window.parent.postMessage({
  33. api: "fromWidget",
  34. action: "m.sticker",
  35. requestId: `sticker-${Date.now()}`,
  36. widgetId,
  37. data: {
  38. name: content.body,
  39. content,
  40. },
  41. }, "*")
  42. }