function sensaCollectTextList(selector, limit) {
  const results = [];
  for (const element of document.querySelectorAll(selector)) {
    if (!sensaIsElementVisible(element)) {
      continue;
    }
    const text = sensaNormalizeText(
      element.textContent ||
        element.value ||
        element.getAttribute("aria-label") ||
        element.getAttribute("placeholder") ||
        ""
    );
    if (!text) {
      continue;
    }
    results.push(text);
    if (results.length >= limit) {
      break;
    }
  }
  return results;
}

function sensaCollectVisibleText(root, maxChars) {
  if (!root) {
    return "";
  }

  const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
  const chunks = [];
  let total = 0;

  while (walker.nextNode()) {
    const node = walker.currentNode;
    const parent = node.parentElement;
    if (!parent || sensaShouldSkipTextNode(parent)) {
      continue;
    }

    const text = sensaNormalizeText(node.textContent || "");
    if (!text) {
      continue;
    }

    const remaining = maxChars - total;
    if (remaining <= 0) {
      break;
    }

    const slice = text.slice(0, remaining);
    chunks.push(slice);
    total += slice.length + 1;
  }

  return chunks.join("\n").trim();
}
