Cheerio - Capturing value from a JS variable?

Hey, I’ve started using Cheerio for the first time and I’ve tried Googling around but can’t seem to find an answer to this, or if it’s even possible.

I want to grab the “int_id” value that’s inside the variable “mt1_instance”.
How can I do this? :smile:
I tried playing about using Cheerio, but I just keep crashing Postman so it’s really hard to trial and error this sort of thing.

image

HI @pfarrell

Nothing like a challenge… how’s this for you?

Using

const htmlResp = 
`<div id="app_body">
	<div id="app_content">
		<script language="javascript">
			// we need to providean access to the layout mode to the ...
			var bol_pos_mode = false;
			
			var mt1_instance = {
				int_id:1111911,
				str_hash:"gobbledigook",
				int_hub:1 };
				
			// preload functionality
			Ext.onReady(function() {
						});
			
			var DR_ALT_BANK_REFUNDS = 1;
		</script>
	</div>
</div>`; 

code

const htmlAsJson = xml2Json(htmlResp); console.log(htmlAsJson);
const scriptStr = htmlAsJson.div.div.script._; console.log(scriptStr);
// you could use a bit more short circuiting here if you were in any doubt 
// whether the expected elements are likely to be there or not

const matches = scriptStr.match(/var mt1_instance = (\{[\s\S]*?\});/); console.log(matches);
// regex looking for the lines that define mt1_instance 
// with capturing group for the {}
// if a match was found, it is in matches[0], 
// and the capturing group in matches[1]

try { // json parse the variable string
	var mt1_instance = matches && matches[0] && matches[1] 
		&& JSON.parse(matches[1]);
}catch(e){ console.log(e); /* ignore paring errors*/ }

if(!mt1_instance){
	console.log("could not parse, will try eval instead");
	matches && matches[0] && eval(matches[0]);
}

mt1_instance && console.log("mt1_instance.int_id: " + mt1_instance.int_id);

produces output

I would have preferred not to use the eval() but I used it because the variable does not parse as JSON.

It doesn’t use Cheerio.js and there will be other ways to manipulate the html, but I like keeping it in javascript as much as possible, hence I used xml2Json().

Hope that gives you some ideas to try out @pfarrell :cowboy_hat_face:

See short circuiting for more info.