Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
4
Indexable
Never
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script
      src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"
      integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>
    <title>Document</title>
  </head>
  <body>
    <div class="content">
      <p>
        <br />
        <br />
        Lorem ipsum dolor sit, click amet consectetur adipisicing elit. Neque ut minima veritatis
        voluptas beatae error tempore assumenda omnis iure est.
        <br />
        <br />
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora suscipit, sunt magnam,
        incidunt eum, accusamus provident hic voluptates commodi totam expedita odit! Deserunt,
        temporibus laborum?
      </p>
    </div>
  </body>
  <script>
    $(document).ready(function () {
      var data = ["click", "abc", "def"];
      var content = document.querySelector(".content p");
      var content_text = content["innerText"];

      const replaceOnDocument = (pattern, string, { target = content } = {}) => {
        [target].forEach(({ childNodes: [...nodes] }) =>
          nodes
            .filter(({ nodeType }) => nodeType === Node.TEXT_NODE)
            .forEach(
              (textNode) => (textNode.textContent = textNode.textContent.replace(pattern, string))
            )
        );
      };

      for (let i = 0; i < data.length; i++) {
        var cari = content_text.search(data[i]);
        if (cari) {
          replaceOnDocument(data[i], '<a href="google.com">click</a>');
        }
      }
    });
  </script>
</html>