Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: enable js for google search tool #367

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions google/search/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,13 @@ export async function search (

const encodedQuery = encodeURIComponent(query)
const searchUrl = `https://www.google.com/search?q=${encodedQuery}&udm=14`

const foundURLs = new Set<string>()
const results: Array<Promise<SearchResult | null>> = []

const page = await context.newPage()
const noJSPages = await Promise.all(
Array.from({ length: maxResults }, async () => {
const page = await context.newPage()
await page.addInitScript(() => {
// Disable JavaScript for the page
Object.defineProperty(navigator, 'javaScriptEnabled', { value: false })
Object.defineProperty(window, 'Function', { value: () => { } })
Object.defineProperty(window, 'eval', { value: () => { } })
})

return page
})
const pages = await Promise.all(
Array.from({ length: maxResults }, () => context.newPage())
)

try {
Expand All @@ -55,7 +45,7 @@ export async function search (
const url = $(element).attr('href') ?? ''
if ((url !== '') && !url.includes('youtube.com/watch?v') && !foundURLs.has(url)) {
foundURLs.add(url)
results.push(getMarkdown(noJSPages[results.length], url).then(content => {
results.push(getMarkdown(pages[results.length], url).then(content => {
return (content !== '') ? { url, content } : null
}))
}
Expand All @@ -68,13 +58,14 @@ export async function search (
} finally {
// Fire and forget page close so we can move on
void page.close()
void Promise.all(noJSPages.map(async p => { await p.close() }))
void Promise.all(pages.map(async p => { await p.close() }))
}
}

export async function getMarkdown (page: Page, url: string): Promise<string> {
try {
await page.goto(url, { timeout: 1000 })
await page.waitForLoadState('networkidle', { timeout: 1000 })
} catch (e) {
console.warn('slow page:', url)
}
Expand All @@ -83,7 +74,7 @@ export async function getMarkdown (page: Page, url: string): Promise<string> {
while (content === '') {
let fails = 0
try {
content = await page.content()
content = await page.evaluate(() => document.documentElement.outerHTML)
} catch (e) {
fails++
if (fails > 2) {
Expand Down Expand Up @@ -116,6 +107,7 @@ export async function getMarkdown (page: Page, url: string): Promise<string> {
continue;
}


$(selector).each(function () {
resp += turndownService.turndown($.html(this))
})
Expand Down