electron-browser-shell icon indicating copy to clipboard operation
electron-browser-shell copied to clipboard

[chrome context menu] Action to save images

Open FormalSnake opened this issue 11 months ago • 11 comments

Currently there is no way to save images Screenshot 2025-01-10 at 10 15 23 The added action would increase the usefulness of this package.

FormalSnake avatar Jan 10 '25 10:01 FormalSnake

@FormalSnake If you have interest in contributing to the Electron project, this would be an approachable first-issue for doing so.

Chromium has a C++ class called content::RenderFrameHost which contains the functionality you're asking for: https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/render_frame_host.h;l=617-627;drc=61a5359d2a66fc8484ddcad5d7384c98e753f0da

Electron's WebFrameMain API is a JavaScript abstraction of RenderFrameHost. Two methods WebFrameMain.saveImageAt(x, y) and WebFrameMain.copyImageAt(x, y) can be added which call the same methods in RenderFrameHost. https://github.com/electron/electron/blob/main/shell/browser/api/electron_api_web_frame_main.cc

samuelmaddock avatar Jan 19 '25 23:01 samuelmaddock

@FormalSnake If you have interest in contributing to the Electron project, this would be an approachable first-issue for doing so.

Chromium has a C++ class called content::RenderFrameHost which contains the functionality you're asking for: https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/render_frame_host.h;l=617-627;drc=61a5359d2a66fc8484ddcad5d7384c98e753f0da

Electron's WebFrameMain API is a JavaScript abstraction of RenderFrameHost. Two methods WebFrameMain.saveImageAt(x, y) and WebFrameMain.copyImageAt(x, y) can be added which call the same methods in RenderFrameHost. https://github.com/electron/electron/blob/main/shell/browser/api/electron_api_web_frame_main.cc

Sure, i'll make the issue! But isn't an alternative to use the image address and download that in the background to "simulate" the effect?

FormalSnake avatar Jan 20 '25 17:01 FormalSnake

But isn't an alternative to use the image address and download that in the background to "simulate" the effect?

That's one possible approach, but will fail in cases where authentication http headers are required or for generated images. The benefit of using the internal methods is that it may also avoid redownloading the image.

samuelmaddock avatar Jan 20 '25 17:01 samuelmaddock

makes a lot of sense!

I made the issue, if there are any mistakes please tell me https://github.com/electron/electron/issues/45268

FormalSnake avatar Jan 20 '25 17:01 FormalSnake

lgtm! If you have the appetite to contribute the code changes as well, the process for building Chromium is fully documented here: https://www.electronjs.org/docs/latest/development/pull-requests

The whole set of changes would only require modifying a few files:

  1. Document new WebFrameMain methods in the docs which will generate the type definitions:
    https://github.com/electron/electron/blob/main/docs/api/web-frame-main.md
  2. Implement WebFrameMain::SaveImageAt and WebFrameMain::CopyImageAt. Then define JavaScript methods within WebFrameMain::FillObjectTemplate
    https://github.com/electron/electron/blob/main/shell/browser/api/electron_api_web_frame_main.cc
    https://github.com/electron/electron/blob/main/shell/browser/api/electron_api_web_frame_main.h
  3. Add tests to ensure images are downloaded and written to clipboard
    https://github.com/electron/electron/blob/main/spec/api-web-frame-main-spec.ts

samuelmaddock avatar Jan 20 '25 17:01 samuelmaddock

lgtm! If you have the appetite to contribute the code changes as well, the process for building Chromium is fully documented here: https://www.electronjs.org/docs/latest/development/pull-requests

The whole set of changes would only require modifying a few files:

  1. Document new WebFrameMain methods in the docs which will generate the type definitions: https://github.com/electron/electron/blob/main/docs/api/web-frame-main.md
  2. Implement WebFrameMain::SaveImageAt and WebFrameMain::CopyImageAt. Then define JavaScript methods within WebFrameMain::FillObjectTemplate https://github.com/electron/electron/blob/main/shell/browser/api/electron_api_web_frame_main.cc https://github.com/electron/electron/blob/main/shell/browser/api/electron_api_web_frame_main.h
  3. Add tests to ensure images are downloaded and written to clipboard https://github.com/electron/electron/blob/main/spec/api-web-frame-main-spec.ts

I would love to, but c++ development is not really my cup of tea. I will give it a try but I cannot guarantee anything

FormalSnake avatar Jan 20 '25 17:01 FormalSnake

I would love to, but c++ development is not really my cup of tea. I will give it a try but I cannot guarantee anything

The most painful aspect will be the initial build of Chromium (>35 million lines of code!). Happy to help if you get stuck at any point.

samuelmaddock avatar Jan 20 '25 17:01 samuelmaddock

I am stuck with building it :(

I do not have enough storage on my laptop, and I have no extra drives sadly

FormalSnake avatar Jan 20 '25 17:01 FormalSnake

and forgot to mention that i also have some issues with build-tools and nix lol

FormalSnake avatar Jan 20 '25 17:01 FormalSnake

I do not have enough storage on my laptop, and I have no extra drives sadly

Ah, that is a requirement unfortunately. It's close to 100GB between the source+build files and the git cache on my local disk.

samuelmaddock avatar Jan 20 '25 20:01 samuelmaddock

so apparently copyImageAt is already an existing function, so maybe that can be added for now before they add the saveImageAt (if that happens) https://github.com/electron/electron/issues/45268#issuecomment-2603293584

FormalSnake avatar Jan 21 '25 14:01 FormalSnake