一段监视 DOM 的神奇代码

访客4年前黑客资讯901

通过使用此模块,只需将鼠标悬停在浏览器中,即可快速查看DOM米素的属性。基本上它是一个即时检查器。


将鼠标悬停在 DOM 米素上会显示其属性!


自己实验一下

复制下面的整个代码块,并将其粘贴到浏览器 Web 控制台中。现在将鼠标悬停在你正在浏览的任何网页上。 看到了什么?

(function SpyOn() {

  const _id = 'spyon-container',
        _posBuffer = 3;

  function init() {
    document.body.addEventListener('mousemove', glide);
    document.body.addEventListener('mouseover', show);
    document.body.addEventListener('mouseleave', hide);
  }

  function hide(e) {
    document.getElementById(_id).style.display = 'none';
  }

  function show(e) {
    const spyContainer = document.getElementById(_id);
    if (!spyContainer) {
      create();
      return;
    }
    if (spyContainer.style.display !== 'block') {
      spyContainer.style.display = 'block';
    }
  }

  function glide(e) {
    const spyContainer = document.getElementById(_id);
    if (!spyContainer) {
      create();
      return;
    }
    const left = e.clientX + getScrollPos().left + _posBuffer;
    const top = e.clientY + getScrollPos().top + _posBuffer;
    spyContainer.innerHTML = showAttributes(e.target);
    if (left + spyContainer.offsetWidth > window.innerWidth) {
      spyContainer.style.left = left - spyContainer.offsetWidth + 'px';
    } else {
      spyContainer.style.left = left + 'px';
    }
    spyContainer.style.top = top + 'px';
  }

  function getScrollPos() {
    const ieEdge = document.all ? false : true;
    if (!ieEdge) {
      return {
        left : document.body.scrollLeft,
        top : document.body.scrollTop
      };
    } else {
      return {
        left : document.documentElement.scrollLeft,
        top : document.documentElement.scrollTop
      };
    }
  }

  function showAttributes(el) {
    const nodeName = `<span>${el.nodeName.toLowerCase()}</span><br/>`;
    const attrArr = Array.from(el.attributes);
    const attributes = attrArr.reduce((attrs, attr) => {
      attrs += `<span>${attr.nodeName}</span>="${attr.nodeValue}"<br/>`;
      return attrs;
    }, '');
    return nodeName + attributes;
  }

  function create() {
    const div = document.createElement('div');
    div.id = _id;
    div.setAttribute('style', `
      position: absolute;
      left: 0;
      top: 0;
      width: auto;
      height: auto;
      padding: 10px;
      box-sizing: border-box;
      color: #fff;
      background-color: #444;
      z-index: 100000;
      font-size: 12px;
      border-radius: 5px;
      line-height: 20px;
      max-width: 45%;
      `
    );
    document.body.appendChild(div);
  }

  init();

})();


它是怎么运作的

此模块以IIFE的形式实现。这样只要需要一些 DOM 监视辅助,就可以将代码复制并粘贴到 Web 控制台中。将 div 插入到文档的正文中,并在正文上启用鼠标事宜侦听器。从目的米素中检索属性,将其简化为单个字符串,最后在工具提醒中显示。


用例

  1. 辅助解决UI错误
  2. 确保你所应用的 DOM 米素能够按预期事情(好比点击获得准确的类,等等)
  3. 领会一个 Web 应用的结构


你可以从这段代码中学到什么

  1. 若何使用 Vanilla *** 实现工具提醒模块
  2. 若何剖析 DOM 工具的属性
  3. 若何找到鼠标 X 和 Y 的位置
  4. 若何获取文档的转动位置
  5. 领会差别浏览器的行为方式 —— Edge vs. Chrome vs. Safari


开源

你可以在这里找到源代码,希望你能做得更好!也许你不希望将其作为 IIFE 来实现,或者是去显示其他数据。

原文:https://dev.to/eddieaich/spy-on-the-dom-3d47
作者:Eddie Aich,翻译:疯狂的手艺宅
思源资源网:分类流动

1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入

相关文章

CSS中auto是什么意思?

auto 你可以理解为一种 自动/自顺应 的观点,好比现在项目需要一个宽度为960px的整体结构居中,你可使用margin:0 auto;来实现。 无论用户浏览器宽度为若干。 css中的auto是自动...

手机摄像头被入侵特征(如何入侵手机摄像头)

远程控制别人的手机是可以的,下一个远程监控软件就可以了,但是把别人的手机摄像头变成自己的好像不太可能,你可以下载天眼远控软件试试 自己手机摄像头有没有被黑客侵入是无法判断的;原因是因为黑客研究智取计...

云顶之弈10.15阵容推荐 压缩饼干星系阵容运营思路分享

云顶之弈10.15阵容推荐 压缩饼干星系阵容运营思路分享

云顶之弈在10.14到10.15版本更新时,不仅出来了爆金币星系,现在同时也出来了压缩饼干星系。基本功能就是整个云顶之弈棋盘禁用第一排和最后一排的站位格子。顾名思义,也就是说棋盘被缩小了,棋子活动的空...

护眼需知:越橘VS叶黄素,叶黄素哪个牌子好?

关于日常护眼,我们除了要做好必要的护眼措施,也需注意眼部营养的补充。说及补充眼部营养,不可不知越橘、叶黄素,那么,越橘与叶黄素,能够起到什么护眼作用呢? 越橘 越橘,是杜鹃花科植物,其果实外形跟蓝莓相...

可以查开房记录的软件 开房记录在哪里可以查询

英国黑客朱奈德·侯赛因和美国中央司令部官方推特遭受的网络袭击有关。英国前首相托尼·布莱尔也曾是侯赛因黑客行为的受害者。另一方面,美国军方亡羊补牢,已经加强了网络密码的安保措施。   英国黑客牵头?...

全国高端商务资源,三个办法预约国内空姐【吕雪】

全国高端商务资源,三个办法预约国内空姐【吕雪】 今天给大家分享的内容是“全国高端商务资源,三个办法预约国内空姐【吕雪】”,我是吕雪,来自汕尾市,今年28岁,作为职业:普通服务行业,我热爱我的职业:...