layerssss / paste.js

read image/text data from clipboard (cross-browser)
http://layerssss.github.io/paste.js/
MIT License
463 stars 94 forks source link

Pasting doesn't work in Firefox on Linux #22

Closed pkaminski closed 8 years ago

pkaminski commented 8 years ago

In Firefox 43 on Linux, trying to paste either text or an image via ctrl+v results in the text area losing focus (and I believe the contentEditable area gaining focus, though it's hard to tell), and no pasteImage/pasteText event being fired. Also, any further key presses of v (without ctrl) behave as if ctrl+v was pressed.

Original issue: Reviewable/Reviewable#235

layerssss commented 8 years ago

@pkaminski it's really only on linux, I'll install a vm to find out the causing

SimonSapin commented 8 years ago

I reported the reviewable bug. I can reproduce it in the $('.demo-textarea').pastableTextarea() box in http://micy.in/paste.js/ . Let me know if I can help test something.

layerssss commented 8 years ago

hi @SimonSapin , which distro of linux and which desktop environment are you using?

layerssss commented 8 years ago

ah, great @SimonSapin . I like archlinux!

SimonSapin commented 8 years ago

Archlinux x86_64, running GNOME 3 with X11.

pkaminski commented 8 years ago

Ping @layerssss -- have you made any progress on this? Thanks.

layerssss commented 8 years ago

hi @pkaminski I going to fix it today or to tomorrow, haven't investigated the cause yet, but it shouldn't be a problem. Just got a fresh GNOME setup now.

pkaminski commented 8 years ago

Awesome, thanks in advance! Otherwise I'd have to add user-agent sniffing to reviewable.io to disable paste.js on Linux.

layerssss commented 8 years ago

Hi what I've discovered is if I do copy an image from GIMP, it works fine. But if I copy an image from another webpage (right-click the image -> copy image), this issue occurred, and that's because Firefox only exposed the url of that image in this case. (This turned out to be not Linux specific, but Firefox specific, it can also be reproduced by ff on OSX and Windows.) So when this happens, it will only be possible to retrieve image from a "CORS-enabled" url.

So now I added a new event pasteImageError, incase that having detected an image been pasted, but unable to retrieve it's data from just a url. You can see the usage of that in changes in the demo.

For example, if you go to http://www.centos.org/ , copy the logo, and paste, you will get that error, but you can still get that url if that would be useful for you.

If you copy the big wide image from http://www.merrypapery.com/texture , and paste, you will be able to get everything like previously expected. Because those images are hosted on a CORS-enabled CDN.

Last case, but the least, image pasting behaviour been detected, but no valid url nor data could be retrieved. (e.g. Safari)

layerssss commented 8 years ago

fixed by:

layerssss commented 8 years ago

hi @pkaminski , I pushed a release 0.0.11, so if you are using package manager like bower you can update to that.

pkaminski commented 8 years ago

Thanks, I'll integrate the new release later tonight. But did you also test pasting text? Because that had the same effect as pasting images (lost focus, ctrl "stuck") and doesn't seem related to CORS.

layerssss commented 8 years ago

Yes, text pasting is fixed in a separate commit, it got to do something with the keyevent hijacking logic.

SimonSapin commented 8 years ago

Has http://micy.in/paste.js/ been updated for both changes? I now have no problem type the letter V, but CTRL+V still moves focus away and doesn’t paste for me. (I can paste a selection with middle-click, but that’s always been the case, and it’s a "separate clipboard" form CTRL+C / CTRL+V.)

layerssss commented 8 years ago

@SimonSapin It has been updated already. What is the image source? Did you copied an image from another program like GIMP, or just copied an image from anther webpage's context menu?

layerssss commented 8 years ago

@SimonSapin if it's an image copied from another webpage, you would expect a few seconds before an error alert being shown and the "focus" being recovered.

layerssss commented 8 years ago

alert like this, image , if it's not the case, please tell me what's your image source

SimonSapin commented 8 years ago

I’m copying text, in this case “Paste” from <h1>Paste.js</h1> on the test page, with double click to select + CTRL+C to copy. I’ve tried waiting a full minute just to be sure, nothing happened after focus went away.

The original issue is also about copying text. I didn’t know Reviewable supported copying images before filing this issue, and I don’t expect to use that feature. (I haven’t used any image in code review so far.)

pkaminski commented 8 years ago

I deployed the new paste.js to Reviewable, and AFAICT things are now working correctly. @SimonSapin, can you give it a try? I think maybe the problem on http://micy.in/paste.js/ is just #19, for which I have a workaround on Reviewable.

SimonSapin commented 8 years ago

@pkaminski Same as on the test page, CTRL+V still moves focus and doesn’t paste on Reviewable, but now I can type the letter V afterwards.

layerssss commented 8 years ago

@pkaminski , I will try to integrate your workaround to fix #19

layerssss commented 8 years ago

hi, @SimonSapin I'm unable to reproduce it currently, but I push another fix could potentially solve your problem (losing focus when pasting text). Could you please try it out on the demo page? And be sure to refresh the cache.

SimonSapin commented 8 years ago

Still the same CTRL+F5, with devtools showing fresh requests with 200 response status codes being made for everything. I can type the letter V, paste text with middle-click, or right-click + "Paste" in the context menu, but CTRL+V moves focus and doesn’t paste.

http://micy.in/paste.js/paste.js has Last-Modified: Sun, 24 Jan 2016 14:55:06 GMT

However, I could not reproduce the issue in an new Firefox profile. Back in my main profile though, uBlock shows nothing blocked, disabling HTTPSEverywhere doesn’t help, and none of the other Firefox extensions should affect content.

Looking at the debugger for a bit, I’m suspicious of this code as it doesn’t seem to do any pasting:

        if (ctlDown && ev.keyCode === 86) {
          return paste._container.focus();
        }
pkaminski commented 8 years ago

While I was able to reproduce the original issue in a Sauce Labs Linux VM (don't know which distro they use), I can no longer do so. Could your Firefox profile be corrupted in some subtle way, @SimonSapin?

The code you found is fine. It focus a special off-screen element to receive the keystroke, which the browser then translates into a paste event (even if the clipboard holds an image). This then triggers paste.js internal processing and a focus back to the original element. Or at least it should.

SimonSapin commented 8 years ago

Well, yes, as I said the problem does not happen in a new profile of the same browser, but “corrupted in some subtle way” is just a nice way to say we don’t know what’s going on.

This then triggers paste.js internal processing and a focus back to the original element. Or at least it should.

It sounds like this is where things are going wrong. Which part of the code is doing that? I can try debugging some more.

pkaminski commented 8 years ago

I believe the paste event is handled on line 186, and the focus back to the textarea after a text paste is invoked on line 151. This is all in v0.0.11 which is what's currently integrated into Reviewable, but I'll try to integrate the latest 0.0.13 later this afternoon, see if it helps.

pkaminski commented 8 years ago

I integrated paste.js 0.0.13 into Reviewable, in case it makes a difference.

layerssss commented 8 years ago

hi, @SimonSapin , could you try on the demo page ( http://micy.in/paste.js/ ) to see if you can reproduce the problem?

layerssss commented 8 years ago

hi, @SimonSapin , could you set some breakpoints and see if they were executed, and the order of execution.

SimonSapin commented 8 years ago

When I hit CTRL+CV after setting breakpoints on all of these lines in the browser profile with the problem, none of them are executed. The code I quoted above is executed and I can step through jQuery triggering a focus event, but then nothing interesting seems to happen before control goes back to the event loop.

layerssss commented 8 years ago

Thanks @SimonSapin , is that you mean CTRL+V ? That'll be strange.

SimonSapin commented 8 years ago

Yes, CTRL+V

layerssss commented 8 years ago

Hi @SimonSapin , could you try on that demo-page( http://micy.in/paste.js ), but on the third box (that [contenteditable]), to see if text-pasting and image-pasting are functioning normally?

SimonSapin commented 8 years ago

The third box works ok, but if I remember correctly it always has.

layerssss commented 8 years ago

This issue should be fixed in 0.0.16 now, otherwise please feel free to let me know and reopen it.