html 中拦截 js 的样式更改

发布时间 2023-06-14 10:44:08作者: 空明流光
比如拦截 html 标签的 font-size 样式的更改:
...
<body>
  <script>
    var observer = new MutationObserver(function (mutations) {
      mutations.forEach(function (mutation) {
        if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
          var target = mutation.target;
          var oldStyle = mutation.oldValue;
          var newStyle = target.getAttribute('style');
          if (target.tagName == "HTML" && newStyle.includes("font-size"))
            debugger
        }
      });
    });

    var targetNode = document.documentElement;
    var config = { attributes: true, attributeOldValue: true, attributeFilter: ['style'] };
    observer.observe(targetNode, config);
  </script>
...