코드
const usernameElement = document.querySelector('.username.d-none.d-sm-inline');
const usernameText = usernameElement.textContent.trim(); // 공백 제거
let dataFilterValues = {};
var numOfRequests = 0;
var numOfStyle = 0;
function setLinkStyle(link, post, comment) {
console.log('P :' + post + ' C : ' + comment);
if (post === 0 && comment === 0) {
link.style.fontWeight = 'bold';
link.style.color = 'red';
link.style.textDecoration = 'line-through';
link.textContent += ' [삭제된 계정]';
} else {
link.style.color = (post <= 5 || comment <= 5) ? 'red' :
(post <= 10 || comment <= 10) ? 'orange' :
(post <= 14 || comment <= 14) ? 'lightgreen' :
'';
link.textContent += (post < 15 || comment < 15) ? ` [최근 글: ${post} | 댓글: ${comment}]` : '';
}
numOfStyle++;
}
async function fetchDataAndSetStyle(link) {
const targetURL = link.href;
const dataFilterValue = link.getAttribute('data-filter').trim();
numOfRequests++;
if (usernameText !== dataFilterValue && !dataFilterValues[dataFilterValue]) {
dataFilterValues[dataFilterValue] = { post: 0, comment: 0 };
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: targetURL,
onload: function (response) {
const htmlData = response.responseText;
const parser = new DOMParser();
const doc = parser.parseFromString(htmlData, 'text/html');
if (doc.querySelectorAll('.error-code').length >= 1) {
console.log("DEL ACC");
} else {
const cardBlockElement = doc.querySelector('.card-block');
const childNodes = cardBlockElement.childNodes;
let postCount = 0;
let commentCount = 0;
let flag = 0;
for (const node of childNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.className === "clearfix") {
flag += 1;
}
if (node.className === "user-recent" && flag === 0) {
postCount += 1;
} else if (node.className === "user-recent" && flag === 1) {
commentCount += 1;
}
}
}
dataFilterValues[dataFilterValue] = { post: postCount, comment: commentCount };
}
resolve();
}
});
});
}
}
(async () => {
const promises = [];
document.querySelectorAll('.info-row .user-info a').forEach(link => {
promises.push(fetchDataAndSetStyle(link));
});
await Promise.all(promises);
document.querySelectorAll('.info-row .user-info a').forEach(secondLink => {
const secondDataFilterValue = secondLink.getAttribute('data-filter').trim();
if (dataFilterValues[secondDataFilterValue]) {
const { post, comment } = dataFilterValues[secondDataFilterValue];
setLinkStyle(secondLink, post, comment);
}
});
console.log('Req : ' + numOfRequests + ' Sty : ' + numOfStyle);
console.log(dataFilterValues);
})();
스크립트 다운로드 링크
댓글에도 뜨면 좋겠다해서 수정해옴
원작자한테 허락받으려고했는데 원작자 글이 날라가서 그냥 올림
문제되면 삭제할듯?
이전글
2.0
코드 개선 및 중복 항목 처리 추가?
2.1
비동기식으로 변경