servo/tests/wpt/web-platform-tests/feature-policy/experimental-features/resources/common.js

34 lines
989 B
JavaScript

const url_base = "/feature-policy/experimental-features/resources/";
window.messageResponseCallback = null;
function setFeatureState(iframe, feature, origins) {
iframe.setAttribute("allow", `${feature} ${origins};`);
}
// Returns a promise which is resolved when the <iframe> is navigated to |url|
// and "load" handler has been called.
function loadUrlInIframe(iframe, url) {
return new Promise((resolve) => {
iframe.addEventListener("load", resolve);
iframe.src = url;
});
}
// Posts |message| to |target| and resolves the promise with the response coming
// back from |target|.
function sendMessageAndGetResponse(target, message) {
return new Promise((resolve) => {
window.messageResponseCallback = resolve;
target.postMessage(message, "*");
});
}
function onMessage(e) {
if (window.messageResponseCallback) {
window.messageResponseCallback(e.data);
window.messageResponseCallback = null;
}
}
window.addEventListener("message", onMessage);