// ==UserScript== // @name FilterComments // @namespace http://tampermonkey.net/ // @version 0.1 // @description xhs // @author fbjyet // @license MIT // @match https://www.xiaohongshu.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=xiaohongshu.com // @grant none // @downloadURL none // ==/UserScript== var FILTER_LOCATION = '广东'; function runInWaitRender(callback) { setTimeout(() => { callback(); }, 1500); } function createObserver(observedSelector, targetSelector, callback) { const wrappedCallback = function (mutationsList) { for (const mutation of mutationsList) { if (mutation.addedNodes) { for (let i = 0; i < mutation.addedNodes.length; i++) { const addedNode = mutation.addedNodes[i]; if (addedNode.matches && addedNode.matches(targetSelector)) { callback(addedNode); } } } } }; const observer = new MutationObserver(wrappedCallback); const observedElement = document.querySelector(observedSelector); observer.observe(observedElement, { childList: true, subtree: true }); } function filterComments(parentElement) { const comments = parentElement.querySelectorAll('.comment-inner-container'); comments.forEach(function (comment) { const locationElement = comment.querySelector('.location'); if (locationElement.textContent.trim() !== FILTER_LOCATION) { console.log(comment); comment.style.display = 'none'; } }); } function main() { createObserver( '.with-side-bar.main-content', '.note-detail-mask', (noteDetailElement) => { runInWaitRender(() => { // first time render filterComments(noteDetailElement); // monitoring loading more comments createObserver( '.comments-container > .list-container', '.parent-comment', (parentCommentElement) => { filterComments(parentCommentElement); }, ); }); }, ); } (function () { 'use strict'; runInWaitRender(() => { main(); }); })();