Executing javascript selectors on page source in string format
I am trying to develop and application with jsoup and java to drop some web pages. So I hope jsoup gets the page source first, and the page source will allow the below javascript to execute and return the result.
$("body, body *").each(function(i, val) {
// do something and something more
});
I am planning to use ScriptEngineManager to execute javascript code from Java.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
My question is how can I create / is there a way that a JS script takes a document (string) that jsoup returns, and do an operation on it just like a normal page.
So I hope it goes something like this:
// document is the Document object returned from the Jsoup or Document converted to string
var myfunction = function(document){
document.$("body, body *").each(function(i, val) {
// do something and something more
});
}
+3
source to share
1 answer
jQuery selector accepts html string and you can use context / scope to select only that variable. So you can do something like this as an example (using a variable as a context):
//a string of elements/html
var doc = '<html><body><ul><li>1</li><li>2</li><li>3</li></ul></body></html>';
$('li', doc).each(function() {
console.log(this); //iterates each 'li' within 'doc'
});
Or, in your case:
var myfunction = function(doc){
$("*", doc).each(function(i, val) {
// do something and something more
});
}
+1
source to share