gpt4 book ai didi

javascript - 观察整个 HTML DOM 中的文本变化 - 如何做到这一点?

转载 作者:行者123 更新时间:2023-12-04 13:54:13 25 4
gpt4 key购买 nike

我想监视以下更改的完整 HTML:

  • 每当标签中的“文本”发生变化时
  • 每当添加标签中的“文本”时
  • 每当添加带有“文本”的新标签时

  • 所以,我只想监视 DOM 中的文本更改。可能是因为新节点添加了一些文本,也可能是因为某个现有标签中的文本从 a 更改了。至 b ,或者可能是因为文本被添加到空标签 <h1></h1> --> <h1>hello</h1>
    我怎么能这样做?我尝试了 MutationObserver,但我发现它很困惑,我无法正确使用它。我想检测新添加的文本并通过转换函数对其进行转换。例如,
    <span>apple</span>
    添加后,它将被转换为:
    const transformedText = transform("apple");
    // update the text
    <span>transformedText</span>
    这是我尝试过的:
    const config = {
    attributes: false,
    childList: true,
    subtree: true,
    characterData: true,
    characterDataOldValue: true
    };
    const callback = function (mutationsList, observer) {
    // Use traditional 'for loops' for IE 11
    const dict = transformDictionary();
    for (const mutation of mutationsList) {
    if (mutation.type === "childList") {
    for (const node of mutation.addedNodes) {
    if (node.setAttribute) {
    // TRANSFORM
    }
    }
    if (mutation.target) {
    // TRANSFORM
    }
    } else if (mutation.type === "characterDataOldValue") {
    console.log(mutation);
    } else if (mutation.type === "characterData") {
    }
    }
    };

    const o = new MutationObserver(callback).observe(document.body, config);
    但似乎有很多不是文本节点的节点的触发器。例如,一个节点也会收到一个触发器,比如这个完整的 div :
    <div>
    wow
    <div>
    ok
    <p>
    <span>hello</span>
    </p>
    </div>
    </div>
    我怎样才能做到这一点?

    最佳答案

    "use strict";
    const NOT_AVAILABLE = "NOT AVAILABLE";

    let dictionary = new Map();
    dictionary.set("apple", "transformedAppleText");
    dictionary.set("pickle", "transformedPickleText");
    dictionary.set("pumpkin", "transformedPumpkinText");

    const callback = function(mutationsList, observer) {

    const transform = (node) => {
    let transformedText = dictionary.get(node.textContent);
    node.textContent = transformedText !== undefined ? transformedText : NOT_AVAILABLE;
    };

    for (let mutation of mutationsList) {
    if (mutation.type === "childList") {
    for (let node of mutation.addedNodes) {
    // Thanks to:https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
    if (node.nodeType === Node.TEXT_NODE) {
    transform(node);
    } else if (node.childNodes.length > 0) {
    for (let childNode of node.childNodes) {
    if (childNode.nodeType === Node.TEXT_NODE) {
    transform(childNode);
    }
    }
    }
    }
    }

    }
    // clear out any changes made during this function - prevents bouncing
    observer.takeRecords();
    };

    const config = {
    childList: true,
    subtree: true
    };

    const o = new MutationObserver(callback).observe(document.getElementById("main"), config);

    // DOM updates for testing
    const div = document.createElement("div");
    document.body.appendChild(div);
    const apple = document.createTextNode("apple");
    const p = document.createElement("p");
    p.id = "apple";
    p.appendChild(apple);
    document.getElementById("div").appendChild(p);
    const orange = document.createTextNode("orange");
    const p2 = document.createElement("p");
    p2.appendChild(orange);
    document.getElementById("div").appendChild(p2);
    document.getElementById("span").textContent = "pumpkin";
    setTimeout(() =>
    document.getElementById("apple").textContent = "pickle", 4000);
    <main id="main">
    <h1>Demo</h1>
    <p>New line</p>
    <div id="div">
    <p>Some text</p>
    <div>cat
    <span id="span"></span>
    </div>
    </div>
    </main>
    .takeRecords方法读取队列中的所有突变并返回它们。它还清空队列。我用它来丢弃由观察者中的代码更改文本引起的任何突变。 https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/takeRecords

    关于javascript - 观察整个 HTML DOM 中的文本变化 - 如何做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65501964/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com