Refactor this to make it check if its already block:
js
show() {
if (!this.deadReplyWidgetElement)
return;
this.deadReplyWidgetElement.style.display = 'block';
}
Assistant
You can refactor the show method to first check if the element is already set to block before attempting to change the style. Here's a possible refactor:
In this version, the method first checks if this.deadReplyWidgetElement is falsy or if its display style is already set to block. If either condition is true, it exits early without making any changes. If neither condition is true, it sets the display style to block.
User
cant it be simple? i think it looks like pure boilerplate
Assistant
Certainly! You can simplify the show method by removing unnecessary checks. If the main goal is just to show the element and you're okay with setting it to block regardless of its previous state, the code can be simplified like this:
js
show() {
if (this.deadReplyWidgetElement) {
this.deadReplyWidgetElement.style.display = 'block';
}
}
This concise version retains the check for the existence of this.deadReplyWidgetElement but does not check its current display state since it may be acceptable to set it to block regardless. This keeps the code clean while still performing the necessary operation.
User
Assistant
Refactor this code:
js
class DeadReplyWidget {
deadReplyWidgetHTML =
`<div class="qr" id="select-replies-window">
<div class="qr__header" id="select-replies-window-header">
Выделение постов
<span class="qr__close" id="qr-select-replies-window-close">X</span>
</div>
<div class="qr__body settings" id="select-replies-body"><div class="settings__prefs" id="select-replies-prefs">
<div class="settings__row">
<span class="settings__label">
<input type="checkbox" id="select-replies-chains" class="settings__checkbox"/><label>Выделить ветки</label>
</span>
</div>
<div class="settings__row">
<span class="settings__label">
<label>Лимит постов </label><input type="text" placeholder="Лимит" id="select-replies-limit" class="input" size="6"/>
</span>
</div>
</div>
</div>
<div class="select-replies-buttons qr__footer">
<hr/>
<input type="button" class="button" value="Выделить" id="select-replies-btn"/>
</div>
</div>`
deadReplyWidgetElement;
// Get whole chain by post
getReplyChain(posts, postNumber, result = new Set()) {
const replyPattern = new RegExp(`>>${ postNumber }(?!.*(\(OP\)|→))`, 'g');
// Check each post to see if it replies to the current post number
posts.forEach(post => {
const comment = post.comment;
const hasReply = comment?.match(replyPattern);
if (hasReply) {
result.add(post);
// Recursively search for replies to the found post numbers
this.getReplyChain(posts, post.num, result);
}
});
return [ ...result ];
};
// Get replies to removed posts
findDeadReplyPosts(posts, limit, checkWholeChain) {
const findReplies = (post) => {
const replyPattern = />>[0-9]+(?!.*(\(OP\)|→))/g;
const replies = post.comment?.match(replyPattern);
return replies?.map(reply => reply.replace('>>', ''));
};
// Get all posts that have dead replies
let deadReplyPosts = posts.filter(post => {
const replies = findReplies(post);
if (!replies)
return false;
// Check if any of the replies point to existing posts
const hasDeadReplies = replies.every(reply =>
!posts.some(some => some.num === parseInt(reply))
);
return hasDeadReplies;
});
if (limit)
deadReplyPosts = deadReplyPosts.slice(-limit);
if (checkWholeChain) {
const replies = [];
deadReplyPosts.forEach(post => {
replies.push( ...this.getReplyChain(posts, post.num) );
});
deadReplyPosts = deadReplyPosts.concat(replies);
}
return deadReplyPosts;
};
// Check replies to removed posts
checkDeadReplyPosts(posts, limit = 0, checkWholeChain = false) {
const deadReplyPosts = this.findDeadReplyPosts(posts, limit, checkWholeChain);
deadReplyPosts.forEach((post) => {
const postElement = $(`.post[data-num="${ post.num }"]`);
postElement.find('.turnmeoff').prop('checked', true);
postElement.addClass('post_type_highlight');
});
$alert(`Выделено ${ deadReplyPosts.length } постов.`);
};
create() {
// Create element
this.deadReplyWidgetElement = createElementFromHTML(this.deadReplyWidgetHTML).querySelector('#select-replies-window');
const selectRepliesButton = this.deadReplyWidgetElement.querySelector('#select-replies-btn');
const closeButton = this.deadReplyWidgetElement.querySelector('#qr-select-replies-window-close');
selectRepliesButton.onclick = async () => {
let limit = this.deadReplyWidgetElement.querySelector('#select-replies-limit').value;
let checkWholeChain = this.deadReplyWidgetElement.querySelector('#select-replies-chains').checked;
console.log(limit, checkWholeChain);
let thread = await fetch(location.href.replace('html', 'json'))
.then(response => response.json())
.then(response => response.threads[0].posts.slice(1));
this.checkDeadReplyPosts(thread, limit, checkWholeChain);
await window.selectedPostsNavFeature.updateSelectedNavCounter();
}
closeButton.onclick = () => this.hide();
mainBody.appendChild(this.deadReplyWidgetElement);
// Make draggable
draggable_qr('select-replies-window', 'center');
}
show() {
if (this.deadReplyWidgetElement)
this.deadReplyWidgetElement.style.display = 'block';
}
hide() {
if (this.deadReplyWidgetElement)
this.deadReplyWidgetElement.style.display = 'none';
}
}
Certainly! Here’s a refactored version of the DeadReplyWidget class that focuses on improving readability, reducing redundancy, and adhering to best practices. The changes involve:
Grouping related methods together.
Using concise arrow functions and avoiding unnecessary variable declarations where possible.
Extracting repeated DOM manipulation code into helper methods when applicable.