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

https://github.com/f/xtract

Extract data from DOM, easily.
https://github.com/f/xtract

Last synced: about 1 year ago
JSON representation

Extract data from DOM, easily.

Awesome Lists containing this project

README

          

# Xtract

Extract data from DOM, easily. Useful for back-end generated contents and **SEO-friendly** rich apps.

![Image](https://raw.githubusercontent.com/f/xtract/master/test/xtract.png)

## Installation
```
npm install xtract
```

## Requirements

- This works on browser, not node.js. But if you use *jsdom*, you can.
- Requires jQuery.

SEO is the main problem of modern web. And we have problems with passing the data from
**HTML to JavaScript**. Your **back-end** generated data is need to be mapped to JavaScript
and **Xtract** helps you to do that.

```html


My name is Fatih,
and I'm from Istanbul.


```

You can simply extract data now just calling:

```js
xtract($("#profile")).$model
```

This will generate following object:
```js
{
user: {
name: "Fatih",
location: "Istanbul"
}
}
```

## Extracting Nested Models

```html


My name is Fatih
Akın,
and I'm from Istanbul,
Turkey
(TR)
.


```

```js
xtract($("#profile")).$model
```

This will generate following object:
```js
{
user: {
name: {
firstname: "Fatih",
lastname: "Akın"
},
location: {
city: "Istanbul",
country: {
name: "Turkey",
code: "TR"
}
}
}
}
```

## Extracting with jQuery

You can use `$this` in `data-x` attribute to reach more values.

```html



My name is Fatih,
and I'm from Istanbul.


```

```js
xtract($("#profile")).$model
```

This will map the `src` tag to the `user.image`:
```js
{
user: {
name: "Fatih",
location: "Istanbul",
image: "my-profile-picture.jpg"
}
}
```

## Plug-ins

You can simply write plugins to use extract easier.

```js
xtract.plug('date', function () {
return $(this).text().replace(/(\d+)\s+(\w+)\s+(\d+)/, '$3, $1 $2');
});
```

The static HTML:
```html


Einstein: 14 March 1879
18 April 1955

```

Output:
```js
date: {
birth: "1879, 14 March",
death: "1955, 18 April"
},
```

## License
MIT.