FilterComments

xhs

目前為 2023-10-30 提交的版本,檢視 最新版本

// ==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
// ==/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();
  });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址