Skip to content

Commit b976a21

Browse files
committed
Added more more methods to control the HWindow
1 parent 6c86c22 commit b976a21

File tree

4 files changed

+106
-33
lines changed

4 files changed

+106
-33
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.osiris.headlessbrowser</groupId>
88
<artifactId>Headless-Browser</artifactId>
9-
<version>ALPHA-0.0.1</version>
9+
<version>ALPHA-0.0.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Headless-Browser</name>

src/main/java/com/osiris/headlessbrowser/HBrowser.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ public HBrowser() {
1111
}
1212

1313
public HWindow openNewWindow() {
14-
return new HWindow();
14+
return new HWindowBuilder(this).build();
15+
}
16+
17+
/**
18+
* Returns the {@link HWindowBuilder} to build custom window.
19+
*/
20+
public HWindowBuilder openNewCustomWindow() {
21+
return new HWindowBuilder(this);
1522
}
1623

1724
public void closeWindow(HWindow HWindow) {

src/main/java/com/osiris/headlessbrowser/HWindow.java

+67-31
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,78 @@
99
import java.io.IOException;
1010
import java.net.URL;
1111
import java.nio.charset.StandardCharsets;
12+
import java.util.Map;
1213

1314
/**
1415
* Headless-Window.
1516
*
1617
* @author Osiris-Team
1718
*/
1819
public class HWindow implements AutoCloseable {
20+
// Options
21+
private HBrowser parentBrowser;
22+
private boolean enableJavaScript;
23+
private Map<String, String> customHeaders;
24+
1925
private final JSContext jsContext = new JSContext(this);
2026
private Document document;
2127
private String authority;
2228
private String javaScriptCode;
2329

30+
public HWindow(HBrowser parentBrowser, boolean enableJavaScript, Map<String, String> customHeaders) {
31+
this.parentBrowser = parentBrowser;
32+
this.enableJavaScript = enableJavaScript;
33+
this.customHeaders = customHeaders;
34+
}
35+
2436
public HWindow load(String url) throws IOException {
2537
if (!url.startsWith("http"))
2638
url = "https://" + url;
2739

40+
Map<String, String> headers = null;
41+
if (this.customHeaders == null)
42+
headers = new ChromeHeaders().get();
43+
else
44+
headers = this.customHeaders;
45+
2846
authority = new URL(url).getAuthority();
29-
document = Jsoup.connect(url).headers(new ChromeHeaders().get())
47+
document = Jsoup.connect(url).headers(headers)
3048
.get();
3149

32-
int scriptElements = 0;
33-
javaScriptCode = "";
34-
for (Element e :
35-
document.getElementsByTag("script")) {
36-
if (e.hasAttr("src")) {
37-
String externalScriptUrl = e.attr("src");
38-
if (!externalScriptUrl.startsWith("http")) {
39-
if (externalScriptUrl.startsWith("/"))
40-
externalScriptUrl = "https://" + authority + externalScriptUrl;
41-
else
42-
externalScriptUrl = "https://" + authority + "/" + externalScriptUrl;
50+
if (enableJavaScript){
51+
int scriptElements = 0;
52+
javaScriptCode = "";
53+
for (Element e :
54+
document.getElementsByTag("script")) {
55+
if (e.hasAttr("src")) {
56+
String externalScriptUrl = e.attr("src");
57+
if (!externalScriptUrl.startsWith("http")) {
58+
if (externalScriptUrl.startsWith("/"))
59+
externalScriptUrl = "https://" + authority + externalScriptUrl;
60+
else
61+
externalScriptUrl = "https://" + authority + "/" + externalScriptUrl;
62+
}
63+
64+
javaScriptCode = javaScriptCode + "\n" +
65+
"//\n" +
66+
"// Following lines are external JS-Code from " + externalScriptUrl + "\n" +
67+
"//\n" +
68+
"\n" +
69+
"" + new String(Jsoup.connect(externalScriptUrl).ignoreContentType(true)
70+
.get()
71+
.connection().response().bodyAsBytes(), StandardCharsets.UTF_8);
72+
} else {
73+
javaScriptCode = javaScriptCode + "\n" +
74+
"//\n" +
75+
"// Following lines are JS-Code from <script> number " + (scriptElements++) + "\n" +
76+
"//\n" +
77+
"\n" +
78+
"" + e.data();
4379
}
4480

45-
javaScriptCode = javaScriptCode + "\n" +
46-
"//\n" +
47-
"// Following lines are external JS-Code from " + externalScriptUrl + "\n" +
48-
"//\n" +
49-
"\n" +
50-
"" + new String(Jsoup.connect(externalScriptUrl).ignoreContentType(true)
51-
.get()
52-
.connection().response().bodyAsBytes(), StandardCharsets.UTF_8);
53-
} else {
54-
javaScriptCode = javaScriptCode + "\n" +
55-
"//\n" +
56-
"// Following lines are JS-Code from <script> number " + (scriptElements++) + "\n" +
57-
"//\n" +
58-
"\n" +
59-
"" + e.data();
81+
// Execute code
82+
jsContext.eval(javaScriptCode);
6083
}
61-
62-
// Execute code
63-
jsContext.eval(javaScriptCode);
6484
}
6585
return this;
6686
}
@@ -83,6 +103,22 @@ public String getJavaScriptCode() {
83103

84104
@Override
85105
public void close() {
86-
//TODO
106+
jsContext.close();
107+
}
108+
109+
public HBrowser getParentBrowser() {
110+
return parentBrowser;
111+
}
112+
113+
public void setParentBrowser(HBrowser parentBrowser) {
114+
this.parentBrowser = parentBrowser;
115+
}
116+
117+
public boolean isEnableJavaScript() {
118+
return enableJavaScript;
119+
}
120+
121+
public void setEnableJavaScript(boolean enableJavaScript) {
122+
this.enableJavaScript = enableJavaScript;
87123
}
88124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.osiris.headlessbrowser;
2+
3+
import java.util.Map;
4+
5+
public class HWindowBuilder {
6+
private HBrowser parentBrowser;
7+
private boolean enableJavaScript = true;
8+
private Map<String, String> customHeaders = null;
9+
10+
11+
public HWindowBuilder(HBrowser parentBrowser) {
12+
this.parentBrowser = parentBrowser;
13+
}
14+
15+
16+
public HWindow build(){
17+
return new HWindow(this.parentBrowser, this.enableJavaScript, this.customHeaders);
18+
}
19+
20+
public HWindowBuilder customHeaders(Map<String, String> customHeaders){
21+
this.customHeaders = customHeaders;
22+
return this;
23+
}
24+
25+
public HWindowBuilder enableJavaScript(boolean enableJavaScript){
26+
this.enableJavaScript = enableJavaScript;
27+
return this;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)