From b8cd053b0063664fc7e818bbe167f92d2bcec5fa Mon Sep 17 00:00:00 2001 From: nite Date: Sun, 24 Aug 2025 20:51:09 +1000 Subject: [PATCH] refactor: improve network event handling and cleanup of hidden elements in Bilinovel processing --- .vscode/launch.json | 2 +- downloader/bilinovel/bilinovel.go | 80 ++++++++++++++++++++++++------- test/bilinovel_test.go | 3 +- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 405989e..4de0887 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "args": [ "download", "-n", - "3095", + "3095" ] } ] diff --git a/downloader/bilinovel/bilinovel.go b/downloader/bilinovel/bilinovel.go index e467471..2d8f211 100644 --- a/downloader/bilinovel/bilinovel.go +++ b/downloader/bilinovel/bilinovel.go @@ -460,15 +460,16 @@ func (b *Bilinovel) processContentWithChromedp(htmlContent string) (string, erro var processedHTML string + // 设置网络事件监听 + networkEventChan := make(chan bool, 1) + var requestID string + // 执行处理任务 err = chromedp.Run(ctx, network.Enable(), - // 等待JavaScript执行完成 + // 设置网络事件监听器 chromedp.ActionFunc(func(ctx context.Context) error { - // 监听网络事件 - networkEventChan := make(chan bool, 1) - requestID := "" chromedp.ListenTarget(ctx, func(ev interface{}) { switch ev := ev.(type) { case *network.EventRequestWillBeSent: @@ -476,27 +477,74 @@ func (b *Bilinovel) processContentWithChromedp(htmlContent string) (string, erro requestID = ev.RequestID.String() } case *network.EventLoadingFinished: - if ev.RequestID.String() == requestID { - networkEventChan <- true + if ev.RequestID.String() == requestID && requestID != "" { + select { + case networkEventChan <- true: + default: + } } } }) - - go func() { - select { - case <-networkEventChan: - case <-time.After(30 * time.Second): - log.Println("Timeout waiting for external script") - case <-ctx.Done(): - log.Println("Context cancelled") - } - }() return nil }), + // 导航到本地文件 chromedp.Navigate("file://"+filepath.ToSlash(tempFilePath)), + // 等待页面加载完成 chromedp.WaitVisible(`#acontent`, chromedp.ByID), + + // 等待外部脚本加载或超时 + chromedp.ActionFunc(func(ctx context.Context) error { + select { + case <-networkEventChan: + log.Println("External script loaded successfully") + case <-time.After(10 * time.Second): + log.Println("Timeout waiting for external script, continuing anyway") + case <-ctx.Done(): + return ctx.Err() + } + return nil + }), + + // 遍历所有 #acontent 的子元素, 通过 window.getComputedStyle().display 检测是否是 none, 如果是 none 则从页面删除这个元素 + chromedp.ActionFunc(func(ctx context.Context) error { + // 执行JavaScript来移除display:none的元素 + var result string + err := chromedp.Evaluate(` + (function() { + const acontent = document.getElementById('acontent'); + if (!acontent) { + return 'acontent element not found'; + } + + let removedCount = 0; + const elements = acontent.querySelectorAll('*'); + + // 从后往前遍历,避免删除元素时影响索引 + for (let i = elements.length - 1; i >= 0; i--) { + const element = elements[i]; + const computedStyle = window.getComputedStyle(element); + + if (computedStyle.display === 'none') { + element.remove(); + removedCount++; + } + } + + return 'Removed ' + removedCount + ' hidden elements'; + })() + `, &result).Do(ctx) + + if err != nil { + log.Printf("Failed to remove hidden elements: %v", err) + return err + } + + log.Printf("Hidden elements removal result: %s", result) + return nil + }), + // 获取页面的HTML代码 chromedp.OuterHTML("html", &processedHTML, chromedp.ByQuery), ) diff --git a/test/bilinovel_test.go b/test/bilinovel_test.go index d1231fc..67151a7 100644 --- a/test/bilinovel_test.go +++ b/test/bilinovel_test.go @@ -43,10 +43,11 @@ func TestBilinovel_GetVolume(t *testing.T) { func TestBilinovel_GetChapter(t *testing.T) { bilinovel, err := bilinovel.New() + bilinovel.SetTextOnly(true) if err != nil { t.Fatalf("failed to create bilinovel: %v", err) } - chapter, err := bilinovel.GetChapter(1410, 52748, 52752) + chapter, err := bilinovel.GetChapter(3095, 154930, 154933) if err != nil { t.Fatalf("failed to get chapter: %v", err) }