const watchReply = function() {
// Check addComment._Jetpack_moveForm to make sure we don't monkey-patch twice.
if ( 'undefined' !== typeof addComment && ! addComment._Jetpack_moveForm ) {
// Cache the Core function.
addComment._Jetpack_moveForm = addComment.moveForm;
const commentParent = document.getElementById( 'comment_parent' );
const cancel = document.getElementById( 'cancel-comment-reply-link' );
function tellFrameNewParent ( commentParentValue ) {
const url = new URL( iframe.src );
if ( commentParentValue ) {
url.searchParams.set( 'replytocom', commentParentValue )
} else {
url.searchParams.delete( 'replytocom' );
}
if( iframe.src !== url.href ) {
iframe.src = url.href;
}
};
cancel.addEventListener( 'click', function () {
tellFrameNewParent( false );
} );
addComment.moveForm = function ( _, parentId ) {
tellFrameNewParent( parentId );
return addComment._Jetpack_moveForm.apply( null, arguments );
};
}
}
document.addEventListener( 'DOMContentLoaded', watchReply );
// In WP 6.4+, the script is loaded asynchronously, so we need to wait for it to load before we monkey-patch the functions it introduces.
document.querySelector('#comment-reply-js')?.addEventListener( 'load', watchReply );
const commentIframes = document.getElementsByClassName('jetpack_remote_comment');
window.addEventListener('message', function(event) {
if (event.origin !== 'https://jetpack.wordpress.com') {
return;
}
if (!event?.data?.iframeUniqueId && !event?.data?.height) {
return;
}
const eventDataUniqueId = event.data.iframeUniqueId;
// Change height for the matching comment iframe
for (let i = 0; i < commentIframes.length; i++) {
const iframe = commentIframes[i];
const url = new URL(iframe.src);
const iframeUniqueIdParam = url.searchParams.get('iframe_unique_id');
if (iframeUniqueIdParam == event.data.iframeUniqueId) {
iframe.style.height = event.data.height + 'px';
return;
}
}
});
})();