B站愚人节去绿字

  

function youzhi(exp) {
  if (!exp && typeof exp != "undefined" && exp != 0) {
    //alert(“is null”);
    return false;
  }
  if (typeof exp == "undefined") {
    // alert("undefined");
    return false;
  }

  return true;
}

function rollhtml() {
  console.log("我被执行了" + Date().toLocaleString());
  var map = {};
  var map_num = 0;
  //采用递归调用的方法,比较方便和简单。
  function fds(node) {
    if (node.nodeType === 1) {
      map_num++;
      // node.id = "id" + map_num.toLocaleString(); //给节点设置ID
      let x9 = " classid" + map_num.toLocaleString(); //给节点增加class

      if (node.className.toLocaleString().indexOf(x9) == -1) {
        node.className = node.className + x9;
        //  console.log("已经存在class",x9)
      }
    }
    //获取该元素节点的所有子节点
    var children = node.childNodes;
    for (var i = 0; i < children.length; i++) {
      //递归调用
      fds(children[i]);
    }
  }

  fds(document);

  var nodes_arry = ["A", "DIV", "SPAN", "a", "div", "span"];

  for (i = 1; i <= map_num; i++) {
    let tempid1 = "classid" + i.toLocaleString();
    //let el = document.getElementById(tempid1);
    let el = document.getElementsByClassName(tempid1)[0];
    if (youzhi(el)) {
      // console.log(el)

      let node_temp = el.nodeName.toLocaleString();
      if (youzhi(node_temp)) {
        // console.log(node_temp)
        if (nodes_arry.includes(node_temp, 0)) {
          el.style.color = "black";
        }
      }
    }
  }
}
//document.onmousewheel = rollhtml  一直执行CPU占用太高
rollhtml();
var canRun = true;
document.onscroll = function () {
  if (!canRun) {
    // 判断是否已空闲,如果在执行中,则直接return
    return;
  }

  canRun = false;
  setTimeout(function () {
    console.log("函数节流");
    rollhtml();
    canRun = true;
  }, 1000);
};

 

--  the end --

 

posted @ 2022-04-01 21:04  wsh3166Sir  阅读(31)  评论(3编辑  收藏  举报