-
Notifications
You must be signed in to change notification settings - Fork 1
/
jextract.min.js
7 lines (7 loc) · 4.48 KB
/
jextract.min.js
1
2
3
4
5
6
7
/**
jExtract: a function for extracting data from DOM.
Version: 1.1.1
Author: Mikhail Kormanowsky (@kormanowsky)
Date: 18.07.2020
*/
!function(){"use strict";!function(){const t="object"==typeof window;let e,n,r=t?window.document:null;if(t)e=window.jQuery;else{n=require("node-html-parser");try{e=require("jquery")}catch(t){e=null}}class i extends Error{constructor(t){super(),this.name="JExtractError",this.message=t}}class o{static NaN(t){return o.NaN(t)}static null(t){return null===t}static undefined(t){return void 0===t}static object(t){return"object"==typeof t&&t instanceof Object&&!(t instanceof Array)}static array(t){return"object"==typeof t&&t instanceof Array}static emptyArray(t){return this.array(t)&&!t.length}static function(t){return"function"==typeof t}static jQuery(t){return e&&t instanceof e}static jExtractElement(t){return t instanceof c}static NodeList(e){return t?e instanceof window.NodeList:this.array(e)}static string(t){return"string"==typeof t}static emptyString(t){return this.string(t)&&!t.length}static CSSSelector(e){if(!this.string(e)||this.emptyString(e))return!1;if(t)try{return r.querySelector(e),!0}catch(t){return!1}else{const t=new(0,require("css-selector-parser").CssSelectorParser);try{return t.parse(e),!0}catch(t){return!1}}}static JSON(t){if(!this.string(t)||this.emptyString(t))return!1;try{t=JSON.parse(t)}catch(t){return!1}return!0}static HTML(t){return!(!this.string(t)||this.emptyString(t))&&("<"==(t=t.trim())[0]&&">"==t[t.length-1])}}class s{constructor(t){this.text=t}get(t){return(t=t||!0)?this.text.trim():this.text}match(t,e){let n=this.get().match(new RegExp(t));return!o.undefined(e)&&o.array(n)&&e in n?n[e]:n}toInt(t){return t=t||!1,!o.NaN(parseInt(this.get()))||t?parseInt(this.get()):0}toFloat(t){t=t||!1;let e=this.get().replace(",",".");return!o.NaN(parseFloat(e))||t?parseFloat(e):0}}class c{constructor(t){this.element=t}text(){return this.element.innerText||this.element.textContent||this.element.text}attr(t){return this.element.getAttribute(t)}get(){return this.element}}function u(t,e){if(!t.get())throw new i("Incorrect parent element or selector");if("."==e)return[t];{if(!o.CSSSelector(e))throw new i("Could not find elements that match given selector: "+e);let n=t.get().querySelectorAll(e),r=[];return n=o.NodeList(n)?n:[n],l(n,(function(t,e,n){r[e]=new c(t)})),r}}function a(t,e){let n=e[0],r=e[1];return o.undefined(t)||(o.string(t)||o.function(t)?n=t:o.array(t)&&!o.empt.array(t)&&(n=t[0],t.length>1&&(r=t.slice(1)))),[n,r]}function l(t,e){if(o.array(t)||o.NodeList(t))t.forEach(e);else if(o.object(t))for(let n in t){e(t[n],n,t)}}function f({struct:f,root:g,options:p}){let y={};if(f=f||{},(p=o.object(p)?p:{}).json=p.json||!1,!g)throw new i("Root element is not specified.");if(!o.object(f)){if(!o.JSON(f))throw new i("Incorrect JSON");f=JSON.parse(f)}return o.string(g)&&(g=function(e){if(o.CSSSelector(e))return new c(r.querySelector(e));if(o.HTML(e)){let i;return t?(i=r.createElement("div"),i.innerHTML=e):(i=n.parse("<div></div>"),i.set_content(e)),new c(i)}}(g)),o.jQuery(g)&&(g=g[0]),o.jExtractElement(g)||(g=new c(g)),l(f,(function(t,n){if(o.object(t))return void(y[n]=h(t));let r,c,f=["text",[]],p=["get",[]],d={},m=[];o.string(t)?r=u(g,t):(r=u(g,t[0]),o.object(t[1])?c=t[1]:f=a(t[1],f),p=a(t[2],p),o.object(t[3])&&(d=t[3]),d.keepArray=d.keepArray||!1),l(r,(function(t,n){if(o.undefined(c)){let c=function(){},u=[],a=null;if(o.function(f[0]))c=f[0],u=[t,n,r].concat(f[1]);else if(o.string(f[0]))if(u=f[1],f[0]in t)c=t[f[0]],a=t;else{if(!e||!(f[0]in e(t.get())))throw new i("Undefined Element method: "+f[0]);c=e(t.get())[f[0]],a=e(t.get())}let l,h=c.apply(a,u);if(o.string(h)&&p[0]){if(c=function(){},u=[],a=null,h=new s(h),o.function(p[0]))c=p[0],u=[h.get(),n].concat(p[1]);else if(o.string(p[0]))if(u=p[1],p[0]in h)c=h[p[0]],a=h;else{if(!(p[0]in new String(h.get())))throw new i("Undefined Text method: "+p[0]);c=h.get()[p[0]],a=h.get()}l=c.apply(a,u)}else l=h;m.push(l)}else m.push(h(c))})),!d.keepArray&&m.length<2&&(m=m[0]),y[n]=m})),p.json?JSON.stringify(y):y}function h(e){let n={struct:e,options:{},root:{}},o={using:t=>(n.options=t,o),from:t=>(n.root=t,f(n)),fromDocument(){if(!t)throw new i("Cannot extract from document when not in browser.");return this.from(r)}};return o}h.extendText=function(t){l(t,(t,e)=>{s.prototype[e]=function(){return t.call(this,this)}})},h.extendElement=function(t){l(t,(t,e)=>{c.prototype[e]=function(){return t.call(this,this)}})},t?(window.jExtract=h,e&&(window.jQuery.fn.jExtract=function(t){return h(t,e(this))})):module.exports=h}()}();