![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
A function that allows developers to extract data from DOM to JavaScript objects.
It makes possible extracting data from DOM. May be useful when you are working with data from websites that don't have any data APIs. In that case you can use this function to read data directly from DOM (or HTML string).
Just download jextract.min.js and include it in your page.
npm install jextract
<h1 id="page-title">Hello, world!</h1>
<p id="page-content">Lorem ipsum dolor sit amet.</p>
<!-- Your HTML continues here -->
<script src="/path/to/jextract.min.js"></script>
<script>
//Your JavaScript goes here
</script>
or
// Import jExtract
const jExtract = require("jextract")
var structure = {
title: "#page-title",
content: "#page-content"
};
var data = jExtract(structure).fromDocument();
data
will be:
{
title: "Hello, world!",
content: "Lorem ipsum dolor sit amet."
}
// 1. Extract data from specified root (root may be an element, a CSS selector or a HTML string)
jExtract(structure).using(options).from(root); // .using() is optional
// 2. Extract data from the whole document (only in browser)
jExtract(structure).using(options).fromDocument(); // Does not work in Node.js
Method | Description |
---|---|
.fromDocument (Required) | Extracts the data from the whole document. Works only in browser. |
.from(root) (Required) | Extracts the data from given root (root may be an element, a jQuery instance, a CSS selector, a HTML string) |
.using(options) (Optional) | Allows passing options object to jExtract |
Name | Description | Possible Values | Default Value |
---|---|---|---|
json | Should the output be in JSON format? | true/false | false |
You can pass structure as JSON.
var data = jExtract("JSON here").fromDocument();
You can add substructures into your main structure.
var struct = {
key1: 'selector1',
key2: {
subkey1: 'selector2',
subkey2: 'selector3'
}
}, data = jExtract(struct).fromDocument();
By default, jExtract returns the text of matched element(s). But you can change this behavior by passing more than argument in your structure keys (key: [selector, dataGettingMethod, filterMethod, options]
instead of key: selector
).
It's a function that returns data that is extracted from element.
Default: text()
.
Before v0.0.4, jExtract used its own element object that was based on jQuery.
Since v0.0.4 until v0.0.6, jExtract used a plain jQuery object without any additions/deletions, so you were able to call any jQuery object methods while extracting data with jExtract.
There are a few ways to pass data getting method to jExtract:
width
);['attr', 'href']
);element
, index
, elements
:var struct = {
key1: ['div', function(element, index, elements){
//in element -> one div (current in the loop)
//in index -> index of this div
//in elements -> all matched elements
}]
}
Since v0.0.7, jExtract uses its own object again, but its behavior was changed. Here's what jExtract does while extracting data:
console.error()
and stops extracting data from this structure key.It's a function that filters extracted data.
Default: jExtractText.get()
jExtractText is a class that exists only in jExtract function, so you can't access it outside of it. In this class I collect useful methods for working with strings. Currently supported:
1.jExtractText.get(trim)
: if trim is true - trims text that is stored in jExtractText and returns it. Defaults: trim = true
.
2.jExtractText.match(regexp, index)
: tries to match text with a given regular expression and returns a match with index = index
if it is given or full match if is not. Defaults: no defaults.
3.jExtractText.toInt(leaveNaN)
: tries to parseInt() text. If leaveNaN
is true returns NaN if it appears. If leaveNaN
is false returns 0 instead of NaN. Defaults: leaveNaN = false
.
4.jExtractText.toFloat(leaveNaN)
: tries to parseFloat() text. If leaveNaN
is true returns NaN if it appears. If leaveNaN
is false returns 0 instead of NaN. Defaults: leaveNaN = false
.
If you want to use one of these methods, just pass a name and optionally arguments as a third parameter in your value:
var struct = {
key1: [selector, dataGettingMethod, [name, ...arguments]]
}
If you want to use your own method, pass it as third parameter. Your method will recieve 2 parameters: value
and index
.
var struct = {
key1: ['div', 'text', function(value, index){
// Value will be a number/boolean/etc., that was returned by dataGettingMethod.
// Index is an ordinal number of element (starting with 0)
return value;
}]
}
Since v0.0.7, jExtract's behavior with filter methods is the following:
Name | Description | Possible Values | Default Value |
---|---|---|---|
keepArray | What to do with a single value? jExtract generates an array of values during its loop. If there is less than two elements in the resulting array, the result will contain only first element of this array. If you don't want to lose an array in the result, set this to true | true/false | false |
By default, jExtract searches for elements in <html>
tag. You can call .from() instead of .fromDocument() to change this:
var data = jExtract(structure).from($("#someElement"));
Also you can create a substructure that will be applied to each matched element. Think of the following HTML:
<div class="user">
<div class="uname">User 1</div>
<div class="uid">1</div>
<div class="uemail">user1example.com</div>
</div>
<div class="user">
<div class="uname">User 2</div>
<div class="uid">2</div>
<div class="uemail">user2example.com</div>
</div>
<div class="user">
<div class="uname">User 3</div>
<div class="uid">3</div>
<div class="uemail">user3example.com</div>
</div>
<div class="user">
<div class="uname">User 4</div>
<div class="uid">4</div>
<div class="uemail">user4example.com</div>
</div>
<div class="user">
<div class="uname">User 5</div>
<div class="uid">5</div>
<div class="uemail">user5example.com</div>
</div>
You can create an array of all users with each element filled with each user data. In your key pass two parameters: selector and a structure that you want to be applied to this selector.
jExtract({
user: ['.user', {
name: '.uname',
id: ['.uid', [], 'toInt'],
email: '.uemail'
}]
}).fromDocument();
You will get an array:
[
{name: "User 1", id: 1, email: "user1example.com"},
{name: "User 2", id: 2, email: "user2example.com"},
{name: "User 3", id: 3, email: "user3example.com"},
{name: "User 4", id: 4, email: "user4example.com"},
{name: "User 5", id: 5, email: "user5example.com"}
]
Since v0.0.6, it's possible to pass a HTML string as parent element.
Since v0.0.7, it's possible to pass a selector as parent element.
In your values you can refer to current element using "."
as a selector.
You can extend jExtract's Element and Text objects.
//1. Register your method
jExtract.extendElement({
methodName: function(elementInstance){
// do something with Element instance
},
otherMethodName: ...
// etc
});
//2. Use your method
var data = jExtract({
key: ['selector', 'methodName']
});
//1. Register your method
jExtract.extendText({
methodName: function(textInstance){
// do something with Text instance
},
otherMethodName: ...
// etc
});
//2. Use your method
var data = jExtract({
key: ['selector', false, 'methodName']
});
FAQs
A function that allows developers to extract data from DOM to JavaScript objects.
We found that jextract demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.