-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsites.go
64 lines (52 loc) · 1.87 KB
/
websites.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package webshot
import (
"log"
"time"
)
// Screenshot is the default screenshotter which can take the screenshot of webpages
func (p *Webshot) Screenshot(requestURL string, removeModals bool, sleepInterval time.Duration) ([]byte, error) {
p.Webdriver.Get(requestURL)
if removeModals {
// Inject JavaScript to handle modals
js := `
// Function to remove modals and overlays
function removeModalsAndOverlays() {
// Select and remove modal elements
document.querySelectorAll('[role="dialog"], .modal, .popup, [data-modal]').forEach(el => el.remove());
// Select and remove overlay elements
document.querySelectorAll('.overlay, .backdrop, [class*="overlay"], [class*="backdrop"]').forEach(el => {
el.remove(); // Attempt to completely remove the overlay element
});
// If overlays are not removable, hide them forcefully
document.querySelectorAll('*').forEach(el => {
const style = window.getComputedStyle(el);
if (style.position === 'fixed' && style.zIndex > 1000) {
el.style.display = 'none'; // Hide high z-index elements that could act as overlays
}
});
}
// Execute removal on initial load
removeModalsAndOverlays();
// Set up a MutationObserver to handle dynamically added modals and overlays
const observer = new MutationObserver(() => {
removeModalsAndOverlays();
});
observer.observe(document.body, { childList: true, subtree: true });
console.log("Modal and overlay removal script injected.");
`
_, err := p.Webdriver.ExecuteScript(js, nil)
if err != nil {
log.Printf("Error injecting JavaScript to remove modals: %v", err)
} else {
log.Println("JavaScript to remove modals executed successfully.")
}
}
time.Sleep(sleepInterval)
imgByte, err := p.Webdriver.Screenshot()
if err != nil {
return nil, err
}
defer p.Service.Stop()
defer p.Webdriver.Quit()
return imgByte, nil
}