Willy-Kimura / SharpClipboard

A library for anonymously monitoring clipboard entries.
183 stars 34 forks source link

Copying URL from Chrome triggers twice #16

Open ghost opened 3 years ago

ghost commented 3 years ago

I notice that copying the URL from Chrome 84.0.4147.125 triggers the detection twice. Seems to work as intended in every other occasion.

private void foo()
{
            clipboard = new SharpClipboard();
            clipboard.ObserveLastEntry = false;
            clipboard.ObservableFormats.Texts = true;
            clipboard.ClipboardChanged += Clipboard_ClipboardChanged;
}
        private void Clipboard_ClipboardChanged(object sender, SharpClipboard.ClipboardChangedEventArgs e)
        {
            // Get the cut/copied text.
        }
Willy-Kimura commented 3 years ago

Thanks. Will test this out...

ghost commented 3 years ago

Forgot to mention. The same thing happens in your NetCoreWinForms sample. Put a breakpoint on: if (e.ContentType == WK.Libraries.SharpClipboardNS.SharpClipboard.ContentTypes.Text) copy URL in Chrome and it should trigger twice. I'll see if I can figure something out...

Edit: Not completely sure, but other implementations struggle with the same, so I wonder if Chrome is really copying things twice. I find this strange as I thought Chrome couldn't influence the clipboard if I right click in the address bar and copy the value.... Other implementations show me that it first copies the url without "http" and then with "http"....

Anyways, one workaround is to just use a timer and ignore event that come in a close succession.

Willy-Kimura commented 3 years ago

That's quite a strange behaviour from Chrome. You're actually right about the copy issue. The Chrome address bar runs a script that does parse URLs on update and special events, e.g after copying/pasting.

I'll need to test this further and probably provide a custom implementation or workaround that uniquely works with chrome. For now, try using the argument e.SourceApplication.Name in the ClipboardChanged event to determine if the active application being copied from is Chrome (chrome.exe) then capture the first copy instance only and ignore the second.

ghost commented 3 years ago

Yeah, I saw the same thing happening... I've solved with a timer as a workaround for now. I check that the content is different and reset the "cache" when the timer elapses. Works perfectly for my case. :)

        private string clipboardCache = null;
        private readonly Timer clipboardTimer = new Timer(5000);
...
        private void ClipboardTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Remove cache when timer elapses
            clipboardCache = null;
            clipboardTimer.Stop();
        }

// Copied value is not currently in cache
            if (e.Content.ToString() != clipboardCache)
            {
                // Update cache
                clipboardCache = e.Content.ToString();
                clipboardTimer.Start();

                // Only handle valid links
                ....
            }
runamonk commented 3 years ago

This same issue happens with the Windows 10 screen snip.

Click on actions/notifications. Click screen snip, select a region. Clipboard change is triggered twice.

Thanks

BBUBBA commented 3 years ago

It's OS Trouble

  1. you have to use register Custom ClipboardFormat and then Skip Event Custom ClipboardFormat
  2. Save Last Clipboard Info, if Same Clipboard Info Skip. (Recomend)
    • if you Binary Serialieze about Clipboard info it's Easy.

search google "clipboard event twice" or "clipboard event Echo"

if Fire Clipboard.Settext in SharpClipboard.Tests.Winform it's Fire 2 time ChangedEvent.

also every Trouble Clear.

  1. run "notepad.exe"
  2. copy & paste in notepad ("Event")
  3. close notepad ("Event") ( because Clipboard Data is 2 Type) (1. alive with process) (2. not alive with process - when process die. Clipboard Data Delete) ( So When you close process after use clipboard. you will see Clipboard chagned event )

windows act many check clipboard point. But number 2 is Any trouble Solve.

sorry about my english not good

DineshSolanki commented 2 years ago

the same issue is happening on copying from firefox.

guzelonur commented 2 years ago

I discovered the same problem happening even on XP :)

guzelonur commented 2 years ago

And another problem, with this library, it detects images "twice" which were set by My.Computer.ClipBoard.SetImage method of VB.NET. There is a bug. And also, I couldn't find a method to set image natively within this library there.

TorstenC commented 2 years ago

I solved the problem with:

BTW:
In case of Chromium links I get them including the description as TextDataFormat.Html.

SamHobbsOrg commented 8 months ago

I tested copying from the address bar of Edge, Chrome and Opera. With Chrome and Opera my ClipboardChanged event is called (triggered or invoked, whatever) twice. With Edge it is called 3 times.

Anyone familiar with the GetClipboardSequenceNumber function? Perhaps I misunderstand what that does but it seems to solve the problem. The WinAPI function can be used stand-alone.