docbrown/app/javascript/contentDisplayPolicy/hideBlockedContent.js
rhymes f9506affb5
Use faster JS selection methods (#11409)
* Add JS tips section to frontend documentation

* Replace document.getElementsByTagName('body') with document.body

* Replace querySelectorAll with faster selecting methods where appropriate

* Replace querySelector with faster selecting methods where appropriate

* Fix typo

* Fix forEach and getElementsByClassName

* Change querySelector* to faster methods in erb files

* Change querySelector* to faster methods in ruby files

* Fix runkit tag

* Various fixes

* Update app/assets/javascripts/initializers/initializeEllipsisMenu.js

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

* Update app/assets/javascripts/utilities/slideSidebar.js

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

* Commenting out flaky spec

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
2020-11-16 17:35:50 +01:00

29 lines
983 B
JavaScript

/* global userData */
export default function hideBlockedContent() {
const contentUserElements = Array.from(
document.querySelectorAll('div[data-content-user-id]'),
);
const user = userData(); //global var
const blockedUserIds = user ? user.blocked_user_ids : [];
const divsToHide = contentUserElements.filter((div) => {
const { contentUserId } = div.dataset;
return blockedUserIds.includes(parseInt(contentUserId, 10));
});
divsToHide.forEach((div) => {
if (div.className.includes('single-article')) {
div.style.display = 'none';
} else if (div.className.includes('single-comment-node')) {
const divInnerComment = div.getElementsByClassName('inner-comment')[0];
divInnerComment.innerHTML = `
<div class="body " style="padding-bottom:32px;opacity:0.3;user-select:none;cursor:default">
[blocked content]
</div>
`;
}
});
}
window.addEventListener('checkBlockedContent', hideBlockedContent);