I finally found it out. You can access current comment settings by using
wp.data.select('core/editor').getEditedPostAttribute('comment_status')
So the complete solution is:
function isCommentsEnabledInBlockEditor() {
return wp.data.select('core/editor').getEditedPostAttribute('comment_status') === 'open';
}
function registerPublishBtnHandler() {
const publishBtnArray = document.getElementsByClassName("editor-post-publish-button__button");
if (publishBtnArray && publishBtnArray.length > 0) {
const publishBtn = publishBtnArray[0];
if (publishBtn) {
publishBtn.addEventListener("click", function (e) {
if (!isCommentsEnabledInBlockEditor()) {
if (!confirm("Comments are disabled. Still publish?")) {
e.preventDefault();
e.stopPropagation();
}
}
});
}
}
}
window.addEventListener('load', function () {
registerPublishBtnHandler();
});