Луна

This lightweight script automatically cleans up your page’s HTML structure to improve SEO readability and accessibility.
It removes broken or empty links, redundant <strong> tags, and unnecessary empty paragraphs and keeping your site markup clean and efficient.

The script runs after the page loads and:
✓ Deletes empty <p> elements that don’t contain media.
✓ Removes <a> tags without valid href, text, or aria-label.
✓ Simplifies nested <strong> tags inside headings (H1–H3).


Integration

Open your WordPress admin → Snippets Plugin → Add a new snippet

Type: JavaScript (JS)
Location: Footer or Body (recommended)
Scope: Entire site
→ Paste the script and save.
→ Clear cache and refresh any page to activate.


(() => {
  const isBadHref = (href) => {
    if (!href) return true;
    const h = href.trim().toLowerCase();
    return !h || h === "#" || h.startsWith("javascript:");
  };

  const unwrapNode = (el) => {
    const frag = document.createDocumentFragment();
    while (el.firstChild) frag.appendChild(el.firstChild);
    el.replaceWith(frag);
  };

  const cleanContent = () => {
    // 1) Remove <a> tags without meaningful content
    document.querySelectorAll("a").forEach((a) => {
      const href = a.getAttribute("href");
      const txt = (a.textContent || "").trim();
      const aria = (a.getAttribute("aria-label") || "").trim();
      const hasImgAlt = Array.from(a.querySelectorAll("img")).some((img) => {
        const al = img.getAttribute("alt");
        return al && al.trim().length > 0;
      });

      if (isBadHref(href) && !txt && !aria && !hasImgAlt) {
        unwrapNode(a);
      }
    });

    // 2) Normalize nested <strong> tags in headings
    document.querySelectorAll("h1, h2, h3").forEach((h) => {
      h.classList.remove("has-link-color");
      h.innerHTML = h.innerHTML
        .replace(/(?:\s*<strong>)+/gi, "<strong>")
        .replace(/(?:<\/strong>\s*)+/gi, "</strong>");
    });

    // 3) Remove empty <p> elements without media
    document.querySelectorAll("p").forEach((p) => {
      if (p.querySelector("img, video, iframe, svg")) return;
      const txt = (p.textContent || "").replace(/\u00a0/g, " ").trim();
      if (!txt) p.remove();
    });
  };

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", cleanContent);
  } else {
    cleanContent();
  }
})();