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

--selector-all to screenshot of all elements matching a selector #64

Closed
simonw opened this issue Apr 11, 2022 · 8 comments
Closed

--selector-all to screenshot of all elements matching a selector #64

simonw opened this issue Apr 11, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Apr 11, 2022

This then made me think that I actually want the same ability for just raw CSS selectors. But what should that look like?

Originally posted by @simonw in #43 (comment)

@simonw simonw added the enhancement New feature or request label Apr 11, 2022
@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

I found myself wanting to take a screenshot of e.g. all of the SVGs on a page - so a box that is drawn around all of those elements.

Right now the -s/--selector option takes just the first element that matches a selector - though you can use it multiple times to specify other selectors too:

shot-scraper https://simonwillison.net/ \
  -s '#bighead' -s .overband \
  -o bighead-multi-selector.png

I think a --selector-all option may be the best way to do this.

Then I could have --js-selector-all as the equivalent for this new feature:

@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

Open questions: How should that work in the multi YAML format?

@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

Relevant code:

def _selector_javascript(selectors, padding=0):
selector_to_shoot = "shot-scraper-{}".format(secrets.token_hex(8))
selector_javascript = textwrap.dedent(
"""
new Promise(takeShot => {
let padding = %s;
let minTop = 100000000;
let minLeft = 100000000;
let maxBottom = 0;
let maxRight = 0;
let els = %s.map(s => document.querySelector(s));

It uses querySelector() but for this feature it could use querySelectorAll().

@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

I like that --selector-all is to querySelectorAll() as --selector is to querySelector().

@simonw simonw changed the title Ability to take a screenshot of all elements matching a selector --selector-all to screenshot of all elements matching a selector Apr 11, 2022
@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

I'm going to add selector_all: and selectors_all: as the YAML keys, similar to selector: and selectors:.

(Going with Attorneys General pluralization here.)

@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

With the new feature:

shot-scraper https://simonwillison.net/ --selector-all '.day' --padding 10

simonwillison-net 11

@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

And for shot-scraper multi:

echo '- output: selectors-all.png
  url: https://simonwillison.net/
  selectors_all:
  - .day
  - .entry:nth-of-type(1)
  padding: 20' | shot-scraper multi -

Produced this:

selectors-all

@simonw
Copy link
Owner Author

simonw commented Apr 11, 2022

Also manually tested with this:

echo '- output: selector-all.png
  url: https://simonwillison.net/
  selector_all:
  - .day
  padding: 20' | shot-scraper multi -

@simonw simonw closed this as completed in 0d59ff5 Apr 11, 2022
simonw added a commit that referenced this issue Apr 11, 2022
Imitates --selector-all from #64
simonw added a commit that referenced this issue Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant