Skip to content

Remote issues with clipboard and NativeImage #8151

Closed

Description

@bahmutov
  • Electron version: 1.4.10
  • Operating system: MacOS

Expected behavior

Captured image from webview is copied into clipboard

Actual behavior

Crashes with error message

Uncaught TypeError: Error processing argument at index 0, conversion failure from [object Object]

How to reproduce

I am capturing a region of a webview object and then saving as an image (works pretty ok, although sometimes the image is empty), then I am trying to copy the NativeImage into clipboard, but it crashes

webview.capturePage(rect, image => {
              const quality = 90
              const filename = 'image.jpg'
              const fullPath = path.join(__dirname, filename)
              fs.writeFile(fullPath, image.toJPEG(quality), (err) => {                
                 // TODO: retry save if the image is 0 size
                clipboard.writeImage(image)
              })
})

I can see the image on the disk, but the clipboard has never received it, always crashing

Activity

  1. kevinsawicki commented on Dec 6, 2016

    @kevinsawicki
    Contributor

    Uncaught TypeError: Error processing argument at index 0, conversion failure from [object Object]

    The argument to writeImage needs to be a NativeImage or String file path, image.toJPEG() returns a Buffer.

    Use nativeImage.fromBuffer(image.toJPEG(quality)) instead.

    http://electron.atom.io/docs/api/native-image/

  2. bahmutov commented on Dec 6, 2016

    @bahmutov
    Author

    Yes, so in the code example, webview.capturePage returns NativeImage, which I am passing directly into clipboard.writeImage(image). Conversion to JPEG is only done to save the image to disk to make sure the file is valid.

  3. kevinsawicki commented on Dec 6, 2016

    @kevinsawicki
    Contributor

    Conversion to JPEG is only done to save the image to disk to make sure the file is valid.

    Whoops, sorry, misread that.

  4. kevinsawicki commented on Dec 6, 2016

    @kevinsawicki
    Contributor

    You can wrap it in a nativeImage.createFromBuffer(image.getBitmap()) to workaround this.

    This issue is happening because webview.capturePage is actually a remote method doing IPC so the value it calls back with is a remote nativeImage object. It does not seem like it is currently supported to pass these back into subsequent API calls.

  5. changed the title [-]Cannot copy captured NativeImage to clipboard (crashes)[/-] [+]Cannot copy remote NativeImage to clipboard[/+] on Dec 6, 2016
  6. bahmutov commented on Dec 6, 2016

    @bahmutov
    Author

    so far no luck, does not seem the image is placed into the clipboard. Is there a good tool to inspect the clipboard for Mac?

  7. kevinsawicki commented on Dec 6, 2016

    @kevinsawicki
    Contributor

    Is there a good tool to inspect the clipboard for Mac?

    I usually just launch TextEdit since it supports pasting images.

    Can you try clipboard.writeImage(nativeImage.createFromBuffer(image.toJPEG(1000))) instead?

  8. kevinsawicki commented on Dec 6, 2016

    @kevinsawicki
    Contributor

    clipboard.writeImage(nativeImage.createFromBuffer(image.toPNG())) works for me too.

  9. bahmutov commented on Dec 6, 2016

    @bahmutov
    Author

    Cool, both the above work arounds work, and the image can be pasted into Chrome email clients for example. Thank you very much for looking at this.

  10. kevinsawicki commented on Dec 6, 2016

    @kevinsawicki
    Contributor

    Cool, both the above work arounds work

    Great to hear, you can avoid these workarounds if you run this from the main process.

    In Main Processs

    ipcMain.on('capturePageToClipboard', (event, contentsId, rect) => {
      webContents.fromId(contentsId).capturePage(rect, (image) => {
        clipboard.writeImage(image)
      })
    })

    In Render Process

    const capturePageToClipboard = (rect) => {
      ipcRenderer.send('capturePageToClipboard', webview.getWebContents().id, rect)
    } 
  11. 13 remaining items

  12. sofianguy commented on Aug 9, 2019

    @sofianguy
    Contributor

    Thank you for taking the time to report this issue and helping to make Electron better.

    The version of Electron you reported this on has been superseded by newer releases.

    If you're still experiencing this issue in Electron v4.2.x or later, please add a comment specifying the version you're testing with and any other new information that a maintainer trying to reproduce the issue should know.

    I'm setting the blocked/need-info label for the above reasons. This issue will be closed 7 days from now if there is no response.

    Thanks in advance! Your help is appreciated.

  13. added
    blocked/need-info ❌
    on Aug 9, 2019
  14. DuBistKomisch commented on Aug 9, 2019

    @DuBistKomisch

    This is still an issue on 5.0.9:

    Uncaught Error: Could not call remote function 'anonymous'. Check that the function signature is correct. Underlying error: Error processing argument at index 0, conversion failure from #<Object>
    

    at renderer/api/remote.js:222

  15. added and removed
    blocked/need-info ❌
    on Aug 19, 2019
  16. miniak commented on Oct 30, 2019

    @miniak
    Contributor

    Will be possible in Electron 6+ after #20825 is merged and backported.

    <webview>.capturePage() will be returning a NativeImage instance correctly, which can then be passed to clipboard.writeImage() in the renderer process.

    It's still sending data over IPC though, it's better to handle this in the main process as suggested by #8151 (comment)

  17. self-assigned this
    on Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions