Skip to content

Instantly share code, notes, and snippets.

@JohnPhamous
Last active November 19, 2024 23:32
Show Gist options
  • Save JohnPhamous/ecaa69a4f64acac8e02b9baea826863e to your computer and use it in GitHub Desktop.
Save JohnPhamous/ecaa69a4f64acac8e02b9baea826863e to your computer and use it in GitHub Desktop.

Revisions

  1. JohnPhamous revised this gist Jul 12, 2023. 1 changed file with 49 additions and 32 deletions.
    81 changes: 49 additions & 32 deletions chaos-string-test.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,40 @@
    function walkDOMTree(
    root,
    whatToShow = NodeFilter.SHOW_ALL,
    { inspect, collect, callback } = {}
    ) {
    export const chaosTestStrings = (): void => {
    const textNodes = getAllTextNodes(document.body);

    for (const node of textNodes) {
    const textNodeLength = node.textContent ? node.textContent.length : 0;
    if (node.textContent === null) {
    return;
    }
    if (node.parentElement instanceof Element) {
    if (node.parentElement.dataset.originalText === undefined) {
    node.parentElement.dataset.originalText = node.textContent;
    node.textContent = generateRandomString(textNodeLength * 3);
    } else {
    node.textContent = node.parentElement.dataset.originalText;
    node.parentElement.removeAttribute('data-original-text');
    }
    }
    }
    };

    const PARENT_TAGS_TO_EXCLUDE = new Set(['STYLE', 'SCRIPT', 'TITLE']);
    const RANDOM_CHARACTERS =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';

    function* walkDOMTree(
    root: Node,
    whatToShow: number = NodeFilter.SHOW_ALL,
    {
    inspect,
    collect,
    callback,
    }: {
    inspect?: (node: Node) => boolean;
    collect?: (node: Node) => boolean;
    callback?: (node: Node) => void;
    } = {},
    ): Generator<Node, void> {
    const walker = document.createTreeWalker(root, whatToShow, {
    acceptNode(node) {
    if (inspect && !inspect(node)) {
    @@ -15,39 +47,24 @@ function walkDOMTree(
    },
    });

    const nodes = [];
    let n;
    while ((n = walker.nextNode())) {
    callback?.(n);
    nodes.push(n);
    yield 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* getAllTextNodes(el: Node): Generator<Node, void> {
    yield* walkDOMTree(el, NodeFilter.SHOW_TEXT, {
    inspect: (textNode: Node) =>
    !PARENT_TAGS_TO_EXCLUDE.has(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);
    });
    function generateRandomString(length: number): string {
    return Array.from(
    { length },
    () =>
    RANDOM_CHARACTERS[Math.floor(Math.random() * RANDOM_CHARACTERS.length)],
    ).join('');
    }
  2. 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);
    });