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
|
# File 'lib/headless_browser_tool/tools/screenshot_tool.rb', line 19
def execute(filename: nil, highlight_selectors: [], annotate: false, full_page: false)
screenshots_dir = "./.hbt/screenshots"
timestamp = Time.now.strftime("%Y%m%d_%H%M%S_%L")
if filename
base_name = filename.sub(/\.png$/i, "")
filename = "#{base_name}_#{timestamp}.png"
else
filename = "screenshot_#{timestamp}.png"
end
file_path = File.join(screenshots_dir, filename)
inject_highlight_script(highlight_selectors, annotate) if highlight_selectors.any? || annotate
if full_page
take_full_page_screenshot(file_path)
else
browser.save_screenshot(file_path)
end
browser.execute_script("document.querySelectorAll('.ai-highlight').forEach(el => el.remove());")
file_size = File.size(file_path)
{
file_path: file_path,
filename: filename,
file_size: file_size,
file_size_human: "#{(file_size / 1024.0).round(2)} KB",
timestamp: timestamp,
full_page: full_page,
highlighted_elements: highlight_selectors.size,
annotated: annotate,
url: browser.current_url,
title: browser.title
}
end
|