function(text, mode, all) {
const needle = mode === 'exact' ? text : text.toLowerCase();
const matches = el => {
const raw = (el.innerText || el.textContent || el.value || '').trim();
if (mode === 'exact') return raw === needle;
const t = raw.toLowerCase();
if (mode === 'starts_with') return t.startsWith(needle);
if (mode === 'ends_with') return t.endsWith(needle);
return t.includes(needle);
};
const interactive = Array.from(document.querySelectorAll('a,button,input[type="submit"],input[type="button"],[role="button"],[onclick]')).filter(matches);
if (interactive.length && !all) return interactive;
const innermost = Array.from(document.querySelectorAll('label,span,div,p,h1,h2,h3,h4,h5,h6,li,td,th'))
.filter(matches)
.filter(el => !Array.from(el.children).some(matches))
.filter(el => !interactive.some(i => el.contains(i) || i.contains(el)));
return interactive.concat(innermost);
}