mirror of
https://github.com/element-hq/element-web
synced 2024-11-25 02:35:48 +03:00
Return a response to widget postMessage request (inline with scalarMessaging API).
This commit is contained in:
parent
81fdccb109
commit
5239729e8e
1 changed files with 36 additions and 0 deletions
|
@ -35,6 +35,9 @@ The complete request object is returned to the caller with an additional "respon
|
||||||
}
|
}
|
||||||
|
|
||||||
The "action" determines the format of the request and response. All actions can return an error response.
|
The "action" determines the format of the request and response. All actions can return an error response.
|
||||||
|
|
||||||
|
A success response is an object with zero or more keys.
|
||||||
|
|
||||||
An error response is a "response" object which consists of a sole "error" key to indicate an error.
|
An error response is a "response" object which consists of a sole "error" key to indicate an error.
|
||||||
They look like:
|
They look like:
|
||||||
{
|
{
|
||||||
|
@ -101,8 +104,10 @@ function onMessage(event) {
|
||||||
action: 'widget_content_loaded',
|
action: 'widget_content_loaded',
|
||||||
widgetId: widgetId,
|
widgetId: widgetId,
|
||||||
});
|
});
|
||||||
|
sendResponse(event, {success: true});
|
||||||
} else {
|
} else {
|
||||||
console.warn("Widget postMessage event unhandled");
|
console.warn("Widget postMessage event unhandled");
|
||||||
|
sendError(event, {message: "The postMessage was unhandled"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +130,37 @@ function trustedEndpoint(origin) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a postmessage response to a postMessage request
|
||||||
|
* @param {Event} event The original postMessage request event
|
||||||
|
* @param {Object} res Response data
|
||||||
|
*/
|
||||||
|
function sendResponse(event, res) {
|
||||||
|
const data = JSON.parse(JSON.stringify(event.data));
|
||||||
|
data.response = res;
|
||||||
|
event.source.postMessage(data, event.origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send an error response to a postMessage request
|
||||||
|
* @param {Event} event The original postMessage request event
|
||||||
|
* @param {string} msg Error message
|
||||||
|
* @param {Error} nestedError Nested error event (optional)
|
||||||
|
*/
|
||||||
|
function sendError(event, msg, nestedError) {
|
||||||
|
console.error("Action:" + event.data.action + " failed with message: " + msg);
|
||||||
|
const data = JSON.parse(JSON.stringify(event.data));
|
||||||
|
data.response = {
|
||||||
|
error: {
|
||||||
|
message: msg,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (nestedError) {
|
||||||
|
data.response.error._error = nestedError;
|
||||||
|
}
|
||||||
|
event.source.postMessage(data, event.origin);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents mapping of widget instance to URLs for trusted postMessage communication.
|
* Represents mapping of widget instance to URLs for trusted postMessage communication.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue