Skip to content

Instantly share code, notes, and snippets.

@WooodHead
Forked from JohnPhamous/chaos-string-test.js
Created February 4, 2023 04:57
Show Gist options
  • Save WooodHead/ea4044a1d2ceb1f0b9c72d79facd9989 to your computer and use it in GitHub Desktop.
Save WooodHead/ea4044a1d2ceb1f0b9c72d79facd9989 to your computer and use it in GitHub Desktop.

Revisions

  1. WooodHead revised this gist Feb 4, 2023. 1 changed file with 1 addition and 53 deletions.
    54 changes: 1 addition & 53 deletions chaos-string-test.js
    Original file line number Diff line number Diff line change
    @@ -1,53 +1 @@
    function walkDOMTree(
    root,
    whatToShow = NodeFilter.SHOW_ALL,
    { inspect, collect, callback } = {}
    ) {
    const walker = document.createTreeWalker(root, whatToShow, {
    acceptNode(node) {
    if (inspect && !inspect(node)) {
    return NodeFilter.FILTER_REJECT;
    }
    if (collect && !collect(node)) {
    return NodeFilter.FILTER_SKIP;
    }
    return NodeFilter.FILTER_ACCEPT;
    },
    });

    const nodes = [];
    let n;
    while ((n = walker.nextNode())) {
    callback?.(n);
    nodes.push(n);
    }

    return nodes;
    }

    const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"];

    function getAllTextNodes(el) {
    return walkDOMTree(el, NodeFilter.SHOW_TEXT, {
    inspect: (textNode) =>
    !PARENT_TAGS_TO_EXCLUDE.includes(textNode.parentElement?.nodeName),
    });
    }

    function generateRandomString(length) {
    var result = "";
    var characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
    }

    const textNodes = getAllTextNodes(document.body);

    textNodes.forEach((node) => {
    const textNodeLength = node.textContent.length;
    node.textContent = generateRandomString(textNodeLength * 3);
    });
    javascript:(function(){ function walkDOMTree( root, whatToShow = NodeFilter.SHOW_ALL, { inspect, collect, callback } = {} ) { const walker = document.createTreeWalker(root, whatToShow, { acceptNode(node) { if (inspect && !inspect(node)) { return NodeFilter.FILTER_REJECT; } if (collect && !collect(node)) { return NodeFilter.FILTER_SKIP; } return NodeFilter.FILTER_ACCEPT; }, }); const nodes = []; let n; while ((n = walker.nextNode())) { callback?.(n); nodes.push(n); } return nodes; } const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"]; function getAllTextNodes(el) { return walkDOMTree(el, NodeFilter.SHOW_TEXT, { inspect: (textNode) => !PARENT_TAGS_TO_EXCLUDE.includes(textNode.parentElement?.nodeName), }); } function generateRandomString(length) { var result = ""; var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var charactersLength = characters.length; for (var i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } const textNodes = getAllTextNodes(document.body); textNodes.forEach((node) => { const textNodeLength = node.textContent.length; node.textContent = generateRandomString(textNodeLength * 3); }); })();
  2. @JohnPhamous JohnPhamous created this gist Jan 24, 2023.
    53 changes: 53 additions & 0 deletions chaos-string-test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    function walkDOMTree(
    root,
    whatToShow = NodeFilter.SHOW_ALL,
    { inspect, collect, callback } = {}
    ) {
    const walker = document.createTreeWalker(root, whatToShow, {
    acceptNode(node) {
    if (inspect && !inspect(node)) {
    return NodeFilter.FILTER_REJECT;
    }
    if (collect && !collect(node)) {
    return NodeFilter.FILTER_SKIP;
    }
    return NodeFilter.FILTER_ACCEPT;
    },
    });

    const nodes = [];
    let n;
    while ((n = walker.nextNode())) {
    callback?.(n);
    nodes.push(n);
    }

    return nodes;
    }

    const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"];

    function getAllTextNodes(el) {
    return walkDOMTree(el, NodeFilter.SHOW_TEXT, {
    inspect: (textNode) =>
    !PARENT_TAGS_TO_EXCLUDE.includes(textNode.parentElement?.nodeName),
    });
    }

    function generateRandomString(length) {
    var result = "";
    var characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
    }

    const textNodes = getAllTextNodes(document.body);

    textNodes.forEach((node) => {
    const textNodeLength = node.textContent.length;
    node.textContent = generateRandomString(textNodeLength * 3);
    });