0

I have a list of potential html classes in my javascript file

var html_list = ["blogrss",
"btnrss",
"buttonsrssfeed",
"copypasteblocker",
"facebook128x128",
"feedicon",
"iconrss",
"instagram128x128",
"jobwidget",
"pinterest128x128",
"pushnotification",
"rss128x128"]

Upon visiting a webpage i would like to iterate the array and if the webpage contains one of the elements a simple alert will show which html element the page contains.

6
  • 3
    what are these strings supposed to be? Classes? Ids? - what have you tried? Commented Jun 20, 2018 at 19:29
  • Please share your code for better understanding Commented Jun 20, 2018 at 19:30
  • Try google chrome extension TamperMonkey or FF extn GreaseMonkey and learn Userscripts to write in them Commented Jun 20, 2018 at 19:35
  • Are the values - ids of the elements or the tagnames ? Commented Jun 20, 2018 at 19:35
  • hi these are typical examples of spam html classes used in certain webpages Commented Jun 20, 2018 at 19:38

4 Answers 4

1

You would need to run a script that basically iterates through your list and checks the DOM to see if there's a match. Keep in mind that a selector can be a tag, class, id, etc... In your list, all selectors would be treated as a tag selector so you would need to provide the right selector and the rest will fall into place (#id or .class-name or tagName):

( () => {
    const elements = [
  "blogrss",
  "btnrss",
  "buttonsrssfeed",
  "copypasteblocker",
  "facebook128x128",
  "feedicon",
  "iconrss",
  "instagram128x128",
  "jobwidget",
  "pinterest128x128",
  "pushnotification",
  "rss128x128"
  ];
  const matchedElements = [];
  elements.forEach( (el) => {
    const match = document.querySelectorAll(el);
    if (match && match.length > 0) {
        matchedElements.push({ elementName: el, matches: match.length });
    }
  });
  alert(`Found ${matchedElements.length}`);
  console.log(matchedElements); // More detailed view of the matches
})();
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. These are examples of class names. Your solution looks good.
is there anyway to return to the name of the found element. Currently only found[Object object] is displayed?
You'll have to update your code and provide a JS Fiddle. The above code will display the element name since the array is an array of strings
0

You can have a look at the jquery all selector https://api.jquery.com/all-selector/

Comments

0

You can use Cheerio: https://www.npmjs.com/package/cheerio

More specifically, check out the traversing section: https://www.npmjs.com/package/cheerio#traversing

Comments

0

you can use document.getElementsByTagName("*") to get all the elements, then simply use classList and some array method to determine if any elements have the class.

In the below function the searchForClasses function will return true or false depending on if there's a match.

var html_list = [
  "testClass"
],
second_html_list = [
"banana"
]


function searchForClasses(classArray) {
  let class_included = false,
    elements = Array.from(document.getElementsByTagName("*"));
  for (let _class of classArray) {
    class_included = elements.some(ele => ele.classList.contains(_class));
    if (class_included) break;
  }
  return class_included;
}

console.log(searchForClasses(second_html_list)); // false
console.log(searchForClasses(html_list)); // true
<div class="testClass"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.