npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

get-elements-by-text-contents

v1.0.1

Published

Extremely-lightweight, flexible method which allows the user to search the DOM for all nodes containing a specific, provided search string. Supports DOM tree scoping, case-(in)sensitivity as well as an exclusionary selector to omit matching nodes. No depe

Downloads

4

Readme

get-elements-by-text-contents

NPM js-standard-style dark-side

NPM Package - Extremely-lightweight, flexible method which allows the user to search the DOM for all nodes containing a specific, provided search string. Supports DOM tree-scoping, case-(in)sensitivity as well as an exclusionary selector to omit matching nodes.

No dependencies.

So what does it ACTUALLY do?

If you wanna search a page for every instance of a string, and get back the elements that CONTAIN those instances - almost instantly, and without mutating or having to iterate the entire DOM - this is your method.

In goes search text...

...out comes DOM nodes.

Examples

// Import the method
const getElementsByTextContents = require('get-elements-by-text-contents');
//         ==OR==  (and this is personal preference; you do you):
document.getElementsByTextContents = require('get-elements-by-text-contents');

// Find your targets
console.log(getElementsByTextContents('myText'))
// RESULT => [p, div, a, h2]

console.log(getElementsByTextContents('MyTeXt', true))
// RESULT => [p, h2]

console.log(getElementsByTextContents('myText', false, ''))
// RESULT => [p, div, a, h2, script, script]

console.log(getElementsByTextContents('myText', false, 'script,p,h2'))
// RESULT => [div, a]

getElementsByTextContents(searchText, [caseSensitive, [exclusionarySelector, [scope]]])

Returns

This method will return an array, regardless of the number of results it manages to locate.

If results are found...

Results will be returned in an array, in the form of the parentNode of the located object (this is due to the text itself being present in a Text node, which are virtually impossible to interact with (especially without mutating the DOM). In short, you get back the node CONTAINING the text, not the text itself).

If no results are found...

In the case of zero results, the returned array will simply be blank ([]).

If no parameters are passed to the method at all...

If you omit ALL the parameters to the method, its default behavior is to return an array containing the node of every NON-<script> element within the DOM, with document.body as their collective root.

Usage/Parameters

Note: Below examples are assuming an html page that looks like the following.

<a href="#findtext">Method: Find This Text</a>
<h4>Find This Text</h4>
<p class="desc">
    I wanted a method that would let me find this text and return all the 
    nodes on the page containing it. So I wrote one.
</p>
<script>
    // Looking for a quick method to "find this text"?
    npm install get-elements-by-text-contents
</script>

searchText (string/regex)

A string or a regular expression that will result in a match for the text the user wishes to find within the page.

getElementsByTextContents('find this text');
// => [a, h4, p]

getElementsByTextContents(/.+find this/im);
// => [a, p]
// Note that the <h4> is omitted, due to the regular expression requiring text to preface the 
// matched string.

caseSensitive (boolean; optional) [default: false]

Whether or not the search should be case sensitive

getElementsByTextContents('find this text', true);
// => [p]
// Note that the <a> and <h4> tags are omitted, due to their being written In Title Case.

exclusionarySelector (string) [default: 'script']

CSS selector used to EXCLUDE nodes containing the search string. Any nodes with characteristics matching the selector will NOT be returned within the result set. By default <script> tags are excluded, as they are non-interactive within the DOM. This can be overridden by simply passing a blank string ("") or a null into this parameter. Note that if you override this parameter to pass in a new set of exclusions, <script> tags will NOT be omitted unless you expressly include them in your selector.

getElementsByTextContents('find this text', false, '');
// => [a, h4, p, script]
// Note the inclusion of the script tag in the search results: passing the blank string 
// as the third parameter overrides the default selector of "script".

getElementsByTextContents('find this text', false, '[href],.desc,script');
// => [h4]
// In this case, the exclusionary selector omits all the results other than the <h4> (those that match
// the selector: any tag with an href property, a class of 'desc' or that itself is a <script> tag.

scope (string) [default: document.body]

Dictates the root DOM Element from which to begin the search. If none is specified, document.body is assumed.

getElementsByTextContents('find this text', false, '', document.querySelector('h4'));
// => [h4]
// Since it is the only node within the document fragment our examples have been based within, 
// naturally it's the only one returned.

LICENSE MIT