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

It would be great to have an omit_background option so you can take transparent PNG screenshots #108

Closed
palewire opened this issue Apr 20, 2023 · 4 comments · Fixed by #109
Labels
bug Something isn't working enhancement New feature or request

Comments

@palewire
Copy link
Contributor

It's in the options, but I don't think it's available.

https://playwright.dev/python/docs/api/class-locator#locator-screenshot-option-omit-background

@simonw
Copy link
Owner

simonw commented Apr 27, 2023

Seeing an error with this in CI: https://github.com/simonw/shot-scraper/actions/runs/4815473826/jobs/8574190652

Screenshot of '.day' on 'https://simonwillison.net/' written to 'examples/selector-all.png'
Screenshot of 'https://simonwillison.net/' written to 'examples/simon-narrow.png'
Error: TypeError: Cannot set properties of undefined (setting 'backgroundColor')
    at eval (eval at evaluate (:195:30), <anonymous>:1:60)
    at eval (<anonymous>)
    at UtilityScript.evaluate (<anonymous>:195:30)
    at UtilityScript.<anonymous> (<anonymous>:1:44)

@simonw simonw reopened this Apr 27, 2023
@simonw simonw added bug Something isn't working enhancement New feature or request labels Apr 27, 2023
@simonw
Copy link
Owner

simonw commented Apr 27, 2023

Looks like the error is from:

# Omit background
shot-scraper https://simonwillison.net/ \
-h 800 -o examples/simonwillison-transparent.png \
--javascript "document.getElementsByTagName('body').style.backgroundColor='transparent'" \
--omit-background

That gives me the same error locally too:

% shot-scraper https://simonwillison.net/ \
  -h 800 -o examples/simonwillison-transparent.png \
  --javascript "document.getElementsByTagName('body').style.backgroundColor='transparent'" \
  --omit-background
Error: TypeError: Cannot set properties of undefined (setting 'backgroundColor')
    at eval (eval at evaluate (:197:30), <anonymous>:1:60)
    at eval (<anonymous>)
    at UtilityScript.evaluate (<anonymous>:197:30)
    at UtilityScript.<anonymous> (<anonymous>:1:44)

@simonw
Copy link
Owner

simonw commented Apr 27, 2023

It's because document.getElementsByTagName('body') returns an array-like collection. I'm going to try document.querySelector('body') instead.

@simonw
Copy link
Owner

simonw commented Apr 27, 2023

This worked without an error:

shot-scraper https://simonwillison.net/ \
  -h 800 -o examples/simonwillison-transparent.png \
  --javascript "document.querySelector('body').style.backgroundColor='transparent'" \
  --omit-background

And produced this image (which has a transparent background, though you can't see that on GitHub Issues because the background here is white too):

simonwillison-transparent

simonw added a commit that referenced this issue Apr 27, 2023
simonw added a commit that referenced this issue Apr 27, 2023
@simonw simonw closed this as completed Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants