samedi 21 février 2015

how to result the contents of a javascript variable using cheerio (jquery like selectors, but no dom)


examplefile.html



<script type="text/javascript">//.... more js</script>
<script type="text/javascript">//.... more js</script>
<script type="text/javascript">var foo = {"b":"bar","c":"cat"}</script>
<script type="text/javascript">//.... more js</script>
<script type="text/javascript">//.... more js</script>
<script type="text/javascript">//.... more js</script>


desired console result



> var result = $('script').<some_selection_thingy>
result = {"b":"bar","c":"cat"}


Let me explain a little bit... By I mean that my questions is - a) how do I select the array object with has the contents 'var foo' b) how do I get the contents of the var foo variable so that I can import that information into a local json variable for further processing.


when you run $('script') in the console, jquery returns an array.



> $('script')
[<script type="text/javascript">//.... more js</script>,<script type="text/javascript">//.... more js</script>,<script type="text/javascript">var foo = {"b":"bar","c":"cat"}</script>,<script type="text/javascript">...</script>]


Because this is cheerio not actually jquery, the dom isn't loaded so I can't just do $(foo) . There is an alternative that I can use jsdom instead of cheerio but I've read in other stackoverflow responses (while researching this question) that it's less performant so I'd prefer to learn the correct jquery selectors I need to scoop out this variable.





Aucun commentaire:

Enregistrer un commentaire