question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

inserting Blob images via navigator.clipboard.write no longer works

See original GitHub issue

Hello. Back in the old version 7 of Puppeteersharp I was able to place .png images into the clipboard using a blob, but after I upgraded to this latest 9.0.2 version that is no longer the case.

Here is a minimal example:

new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision).GetAwaiter().GetResult();

            _browser = Puppeteer.LaunchAsync(new LaunchOptions()
            {
                Headless = false,
                DefaultViewport = null,
                Args = new[]
                    {
                        "--disable-gpu",
                        "--disable-dev-shm-usage",
                        "--disable-setuid-sandbox",
                        "--no-first-run",
                        "--no-sandbox",
                        "--no-zygote",
                        "--deterministic-fetch",
                        "--disable-features=IsolateOrigins",
                        "--disable-site-isolation-trials"
                    }
            }).Result;

            var context = _browser.DefaultContext;
            await context.OverridePermissionsAsync("https://pasteboard.co", new List<OverridePermission>()
            {
                OverridePermission.ClipboardReadWrite
            });

_pasterAppPage = _browser.NewPageAsync().Result;
            await _pasterAppPage.SetUserAgentAsync("user-agent=User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36");
            
            await _pasterAppPage.GoToAsync("https://pasteboard.co");


// then I would add the image bytes like this

var pasteResults = await _pasterAppPage.EvaluateFunctionAsync(@"(imageUrl) => {
                            return new Promise((resolve, reject) => {
                                const byteArray = Uint8Array.from(
                                    atob(imageUrl)
                                        .split('')
                                        .map(char => char.charCodeAt(0))
                                );

                                const imageBlob = new Blob([byteArray], { type: 'image/png' });

                                navigator.clipboard.write([
                                    new ClipboardItem({
                                        'image/png': imageBlob
                                    })
                                ]);

                                resolve(true);  
                            });
                        }"
                        , Convert.ToBase64String(fileBytes));

await _pasterAppPage.Keyboard.DownAsync("Control");
                    await _pasterAppPage.Keyboard.PressAsync("V");
                    await _pasterAppPage.Keyboard.UpAsync("Control");


Interestingly navigator.clipboard.writeText for adding text works fine.

I am using .NET Core 6.0.

Maybe I need to go back to using Puppeteersharp version 7.

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fictuscommented, Feb 16, 2023

In all honesty I think the real answer was me adding the OverridePermission “after” navigating to the URL and also triggering the “click” event into the textbox before pasting.

More than likely all that other stuff is nothing more than a previous desperate attempt from me trying to find a solution.

0reactions
kblokcommented, Feb 16, 2023

I had to add three new flags when creating the browser

Weird. PuppeteerSharp is already adding those args.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any way to copy image to clipboard with pure ...
Anyone know if I'm doing anything wrong or if it's even possible to copy images to clipboard without using external libraries? javascript ...
Read more >
How do I copy to the clipboard in JavaScript?
To write an image to the clipboard, you need to have the image as a blob . You can convert an image URL...
Read more >
JavaScript. Work with clipboard, Ctrl+C Ctrl+V
You can do this: first write the string to the clipboard, if the browser allows -> write the image. // guaranteed clipboard write...
Read more >
Async Clipboard API: Accessing the clipboard using ...
Before we can write an image to the clipboard, we first need to get a Blob of the image. There are several ways...
Read more >
Clipboard: write() method - Web APIs | MDN
The Clipboard method write() writes arbitrary data, such as images, to the clipboard. This can be used to implement cut and copy ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found