白屏骨架屏监控

2020-12-19  本文已影响0人  Roseska

调研过程

  1. 发现了几个有意思的api
  1. 前端监控几个方法

我的思路:

以下是判断白屏、骨架屏方法:

注意点:

  1. 有的页面顶部有走马灯,这样可能会影响骨架屏的判断
  2. 有的插件引入后会中带有默认文字,这样会影响骨架屏的判断,可以动态传入页面id来解决
let root = typeof self == 'object' && self.self === self && self || typeof global == 'object' && global.global === global && global || window || {};
setTimeout(() => {
  skeletonscreen()
}, 5000);

//  递归函数,获取页面dom数组
function countNodes (node, deepArr) {
  node = node || document.body
  //  计算自身
  let arr = deepArr ? deepArr : []
  //  判断是否存在子节点
  if (node.hasChildNodes()) {
    //  获取子节点
    var cnodes = node.childNodes;
    //  对子节点进行递归统计
    for (var i = 0; i < cnodes.length; i++) {
      countNodes(cnodes.item(i), arr)
    }
  } else {
    arr.push(node)
  }
  return arr;
}

//  统计body的节点
let nodesArr = countNodes()
console.log('nodesArr', nodesArr)

let isNoTxt = nodesArr.some((item, index) => {
  return item.nodeValue && item.nodeValue.replace(/\s*/g, "")
})


let seletonseletonCoordinates = []

//计算每个色块的面积
const linkSum = (i, j, num) => {
  //走过的路就置0
  coordinates[i][j] = 0;

  num++;
  //向上
  if ((i + 1 < h) && coordinates[i + 1][j] == 1) {
    num = linkSum(i + 1, j, num);
  }
  //向右
  if ((j + 1 < w) && coordinates[i][j + 1] == 1) {
    num = linkSum(i, j + 1, num);
  }
  //向下
  if ((i - 1 >= 0) && coordinates[i - 1][j] == 1) {
    num = linkSum(i - 1, j, num);
  }
  //向左
  if ((j - 1 >= 0) && coordinates[i][j - 1] == 1) {
    num = linkSum(i, j - 1, num);
  }
  return num;
}


//计算总块数和灰色区域总面积
const getCountAndArea = () => {
  let sum = [];
  let count = 0;
  for (let i = 0; i < h; i++) {
    for (let j = 0; j < w; j++) {
      //连续1的个数
      if (seletonCoordinates[i][j] == 1) {
        let buf = 0;
        buf = linkSum(i, j, buf);
        count++;
        sum.push({
          index: count,
          area: buf
        });
      }
    }
  }
  return {
    count,
    sum
  };
}

//根据颜色判断
function skeletonscreen () {
  if (root.html2canvas) {
    html2canvas(document.body, {
      backgroundColor: null,   //设置截图的背景色
      useCORS: true, // 如果截图的内容里有图片,可能会有跨域的情况,加上这个参数,解决文件跨域问题
      allowTaint: false, //允许跨域(图片跨域相关)
      taintTest: true, //是否在渲染前测试图片
    }).then((canvas) => {
      let ctx = canvas.getContext("2d");
      const ratio = root.devicePixelRatio
      const width = document.body.clientWidth * ratio
      const height = document.body.clientHeight * ratio / 3
      let img = ctx.getImageData(0, 0, width, height);
      let sumPx = 0
      const imgdata = img.data
      let r, g, b
      let x = 0, y = 0
      //设置二维数组
      for (let i = 0; i < height; i++) {
        seletonCoordinates[i] = []
      }
      for (var i = 0; i < imgdata.length; i += 4) {
        r = imgdata[i];
        g = imgdata[i + 1];
        b = imgdata[i + 2];
        if (r === 255 && g === 255 && b === 255) {
          sumPx += 1
          seletonCoordinates[y][x] = 0
        } else {
          seletonCoordinates[y][x] = 1
        }
        x++
        if (x > width) {
          x = 0
          y++
        }
      }
   
      let rst = getCountAndArea();
      if (rst.count > 3 && !isNoTxt) {
        console.log('页面骨架屏')
      }
    })
  } 
}
上一篇下一篇

猜你喜欢

热点阅读