search full

mail@pastecode.io avatar
unknown
jsp
a year ago
1.4 kB
3
Indexable
function findTagsWithSearchText(searchText) {
    const tags = document.querySelectorAll('*');
    const results = [];

    for (let i = 0; i < tags.length; i++) {
        const tag = tags[i];
        if (tag?.innerText !== null && tag?.innerText?.trim() !== '') {
            //   if (tag?.innerText?.includes(searchText)) {
            //     results.push(tag);
            //   }
            if (searchText.split(" ").every(word => tag?.innerText?.includes(word))) {
                console.log("All words found!", tag);
                results.push(tag);
            }
        }
    }
    const filteredResults = []
    results.forEach(item => {
        let isParents = false;
        results.forEach((otherTag) => {
            if (otherTag !== item) {
                if (item.contains(otherTag)) {
                    isParents = true;
                }
            }

        });
        // If tag is not a parent, add it to the result array
        if (!isParents) {
            filteredResults.push(item);
        }
    })

    console.log("Hello", filteredResults)

    return filteredResults;
}

const searchText = 'Days out pointed';
const results = findTagsWithSearchText(searchText);
console.log(results);
for (let i = results.length - 1; i >= 0; i--) {
    if(results[i]?.style){
    results[i].style.backgroundColor = "yellow";

    }
}