I've copied some text nodes from the page (i.e., live DOM) and have stored them in an array. Is it possible to locate those exact nodes again in the live DOM? I would need to use plain JavaScript.
This is basically how my project works:
- A configuration object specifies sections of the page (using CSS selectors) where there is some text content, and optionally subsections within that section (also using CSS selectors) where there is text content that should be ignored. The property names that I use, respectively, are
select
andexclude
. - Using Sizzle (jQuery's selector engine), I generate an element collection for each of the
select
selectors, then clone it. - I then run the
exclude
selectors against theselect
collection, finding any elements that match, and remove them from theselect
collection. - Using the
select
collection with the removedexclude
sections, I traverse it to build an array of only the text nodes. - I use this array of text nodes to do some word matching based on a supplied list of terms that may be in the original text content. To do this, I build an array of objects that include properties like the text node in which a term was found, and at what position/offset that term occurs within the text node's
data
.
Given the latter array, I need to be able to match the cloned text nodes in which matching terms were found to the original text nodes from which they were cloned. If I can do this, I can just iterate over my array of objects, first finding the text node that corresponds to the live DOM (from which it was originally cloned), and then linking the term at the position/offset that I have recorded in the object.
Hopefully this makes some sense - please let me know if not, and I can provide more details. This must be deterministic - i.e., I can't just search the live DOM for a text node that has the same data
, as that may lead to false positives.
Again, I must use plain JavaScript here, not jQuery or any other libraries.
I appreciate any help!