As part of a Chrome extension I am searching the DOM for elements containing specific words inside each ID/Class.
Currently it looks something like:
"allSelectors": document.querySelectorAll("[id*='example'][class*='example']"),
"collapse": function () {
for (var i = 0; i < MyObject.allSelectors.length; i++) {
// Manipulate MyObject.allSelectors[i] etc
}
},
First, I would like to restructure it somehow (possibly using an array
?) so that it is easy to add new selectors as doing it like this:
document.querySelectorAll("[id*='example'][class*='example'][id*='other'][class*='other']")
Isn’t scaleable/good.
Secondly, I think document.querySelectorAll
is very slow - the reason I am using it is because I need to search anywhere in an id/class (hence the use of *=
) and cannot use a big external library (such as jQuery), as this is a small file and is being injected user side.
Is there an any solution to these problems? because if there are many matches then this slowness might become an issue.