Skip to content

Commit 2e026b1

Browse files
committed
Use API for example APP
1 parent dce8f53 commit 2e026b1

File tree

7 files changed

+1518
-131
lines changed

7 files changed

+1518
-131
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
package selineer.adapter;
3+
4+
import selineer.api.Browser;
5+
import selineer.api.BrowserContext;
6+
import selineer.api.BrowserType;
7+
import selineer.api.CDPSession;
8+
import selineer.api.Page;
9+
10+
import java.util.List;
11+
import java.util.function.Consumer;
12+
13+
import org.java_websocket.client.WebSocketClient;
14+
15+
public class BrowserAdapter implements Browser {
16+
private final String browserName;
17+
private WebSocketClient webSocketClient;
18+
19+
public BrowserAdapter(String browserName) {
20+
this.browserName = browserName;
21+
}
22+
23+
@Override
24+
public BrowserType browserType() {
25+
// TODO Auto-generated method stub
26+
return null;
27+
}
28+
29+
@Override
30+
public void close(CloseOptions options) {
31+
if (webSocketClient != null) {
32+
// Optionen für das Schließen könnten hier berücksichtigt werden
33+
if (options != null) {
34+
System.out.println("Optionen für close: " + options);
35+
}
36+
37+
webSocketClient.close();
38+
System.out.println("Browser geschlossen.");
39+
}
40+
}
41+
42+
@Override
43+
public List<BrowserContext> contexts() {
44+
// TODO Auto-generated method stub
45+
return null;
46+
}
47+
@Override
48+
public boolean isConnected() {
49+
// TODO Auto-generated method stub
50+
return false;
51+
}
52+
@Override
53+
public CDPSession newBrowserCDPSession() {
54+
// TODO Auto-generated method stub
55+
return null;
56+
}
57+
@Override
58+
public BrowserContext newContext(NewContextOptions options) {
59+
// TODO Auto-generated method stub
60+
return null;
61+
}
62+
63+
@Override
64+
public Page newPage(NewPageOptions options) {
65+
connectToBrowser();
66+
67+
// Optionen könnten hier auf eine spezifische Art und Weise angewandt werden
68+
if (options != null) {
69+
System.out.println("Optionen für newPage: " + options);
70+
}
71+
72+
return new PageAdapter(webSocketClient);
73+
}
74+
75+
@Override
76+
public void offDisconnected(Consumer<Browser> handler) {
77+
// TODO Auto-generated method stub
78+
79+
}
80+
@Override
81+
public void onDisconnected(Consumer<Browser> handler) {
82+
// TODO Auto-generated method stub
83+
84+
}
85+
@Override
86+
public void startTracing(Page page, StartTracingOptions options) {
87+
// TODO Auto-generated method stub
88+
89+
}
90+
@Override
91+
public byte[] stopTracing() {
92+
// TODO Auto-generated method stub
93+
return null;
94+
}
95+
@Override
96+
public String version() {
97+
// TODO Auto-generated method stub
98+
return null;
99+
}
100+
101+
///////////////////////////////////////////////////////////////////////////////////////////////
102+
/// HELPER / NOT API CONFORM METHODS
103+
///////////////////////////////////////////////////////////////////////////////////////////////
104+
105+
private void connectToBrowser() {
106+
try {
107+
String wsUrl = getWebSocketDebuggerUrl();
108+
if (wsUrl != null) {
109+
webSocketClient = new WebSocketClient(new java.net.URI(wsUrl)) {
110+
@Override
111+
public void onOpen(org.java_websocket.handshake.ServerHandshake handshake) {
112+
System.out.println("WebSocket-Verbindung hergestellt!");
113+
}
114+
115+
@Override
116+
public void onMessage(String message) {
117+
System.out.println("Nachricht vom Browser: " + message);
118+
}
119+
120+
@Override
121+
public void onClose(int code, String reason, boolean remote) {
122+
System.out.println("WebSocket-Verbindung geschlossen: " + reason);
123+
}
124+
125+
@Override
126+
public void onError(Exception ex) {
127+
ex.printStackTrace();
128+
}
129+
};
130+
webSocketClient.connect();
131+
while (!webSocketClient.isOpen()) {
132+
Thread.sleep(100);
133+
}
134+
System.out.println("WebSocket-Client ist aktiv.");
135+
}
136+
} catch (Exception e) {
137+
e.printStackTrace();
138+
}
139+
}
140+
141+
private String getWebSocketDebuggerUrl() {
142+
try {
143+
java.net.URL url = new java.net.URL("http://localhost:9222/json");
144+
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
145+
connection.setRequestMethod("GET");
146+
147+
try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(connection.getInputStream()))) {
148+
StringBuilder response = new StringBuilder();
149+
String line;
150+
while ((line = reader.readLine()) != null) {
151+
response.append(line);
152+
}
153+
154+
com.google.gson.JsonArray jsonArray = com.google.gson.JsonParser.parseString(response.toString()).getAsJsonArray();
155+
if (jsonArray.size() > 0) {
156+
com.google.gson.JsonObject firstPage = jsonArray.get(0).getAsJsonObject();
157+
return firstPage.get("webSocketDebuggerUrl").getAsString();
158+
}
159+
}
160+
} catch (Exception e) {
161+
e.printStackTrace();
162+
}
163+
return null;
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package selineer.adapter;
2+
3+
import selineer.api.Browser;
4+
import selineer.api.BrowserContext;
5+
import selineer.api.BrowserType;
6+
7+
import java.nio.file.Path;
8+
9+
public class BrowserTypeAdapter implements BrowserType {
10+
private final String name;
11+
12+
public BrowserTypeAdapter(String name) {
13+
this.name = name;
14+
}
15+
16+
@Override
17+
public Browser connect(String wsEndpoint) {
18+
// Implementiere Verbindung zu einem bestehenden Browser
19+
throw new UnsupportedOperationException("connect not implemented yet");
20+
}
21+
22+
@Override
23+
public Browser connect(String wsEndpoint, ConnectOptions options) {
24+
// Implementiere Verbindung zu einem bestehenden Browser mit Optionen
25+
throw new UnsupportedOperationException("connect not implemented yet");
26+
}
27+
28+
@Override
29+
public Browser connectOverCDP(String endpointURL) {
30+
// Implementiere Verbindung über CDP
31+
throw new UnsupportedOperationException("connectOverCDP not implemented yet");
32+
}
33+
34+
@Override
35+
public Browser connectOverCDP(String endpointURL, ConnectOverCDPOptions options) {
36+
// Implementiere Verbindung über CDP mit Optionen
37+
throw new UnsupportedOperationException("connectOverCDP not implemented yet");
38+
}
39+
40+
@Override
41+
public String executablePath() {
42+
// Gib den Pfad zum Browser-Executable zurück
43+
return null; // Passe dies an, falls erforderlich
44+
}
45+
46+
@Override
47+
public Browser launch() {
48+
return launch(null);
49+
}
50+
51+
@Override
52+
public Browser launch(LaunchOptions options) {
53+
// Starte den Browser mit den gegebenen Optionen
54+
System.out.println("Launching browser: " + name);
55+
return new BrowserAdapter(name); // BrowserAdapter muss implementiert sein
56+
}
57+
58+
@Override
59+
public BrowserContext launchPersistentContext(Path userDataDir) {
60+
return launchPersistentContext(userDataDir, null);
61+
}
62+
63+
@Override
64+
public BrowserContext launchPersistentContext(Path userDataDir, LaunchPersistentContextOptions options) {
65+
// Starte den Browser mit persistentem Kontext
66+
throw new UnsupportedOperationException("launchPersistentContext not implemented yet");
67+
}
68+
69+
@Override
70+
public String name() {
71+
return name;
72+
}
73+
}

0 commit comments

Comments
 (0)