Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/victornpb/querycomments

querySelector like but for selecting HTML Comments like any other DOM Node
https://github.com/victornpb/querycomments

dom-node getcomments javascript javascript-library npm-package regex-pattern selector-engine

Last synced: 16 days ago
JSON representation

querySelector like but for selecting HTML Comments like any other DOM Node

Awesome Lists containing this project

README

        

# queryComments.js

![Node](https://img.shields.io/node/v/querycomments.svg?style=flat-square)
[![NPM](https://img.shields.io/npm/v/querycomments.svg?style=flat-square)](https://www.npmjs.com/package/querycomments)
[![NPM](https://img.shields.io/npm/dt/querycomments.svg?style=flat-square)](https://www.npmjs.com/package/querycomments)

Select HTML Comments like any other DOM Node, with this fast comment selector implementation.

## Installation

[Yarn](https://github.com/yarnpkg/yarn)

yarn add querycomments

NPM

npm install querycomments

If you don't use a package manager, you can [access `querycomments` via unpkg (CDN)](https://unpkg.com/querycomments/), download the source, or point your package manager to the url.

```html

```

JSFiddle Example [DEMO](https://jsfiddle.net/Victornpb/630garm8/)

## API

```ts
getComments(context:DOMNode, filter:function|RegExp):Array
```

## Examples

get all comments

```js
getComments();
```

get all comments inside a element

```js
getComments(document.getElementById('someDiv'));
```

get the first comment that match a regex pattern

```js
getComments(document, /hey/i);
```

get the first comment is filtered by a function

```js
getComments(document, (text, comment) => {
this.breakOnFirst = true; //I only need the first comment
return text.substring(0, 4) === 'Lorem'; //does it starts with "Lorem"
});
```

get All comments that match a regex pattern

```js
getComments(document, /foo/g); // notice the global flag on the regex
```

get all comments filtered by a function

```js
getComments(document, (text, comment) => {
const possibleValues = ['foo', 'bar', 'baz'];

for (var i = 0; i < possibleValues.length; i++) {
if (text.indexOf(possibleValues[i]) > -1) return true;
}
return false;
});
```

## Suggestions / Questions

File a [issue](https://github.com/victornpb/getComments.js/issues) on this repository