Skip to content

Commit

Permalink
Added more more methods to control the HWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Sep 5, 2021
1 parent 6c86c22 commit b976a21
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.osiris.headlessbrowser</groupId>
<artifactId>Headless-Browser</artifactId>
<version>ALPHA-0.0.1</version>
<version>ALPHA-0.0.2</version>
<packaging>jar</packaging>

<name>Headless-Browser</name>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/osiris/headlessbrowser/HBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ public HBrowser() {
}

public HWindow openNewWindow() {
return new HWindow();
return new HWindowBuilder(this).build();
}

/**
* Returns the {@link HWindowBuilder} to build custom window.
*/
public HWindowBuilder openNewCustomWindow() {
return new HWindowBuilder(this);
}

public void closeWindow(HWindow HWindow) {
Expand Down
98 changes: 67 additions & 31 deletions src/main/java/com/osiris/headlessbrowser/HWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,78 @@
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
* Headless-Window.
*
* @author Osiris-Team
*/
public class HWindow implements AutoCloseable {
// Options
private HBrowser parentBrowser;
private boolean enableJavaScript;
private Map<String, String> customHeaders;

private final JSContext jsContext = new JSContext(this);
private Document document;
private String authority;
private String javaScriptCode;

public HWindow(HBrowser parentBrowser, boolean enableJavaScript, Map<String, String> customHeaders) {
this.parentBrowser = parentBrowser;
this.enableJavaScript = enableJavaScript;
this.customHeaders = customHeaders;
}

public HWindow load(String url) throws IOException {
if (!url.startsWith("http"))
url = "https://" + url;

Map<String, String> headers = null;
if (this.customHeaders == null)
headers = new ChromeHeaders().get();
else
headers = this.customHeaders;

authority = new URL(url).getAuthority();
document = Jsoup.connect(url).headers(new ChromeHeaders().get())
document = Jsoup.connect(url).headers(headers)
.get();

int scriptElements = 0;
javaScriptCode = "";
for (Element e :
document.getElementsByTag("script")) {
if (e.hasAttr("src")) {
String externalScriptUrl = e.attr("src");
if (!externalScriptUrl.startsWith("http")) {
if (externalScriptUrl.startsWith("/"))
externalScriptUrl = "https://" + authority + externalScriptUrl;
else
externalScriptUrl = "https://" + authority + "/" + externalScriptUrl;
if (enableJavaScript){
int scriptElements = 0;
javaScriptCode = "";
for (Element e :
document.getElementsByTag("script")) {
if (e.hasAttr("src")) {
String externalScriptUrl = e.attr("src");
if (!externalScriptUrl.startsWith("http")) {
if (externalScriptUrl.startsWith("/"))
externalScriptUrl = "https://" + authority + externalScriptUrl;
else
externalScriptUrl = "https://" + authority + "/" + externalScriptUrl;
}

javaScriptCode = javaScriptCode + "\n" +
"//\n" +
"// Following lines are external JS-Code from " + externalScriptUrl + "\n" +
"//\n" +
"\n" +
"" + new String(Jsoup.connect(externalScriptUrl).ignoreContentType(true)
.get()
.connection().response().bodyAsBytes(), StandardCharsets.UTF_8);
} else {
javaScriptCode = javaScriptCode + "\n" +
"//\n" +
"// Following lines are JS-Code from <script> number " + (scriptElements++) + "\n" +
"//\n" +
"\n" +
"" + e.data();
}

javaScriptCode = javaScriptCode + "\n" +
"//\n" +
"// Following lines are external JS-Code from " + externalScriptUrl + "\n" +
"//\n" +
"\n" +
"" + new String(Jsoup.connect(externalScriptUrl).ignoreContentType(true)
.get()
.connection().response().bodyAsBytes(), StandardCharsets.UTF_8);
} else {
javaScriptCode = javaScriptCode + "\n" +
"//\n" +
"// Following lines are JS-Code from <script> number " + (scriptElements++) + "\n" +
"//\n" +
"\n" +
"" + e.data();
// Execute code
jsContext.eval(javaScriptCode);
}

// Execute code
jsContext.eval(javaScriptCode);
}
return this;
}
Expand All @@ -83,6 +103,22 @@ public String getJavaScriptCode() {

@Override
public void close() {
//TODO
jsContext.close();
}

public HBrowser getParentBrowser() {
return parentBrowser;
}

public void setParentBrowser(HBrowser parentBrowser) {
this.parentBrowser = parentBrowser;
}

public boolean isEnableJavaScript() {
return enableJavaScript;
}

public void setEnableJavaScript(boolean enableJavaScript) {
this.enableJavaScript = enableJavaScript;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/osiris/headlessbrowser/HWindowBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.osiris.headlessbrowser;

import java.util.Map;

public class HWindowBuilder {
private HBrowser parentBrowser;
private boolean enableJavaScript = true;
private Map<String, String> customHeaders = null;


public HWindowBuilder(HBrowser parentBrowser) {
this.parentBrowser = parentBrowser;
}


public HWindow build(){
return new HWindow(this.parentBrowser, this.enableJavaScript, this.customHeaders);
}

public HWindowBuilder customHeaders(Map<String, String> customHeaders){
this.customHeaders = customHeaders;
return this;
}

public HWindowBuilder enableJavaScript(boolean enableJavaScript){
this.enableJavaScript = enableJavaScript;
return this;
}

}

0 comments on commit b976a21

Please sign in to comment.