0

Is it possible to process JavaScript in a Java application? Possibly utilizing WebKit libraries, or whatever browser libraries use to process JavaScript? A use case would be - how, in Java, can I determine the possible links this web page would go to?

<script>
function goToLink(){
 if(1==1){
   window.location='www.somesite.com'
 } else {
   window.location='www.nevergethere.com'
 }
}
</script>
<html>
<a href onClick='javascript:goToLink()'>CLICK HERE!!</a>
</html>

Typically, you would just search all of the code for a link regular expression, but you will never actually have the chance of going to: 'www.nevergetthere.com'

1

3 Answers 3

3

I've had some luck in tracing down JavaScript-based page links with HTML Unit. It basically acts like a browser that you have access to inside a Java program, so you can simulate a click on a link, and then figure out where it goes.

Sign up to request clarification or add additional context in comments.

Comments

3

You might be looking for Rhino.

2 Comments

He had also better be looking for a good HTML parser, because that's going to be Step 1 :-)
@Pointy Java has Regex, doesn't it? /sarcasm
1

If the objective is to look at a website, without knowing beforehand what the javascript or HTML will look like, and try to figure out where you would end up by clicking on various anchor tags, you could use something like WebDriver to actually load up the page in a browser (either real or virtual), click on various DOM elements, and see where you end up.

Web scraping is tricky business, though. There are a hundred little things that could make your code not read the page correctly. A hundred little expectations you may have that the website in question might not abide by.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.