fix: update courseware iframe check for learning mfe (#22974)
document.referrer may include a trailing slash or path which is now handled by the new condition
This commit is contained in:
@@ -50,13 +50,12 @@ ${static.get_page_title_breadcrumbs(course_name())}
|
||||
// If this chromeless view is in an iframe in the learning microfrontend app
|
||||
// then add a base tag in the head (of the iframe document) to force links
|
||||
// in this iframe to navigate the parent window.
|
||||
if (window !== window.parent) {
|
||||
var learningAppReferrer = "${settings.LEARNING_MICROFRONTEND_URL | n, js_escaped_string}";
|
||||
if (document.referrer === learningAppReferrer) {
|
||||
var baseElement = document.createElement('base');
|
||||
baseElement.setAttribute('target', '_parent');
|
||||
document.head.appendChild(baseElement);
|
||||
}
|
||||
var learningAppUrl = "${settings.LEARNING_MICROFRONTEND_URL | n, js_escaped_string}";
|
||||
var parentIsLearningApp = document.referrer.indexOf(learningAppUrl) === 0;
|
||||
if (window !== window.parent && parentIsLearningApp) {
|
||||
var baseElement = document.createElement('base');
|
||||
baseElement.setAttribute('target', '_parent');
|
||||
document.head.appendChild(baseElement);
|
||||
}
|
||||
</script>
|
||||
</%block>
|
||||
@@ -112,44 +111,43 @@ ${HTML(fragment.foot_html())}
|
||||
// If this view is rendered in an iframe within the learning microfrontend app
|
||||
// it will report the height of its contents to the parent window when the
|
||||
// document loads, window resizes, or DOM mutates.
|
||||
if (window !== window.parent) {
|
||||
var learningAppReferrer = "${settings.LEARNING_MICROFRONTEND_URL | n, js_escaped_string}";
|
||||
if (document.referrer === learningAppReferrer) {
|
||||
var learningAppUrl = "${settings.LEARNING_MICROFRONTEND_URL | n, js_escaped_string}";
|
||||
var parentIsLearningApp = document.referrer.indexOf(learningAppUrl) === 0;
|
||||
|
||||
var lastHeight = window.offsetHeight;
|
||||
var lastWidth = window.offsetWidth;
|
||||
var contentElement = document.getElementById('content');
|
||||
if (window !== window.parent && parentIsLearningApp) {
|
||||
var lastHeight = window.offsetHeight;
|
||||
var lastWidth = window.offsetWidth;
|
||||
var contentElement = document.getElementById('content');
|
||||
|
||||
function dispatchResizeMessage() {
|
||||
var newHeight = contentElement.offsetHeight;
|
||||
var newWidth = contentElement.offsetWidth;
|
||||
function dispatchResizeMessage() {
|
||||
var newHeight = contentElement.offsetHeight;
|
||||
var newWidth = contentElement.offsetWidth;
|
||||
|
||||
if (newWidth === lastWidth && newHeight === lastHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.parent.postMessage({
|
||||
type: 'plugin.resize',
|
||||
payload: {
|
||||
width: newWidth,
|
||||
height: newHeight,
|
||||
}
|
||||
}, document.referrer
|
||||
);
|
||||
|
||||
lastHeight = newHeight;
|
||||
lastWidth = newWidth;
|
||||
if (newWidth === lastWidth && newHeight === lastHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create an observer instance linked to the callback function
|
||||
const observer = new MutationObserver(dispatchResizeMessage);
|
||||
window.parent.postMessage({
|
||||
type: 'plugin.resize',
|
||||
payload: {
|
||||
width: newWidth,
|
||||
height: newHeight,
|
||||
}
|
||||
}, document.referrer
|
||||
);
|
||||
|
||||
// Start observing the target node for configured mutations
|
||||
observer.observe(document.body, { attributes: true, childList: true, subtree: true });
|
||||
|
||||
window.addEventListener('load', dispatchResizeMessage);
|
||||
window.addEventListener('resize', dispatchResizeMessage);
|
||||
lastHeight = newHeight;
|
||||
lastWidth = newWidth;
|
||||
}
|
||||
|
||||
// Create an observer instance linked to the callback function
|
||||
const observer = new MutationObserver(dispatchResizeMessage);
|
||||
|
||||
// Start observing the target node for configured mutations
|
||||
observer.observe(document.body, { attributes: true, childList: true, subtree: true });
|
||||
|
||||
window.addEventListener('load', dispatchResizeMessage);
|
||||
window.addEventListener('resize', dispatchResizeMessage);
|
||||
}
|
||||
}());
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user