I’m working on a custom user script that will block/mark users from my list. Say the list will contain strings "EvilJoe", "ToxicWill" & "NegativeSara". I want to find all, say, "span.username" elements where text inside the span contains one of the strings above, so for example "User EvilJoe Premium" would match.
I can do it with a loop through badUserNames, where for each badUserNameI’d do something like
$("span.username:contains(badUserName)").append(" - bad guy");
Is this an effective solution though?
2
Answers
:contains will be slow if there is a large number of DOM elements because the jQuery itself acting as a middle to translate the :contains to a native JS function. in that case it is better to use a regular expression like this:
This code searches the entire body of the page for elements that contain any of the search terms in the
searchTerms
array. The:not(script)
selector is used to exclude any script elements from the search.Try below Code
You can modify this code as per you want