-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce52547
commit 9976369
Showing
11 changed files
with
175 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.osiris.headlessbrowser; | ||
|
||
/** | ||
* Headless-Browser. | ||
* | ||
* @author Osiris-Team | ||
*/ | ||
public class HBrowser { | ||
|
||
public HBrowser() { | ||
} | ||
|
||
public HWindow openNewWindow() { | ||
return new HWindow(); | ||
} | ||
|
||
public void closeWindow(HWindow HWindow) { | ||
HWindow.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.osiris.headlessbrowser; | ||
|
||
|
||
import com.osiris.headlessbrowser.data.chrome.ChromeHeaders; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Headless-Window. | ||
* | ||
* @author Osiris-Team | ||
*/ | ||
public class HWindow implements AutoCloseable { | ||
private final JSContext jsContext = new JSContext(this); | ||
private Document document; | ||
private String authority; | ||
private String javaScriptCode; | ||
|
||
public HWindow load(String url) throws IOException { | ||
if (!url.startsWith("http")) | ||
url = "https://" + url; | ||
|
||
authority = new URL(url).getAuthority(); | ||
document = Jsoup.connect(url).headers(new ChromeHeaders().get()) | ||
.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; | ||
} | ||
|
||
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); | ||
} | ||
return this; | ||
} | ||
|
||
public Document getDocument() { | ||
return document; | ||
} | ||
|
||
public JSContext getJsContext() { | ||
return jsContext; | ||
} | ||
|
||
public String getAuthority() { | ||
return authority; | ||
} | ||
|
||
public String getJavaScriptCode() { | ||
return javaScriptCode; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
//TODO | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/java/com/osiris/headlessbrowser/HeadlessBrowser.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/java/com/osiris/headlessbrowser/HeadlessWindow.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/osiris/headlessbrowser/data/chrome/ChromeHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.osiris.headlessbrowser.data.chrome; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ChromeHeaders { | ||
|
||
|
||
public Map<String, String> get() { | ||
Map<String, String> headers = new HashMap<>(); | ||
headers.put("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); | ||
headers.put("accept-encoding", "gzip, deflate, br"); | ||
headers.put("accept-language", "en-US,en;"); | ||
headers.put("cache-control", "max-age=0"); | ||
headers.put("referer", "https://google.com/"); | ||
headers.put("sec-ch-ua", "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""); | ||
headers.put("sec-ch-ua-mobile", "?1"); | ||
headers.put("sec-fetch-dest", "document"); | ||
headers.put("sec-fetch-mode", "navigate"); | ||
headers.put("sec-fetch-site", "same-origin"); | ||
headers.put("sec-fetch-user", "?1"); | ||
headers.put("upgrade-insecure-requests", "1"); | ||
headers.put("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36"); | ||
return headers; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/com/osiris/headlessbrowser/HBrowserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.osiris.headlessbrowser; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
class HBrowserTest { | ||
|
||
@Test | ||
void test() throws IOException { | ||
HBrowser hBrowser = new HBrowser(); | ||
HWindow hWindow = hBrowser.openNewWindow().load("https://wikipedia.org"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
src/test/java/com/osiris/headlessbrowser/javascript/JS_API_ConsoleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
package com.osiris.headlessbrowser.javascript; | ||
|
||
import com.osiris.headlessbrowser.HeadlessBrowser; | ||
import com.osiris.headlessbrowser.HBrowser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class JS_API_ConsoleTest { | ||
|
||
@Test | ||
void test() throws IOException { | ||
new HeadlessBrowser().openNewWindow().getJsContext() | ||
new HBrowser().openNewWindow().getJsContext() | ||
.eval("console.log('hello');"); | ||
} | ||
} |