Parth Mane Parth Mane - 2 months ago 13
Javascript Question

I want to obtain a certain part of text from a large webpage using Javascript, how do I?

There is a certain webpage which randomly generates a number, for example

"Frequency : 21"
. I am trying to create a script which takes the number,
21
, and compares it to another variable, then to an
if else
function. Basically, I've completed most of it, but I can't obtain the number
21
. And since it is random, I can't put in a fixed value.

Can anyone help me out?

My code goes like:

setTimeout(MyFunction,5000)

function MyFunction(level,legmin) {
var level = x
var legmin = 49
if (level <= legmin) {
location.reload(true)
}
else {
alert("Met requirements.")
}


where the address of the text I want is:

html>body>div#container>div#contentContainer>div#content>
div#scroll>div#scrollContent>div>div>div#pkmnappear>form>p (x in the code above).

Answer

A quick-n-dirty solution without regex.

var lookFor = "Frequency : ";
var text = document.querySelector("#pkmnappear>form>p").textContent;
var level = text.substr(text.indexOf(lookFor) + lookFor.length).split(" ")[0];

This assumes the number will be followed by a space