-1

ここで何を話しているのかわかりません。

一部のページではそれらをフィルタリングし、他の Youtube コメントなどは機能しません。これらのサイトで機能させるには、どのコードを変更する必要がありますか?

// ==UserScript==
// @name        profanity_filter
// @namespace   localhost
// @description Profanity filter
// @include     *
// @version     1
// @grant       none
// ==/UserScript==

function recursiveFindTextNodes(ele) {
  var result = [];
  result = findTextNodes(ele,result);
  return result;
}

function findTextNodes(current,result) {
  for(var i = 0; i < current.childNodes.length; i++) {
    var child = current.childNodes[i];
    if(child.nodeType == 3) {
      result.push(child);
    }
    else {
      result = findTextNodes(child,result);
    }
  }
  return result;
}

var l = recursiveFindTextNodes(document.body);

for(var i = 0; i < l.length; i++) {
  var t = l[i].nodeValue;
  t = t.replace(/badword1|badword2|badword3/gi, "****");
  t = t.replace(/badword4/gi, "******");
  t = t.replace(/badword5|badword6|badword7/gi, "*****");
  t = t.replace(/badword8/gi, "******");
  l[i].nodeValue = t;
}

* コード内の冒とく的な表現を悪い言葉に置き換えました

4

1 に答える 1

0

Youtube のコメントは、ページが読み込まれてからかなり長い時間が経過してから非同期に読み込まれるため (デフォルトではユーザー スクリプトはイベントで実行されます)、コメント コンテナーまたはまたはのセレクターを使用して、 waitForKeyElementsDOMContentLoadedのコールバック関数としてコードをラップする必要があります。MutationObserversetInterval

replaceNodes(); // process the page
waitForKeyElements('.comment-text-content', replaceNodes);

function replaceNodes() {
    ..............
    ..............
}

waitForKeyElementssetIntervalの代わりに使用:

replaceNodes(); // process the page
var interval = setInterval(function() {
    if (document.querySelector('.comment-text-content')) {
        clearInterval(interval);
        replaceNodes();
    }
}, 100);

function replaceNodes() {
    ..............
    ..............
}

PS やみくもに値をノードに割り当てないでください。レイアウトの再計算を避けるために、値が変更されているかどうかを最初に確認してください。

if (l[i].nodeValue != t) {
    l[i].nodeValue = t;
}
于 2015-10-20T06:28:54.073 に答える