@@ -4005,15 +4005,61 @@ int main(int argc, char *argv[])
40054005 });
40064006
40074007 server.start ();
4008-
4008+
40094009 debugLog (" MCP server ready. Starting pre-emptive CDP connection..." );
4010-
4011- // Pre-emptively start the connection process to reduce first-request latency
4012- QTimer::singleShot (500 , [&bridge]() {
4010+
4011+ // Pre-emptively start the connection process and auto-select a target to reduce first-request latency
4012+ QTimer::singleShot (500 , [&bridge, &cdpClient ]() {
40134013 debugLog (" Starting pre-emptive CDP connection attempt" );
4014- bridge.ensureConnected ();
4014+ if (bridge.ensureConnected ()) {
4015+ // Connection established, now try to auto-select a target
4016+ debugLog (" CDP connected, attempting to auto-select target..." );
4017+
4018+ QJsonArray targets = cdpClient->getAvailableTargets ();
4019+
4020+ if (!targets.isEmpty ()) {
4021+ // Try to find a target titled "Tau5" first
4022+ QString targetTitle;
4023+ for (const QJsonValue& value : targets) {
4024+ QJsonObject target = value.toObject ();
4025+ if (target[" type" ].toString () == " page" ) {
4026+ QString title = target[" title" ].toString ();
4027+ if (title == " Tau5" || title.contains (" Tau5" )) {
4028+ targetTitle = title;
4029+ break ;
4030+ }
4031+ }
4032+ }
4033+
4034+ // If no Tau5 target found, use the first page target
4035+ if (targetTitle.isEmpty ()) {
4036+ for (const QJsonValue& value : targets) {
4037+ QJsonObject target = value.toObject ();
4038+ if (target[" type" ].toString () == " page" ) {
4039+ targetTitle = target[" title" ].toString ();
4040+ break ;
4041+ }
4042+ }
4043+ }
4044+
4045+ if (!targetTitle.isEmpty ()) {
4046+ bool success = cdpClient->setTargetByTitle (targetTitle);
4047+ if (success) {
4048+ debugLog (QString (" Auto-connected to target: %1" ).arg (targetTitle));
4049+ } else {
4050+ debugLog (QString (" Failed to auto-connect to target: %1" ).arg (targetTitle));
4051+ }
4052+ } else {
4053+ debugLog (" No suitable page targets found for auto-connect" );
4054+ }
4055+ } else {
4056+ debugLog (" No targets available for auto-connect" );
4057+ }
4058+ } else {
4059+ debugLog (" CDP connection failed, skipping auto-connect" );
4060+ }
40154061 });
4016-
4062+
40174063 QObject::connect (cdpClient.get (), &CDPClient::connected, []() {
40184064 debugLog (" Successfully connected to Chrome DevTools" );
40194065 });
0 commit comments