MCPcopy
hub / github.com/steel-dev/steel-browser / handleSearch

Function handleSearch

api/src/modules/actions/actions.controller.ts:340–451  ·  view source on GitHub ↗
(
  sessionService: SessionService,
  browserService: CDPService,
  request: SearchRequest,
  reply: FastifyReply,
)

Source from the content-addressed store, hash-verified

338};
339
340export const handleSearch = async (
341 sessionService: SessionService,
342 browserService: CDPService,
343 request: SearchRequest,
344 reply: FastifyReply,
345) => {
346 const startTime = Date.now();
347 let times: Record<string, number> = {};
348 const { query, proxyUrl, logUrl } = request.body;
349
350 let proxy: IProxyServer | null = null;
351 let context: BrowserContext | null = null;
352
353 try {
354 if (proxyUrl) {
355 proxy = await sessionService.proxyFactory(proxyUrl);
356 await proxy.listen();
357 }
358 times.proxyTime = Date.now() - startTime;
359
360 let page: Page;
361
362 if (!browserService.isRunning()) {
363 await browserService.launch();
364 }
365
366 if (proxy) {
367 // If a proxy is used, we proceed with browser navigation; implementing proxy-aware Node fetch
368 // would require an HTTP agent and is outside current scope.
369 context = await browserService.createBrowserContext(proxy.url);
370 page = await context.newPage();
371 times.proxyPageTime = Date.now() - startTime - times.proxyTime;
372 } else {
373 page = await browserService.getPrimaryPage();
374 times.pageTime = Date.now() - startTime - times.proxyTime;
375 }
376
377 await page.evaluate(() => {
378 (window as any).__name = (func: Function) => func;
379 });
380
381 // Go to Brave
382 await page.goto(`https://search.brave.com/search?q=${encodeURIComponent(query)}`, {
383 waitUntil: "networkidle2",
384 });
385
386 // Wait for results to load
387 await page.waitForSelector("#results");
388
389 // Scrape results
390 const results = await page.evaluate(() => {
391 const items = document.querySelectorAll("div.snippet");
392
393 return Array.from(items)
394 .map((item) => {
395 if (
396 [
397 "llm-snippet",

Callers 1

routesFunction · 0.85

Calls 9

updateLogFunction · 0.85
getErrorsFunction · 0.85
isRunningMethod · 0.80
createBrowserContextMethod · 0.80
getPrimaryPageMethod · 0.80
sendMethod · 0.80
listenMethod · 0.65
closeMethod · 0.65
launchMethod · 0.45

Tested by

no test coverage detected