IWBH_01

extract all JSON values from javascript code

Aug 16th, 2020 (edited)
2,659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if(!self.parseHTML){
  2.   var tifr=document.createElement("iframe");
  3.   tifr.style.position="fixed";
  4.   tifr.style.bottom="500px";
  5.  
  6.  self.parseHTML=function(s){
  7.    document.body.appendChild(tifr);
  8.    var unBd=tifr.contentDocument;
  9.    document.body.removeChild(tifr);
  10.    unBd.documentElement.innerHTML=s;
  11.    return unBd; //(DOM)
  12.  };
  13. }
  14.  
  15. // DOM.getElementsByTagName("script")
  16.  
  17. //note, the variable "li" is placed next to "lev" because it's related to it, the expression if(li<3) means if last grouping character is one of these 3 opening characters: ( [ {
  18.  
  19. var RipScript=function(s){
  20.  var i=0,c,L=s.length, jsa=[],
  21.  stack=[],stL,sp,
  22.  ALL=[],ci,
  23.  lev="([{}])",li,
  24.  str,emc={"[":"]","{":"}","(":")"},
  25.  ei,pci,pv,
  26.  psj=[];
  27.  
  28.  while(i<L){
  29.   c=s[i]; stL=stack.length-1;
  30.   sp=!1; li=0;
  31.  
  32.   if(str){
  33.    if(c=="\x5C") i++;
  34.    else if(c==str){sp=!0;li=7;}
  35.   }
  36.   else if(c=="'"||c=='"'){
  37.    if(!str)str=c;
  38.    sp=!0;
  39.   }
  40.   else if(c=="/"){
  41.    li=s[i+1]; if(li=="/"){ ei=s.indexOf("\r",i); pci=s.indexOf("\n",i); if(pci>ei)ei=pci; i=ei; }
  42.    else if(li=="*"){ ei=s.indexOf("*/",i); i=ei+2; }
  43.    //put some regex handling here?
  44.   }
  45.   else if((li=lev.indexOf(c))>=0) sp=!0;
  46.  
  47.   if(sp){
  48.    if(li<3){
  49.     stack.push(ci=[c,i]); ALL.push(ci);
  50.    }else if(str||c==emc[stack[stL][0]]){
  51.      if(str)str=!1;
  52.      sp=stack.pop(); ci=[c,i,sp[1],ALL.indexOf(sp)]; sp[2]=i; sp[3]=ALL.length; ALL.push(ci);
  53.      if(c=="}"||c=="]"){ //is bracket, test for JSON
  54.       if(ci[3])pv=ALL[ci[3]-1];else pv=[0,0];
  55.  
  56.       sbs=s.substring(pv[1],sp[1]);
  57.       ei=sbs.lastIndexOf("="); pci=sbs.lastIndexOf(":");
  58.       if(pci>ei) ei=pci;
  59.       if(ei>=0||lev.indexOf(pv[0])<2){ //YAY possible JSON found
  60.        psj.push([sp[1],ci[1]]);
  61.       }
  62.      }
  63.     }else console.log("wrong order error");
  64.   }
  65.   i++;
  66.  }
  67.  
  68.  
  69.  i=psj.length; pv="";
  70.  while(i--){
  71.   c=psj[i]
  72.   sbs=s.substring(c[0],c[1]+1);
  73.   if(pv.indexOf(sbs)<0){
  74.    try{ ei=JSON.parse(sbs); jsa.unshift(ei); pv+=sbs; }catch(e){}
  75.   }
  76.  }
  77.  console.log("stack length :"+stack.length);
  78.  return {"jsa":jsa,"ALL":ALL};
  79. };
  80.  
  81.  
  82. self.XtracFromDoc=function(ds){
  83.  var scrps=parseHTML(ds).getElementsByTagName("script"),oa=[],i=0,L=scrps.length,cs,cv;
  84.  while(i<L){
  85.   cs=scrps[i];
  86.   if(cs.innerHTML.length){
  87.    cv=RipScript(cs.innerHTML);
  88.    cv.sei=i; //script element index
  89.    oa.push(cv);
  90.   }
  91.   i++;
  92.  }
  93.  return oa;
  94. };
  95.  
Add Comment
Please, Sign In to add comment