diff options
| author | Tulir Asokan <tulir@maunium.net> | 2020-09-05 12:16:27 +0300 |
|---|---|---|
| committer | Tulir Asokan <tulir@maunium.net> | 2020-09-05 12:16:27 +0300 |
| commit | 28c7d2f1a2113d3ea609373a345d23d35361d605 (patch) | |
| tree | 98c4f649d4a90a4dc5db6485e484510d4e15e870 /web/widget-api.js | |
| parent | 27fd49f6e581c8bdc6c05e3ef83bf68fdd472f6f (diff) | |
Split up spinner and widget API into files
Diffstat (limited to 'web/widget-api.js')
| -rw-r--r-- | web/widget-api.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/web/widget-api.js b/web/widget-api.js new file mode 100644 index 0000000..556537f --- /dev/null +++ b/web/widget-api.js @@ -0,0 +1,47 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +let widgetId = null + +window.onmessage = event => { + if (!window.parent || !event.data) { + return + } + + const request = event.data + if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") { + return + } + + if (widgetId) { + if (widgetId !== request.widgetId) { + return + } + } else { + widgetId = request.widgetId + } + + window.parent.postMessage({ + ...request, + response: request.action === "capabilities" ? { + capabilities: ["m.sticker"], + } : { + error: { message: "Action not supported" }, + }, + }, event.origin) +} + +export function sendSticker(content) { + window.parent.postMessage({ + api: "fromWidget", + action: "m.sticker", + requestId: `sticker-${Date.now()}`, + widgetId, + data: { + name: content.body, + content, + }, + }, "*") +} |
