67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/chromedp/chromedp"
|
|
)
|
|
|
|
func main() {
|
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
|
chromedp.Flag("headless", false), // Disable headless mode
|
|
chromedp.Flag("show-automation", true), // Show automation
|
|
chromedp.Flag("enable-automation", true), // Enable automation
|
|
chromedp.Flag("disable-extensions", false), // Enable extensions
|
|
chromedp.Flag("start-maximized", true), // Start maximized
|
|
)
|
|
|
|
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
|
defer cancel()
|
|
|
|
// Create context with logging
|
|
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithDebugf(log.Printf))
|
|
defer cancel()
|
|
|
|
// Set timeout
|
|
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
var htmlContent string
|
|
// Navigate and perform actions
|
|
err := chromedp.Run(ctx,
|
|
chromedp.Navigate("https://oceanofpdf.com"),
|
|
chromedp.WaitVisible(`input[class="sf_input"]`),
|
|
chromedp.SendKeys(`input[class="sf_input"]`, "programming"),
|
|
chromedp.WaitVisible(`button[type="submit"]`),
|
|
//chromedp.SendKeys(`input[class="sf_input"]`, kb.Enter),
|
|
chromedp.Click(`button[type="submit"]`),
|
|
chromedp.Sleep(10*time.Second), // Wait to see results
|
|
chromedp.OuterHTML("html", &htmlContent),
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Printf("HTML Content: %s", htmlContent)
|
|
}
|
|
|
|
// SearchBooks performs a search with JavaScript rendering
|
|
func SearchBooks(query string) error {
|
|
ctx, cancel := chromedp.NewContext(context.Background())
|
|
defer cancel()
|
|
|
|
return chromedp.Run(ctx,
|
|
// Navigate to search page
|
|
chromedp.Navigate(`https://oceanofpdf.com/?s=`+query),
|
|
// Wait for search input to be ready
|
|
chromedp.WaitVisible(`input[type="search"]`),
|
|
// Type search query
|
|
chromedp.SendKeys(`input[type="search"]`, query),
|
|
// Click search button
|
|
chromedp.Click(`button[type="submit"]`),
|
|
// Wait for results
|
|
chromedp.WaitVisible(`.post`),
|
|
)
|
|
}
|