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

Add console.screenshot #160

Open
dcrousso opened this issue Jun 28, 2019 · 4 comments
Open

Add console.screenshot #160

dcrousso opened this issue Jun 28, 2019 · 4 comments

Comments

@dcrousso
Copy link
Contributor

dcrousso commented Jun 28, 2019

WebKit has added support for a way to log images of Nodes, as well as the contents of the viewport, through console.screenshot (when Web Inspector is open).

console.screenshot(); // will log an image of the viewport
console.screenshot(document); // will log an image of the entire document, including below-the-fold content
console.screenshot(document.images[0]); // will log an image of a specific element

There are also plans to expand support for other object types, like ImageData [1], ImageBitmap [1], and instances all the various <canvas> context types (e.g. CanvasRenderingContext2D) [2].

[Exposed=(Window,Worker,Worklet)]
namespace console {
    void screenshot(any... data);
};

If the first argument is not able to take a "screenshot" (or generate some image), console.screenshot will default to logging an image of the viewport.

All arguments are currently logged before the image is shown.

Screen Shot 2019-06-27 at 19 59 30

This could be further extended to provide the ability to generate images from arbitrary content, like a dataURL string [3] or a detached/fragment DOM tree.

[1]: <https://webkit.org/b/199304> Web Inspector: support console.screenshot with ImageData and ImageBitmap
[2]: <https://webkit.org/b/199305> Web Inspector: support console.screenshot with subclasses of CanvasRenderingContext
[3]: <https://webkit.org/b/199307> Web Inspector: support console.screenshot with dataURL strings

@domenic
Copy link
Member

domenic commented Jun 28, 2019

Speaking purely as a web developer, this seems awesome; I hope other engines watching this repo are interested :).

@nchevobbe
Copy link

Firefox console has a :screenshot command which looks similar to this, even if the output is clipboard/file instead of the console output.
This would be nice indeed to have a standard way to do this.

I'd say that it would still be important for the user to directly get the screenshot to clipboard/disk (and not have to perform manual operation from the console output to actually be able to use the image), but that brings question of what to do when the console is not opened yet.
I can see this causing 2 issues:

  • keeping images in console cache might increase memory consumption (think about a console.screenshot in a loop for example)
  • if we have options to save images to disk, I guess we don't want them to end up on end user's devices

I also wonder how would that work for Node, since many terminal can't display images (print the data URI ?)


You can see the supported :screenshot options in https://developer.mozilla.org/en-US/docs/Tools/Web_Console/Helpers (or read this nice article from Eric Meyer about it) https://meyerweb.com/eric/thoughts/2018/08/24/firefoxs-screenshot-command-2018/).
Some of the key options we heard people like to use:

  • take a screenshot of the full page (instead of only the current viewport)
  • set dpr to mimick retina screens (or just get a nice crisp screenshot)
  • save screenshot to clipboard for immediate use (to paste to gfx software, or use in webapp like slack, …)

@dcrousso
Copy link
Contributor Author

@nchevobbe right-clicking (context menu) on the image will show a "Save Image" option that allows the user to save the image to disk.

Right now, console.screenshot is a no-op when Web Inspector is not open. I was considering having it behave like console.log (just log the values without taking a screenshot) when Web Inspector is not open, but given that the expectation of console.screenshot is an image, I thought that that could be confusing.

Images are preserved for the Web Inspector session (just like all other console methods), so closing and reopening will clear them.

It is currently possible to get an image of the full page by using console.screenshot(document).

We have a "similar" way to access console.screenshot via the Console "Command Line" API by just omitting the console. part (e.g. screenshot(...)).

I do like the idea of being able to copy it to the clipboard though 😁

@CleoMenezesJr
Copy link

3 years later and I would still like to know if there is any management to take the screenshot and save at the same time. lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants