Skip to content

Commit

Permalink
Fixed Nullpointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Sep 27, 2021
1 parent 2793def commit 468e321
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/osiris/headlessbrowser/HBrowser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.osiris.headlessbrowser;

import java.io.IOException;

/**
* Headless-Browser.
*
Expand All @@ -20,6 +22,14 @@ public NodeWindow openWindow() {
return new WindowBuilder(this).buildNodeJSWindow();
}

/**
* Shortcut for opening a window and loading a page into it. <br>
* See {@link #openWindow()} for details. <br>
*/
public NodeWindow openWindowAndLoad(String url) throws IOException {
return openWindow().load(url);
}

/**
* Returns the {@link WindowBuilder} to build custom window.
*/
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/osiris/headlessbrowser/NodeWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class NodeWindow implements AutoCloseable {
private Map<String, String> customHeaders;

public static void main(String[] args) throws IOException {
System.out.println(new HBrowser().openWindow().getDocument().outerHtml());
//new HBrowser().openWindow().executeJS("const hello = 'hi!';").executeJS("console.log(hello);");
System.out.println(new HBrowser().openWindow().load("example.com").getDocument().outerHtml());
}

public NodeWindow(HBrowser parentBrowser, boolean enableJavaScript, Map<String, String> customHeaders) {
Expand All @@ -29,8 +30,10 @@ public NodeWindow(HBrowser parentBrowser, boolean enableJavaScript, Map<String,
this.customHeaders = customHeaders;
try {
jsContext.npmInstall("puppeteer");
jsContext.executeJavaScript("const browser = await puppeteer.launch();\n" +
" const page = await browser.newPage();\n");
jsContext.executeJavaScript("" +
"const puppeteer = require('puppeteer');" +
"const browser = await puppeteer.launch();\n" +
"const page = await browser.newPage();\n");
} catch (Exception e) {
System.err.println("Error during install/start of Puppeteer! Details:");
throw new RuntimeException(e);
Expand All @@ -54,16 +57,13 @@ public NodeWindow load(String url) throws IOException {
else
headers = this.customHeaders;

jsContext.executeJavaScript("page.load('" + url + "');");
jsContext.executeJavaScript("await page.goto('" + url + "');");
return this;
}

public Document getDocument() throws IOException {
String rawHtml = jsContext.executeJavaScriptAndGetResult("" +
"var result = null;" +
"await page.content().then((html) => {" +
"result = html;" +
"});");
"var result = await page.evaluate(() => document.body.innerHTML);");
return Jsoup.parse(rawHtml);
}

Expand Down

0 comments on commit 468e321

Please sign in to comment.