JavaScript Wiki
  • The following code will deep traverse through every elements in an object to search for text in each element (after converted by the element's toString() method) and in the element's key, returning list of matched elements,
    ((v,r)=>{let o=Object.prototype.toString,a=Array.prototype.toString,m,i,s=typeof"",l=[],f=(v,kk)=>{v==null||v.toString==o||v.toString==a||(i=v.toString().search(r))<0||l.push([i].concat(kk,[v])); if(typeof v!=s)for(let k in v)try{m=k.match(r),m!=null&&m.length>0&&l.push([m].concat(kk,k)),v==v[k]||f(v[k],kk.concat(k))}catch{}}; f(v,[]);return l})(ytplayer, /videoplayback/i)
    
    . The above example search for the text "videoplayback" in object ytplayer (normally found in YouTube's video page). The format of returned result is like
    [
       [
          offset of matched text,
          elem key 1, elem key 2, ..., elem key n,
          object of matched element
       ],
       [...],
       [...],
       ...
    ]
    
    where "elem key 1, ..., elem key n" is the path (in the object) to the matched element. If the matched text is element's key, the "offset of matched text" will be an array (as return by String.prototype.match()) instead of integer. Following screen demonstrate the output Search js obj