3

I tried to create a RichText Editor using jQuery with the help of execCommand().

But the following code:

document.execCommand('cut', false, null); 

document.execCommand('copy', false, null);

document.execCommand('paste', false, null);

Is not working in Mozilla Firefox, Google Chrome and some other browsers.

Are there any possibilities to do the Cut, Copy and Paste actions using execCommand() or there is any other method to do the Cut, Copy and Paste actions in my RichText Editor?

Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
Gmv
  • 2,008
  • 6
  • 29
  • 46

2 Answers2

3

It's easy to solve this problem, follow these steps: Go to "about:config" no quotes on Firefox and then hit the "i'll be careful, I promise" button, next type "dom.event.clipboardevents.enabled" then double click it, so the value is false. And that should do the trick.

enter image description here

Amin
  • 414
  • 1
  • 11
  • 23
1

From Chrome 42 and Firefox 41, document.execCommand('cut') and document.execCommand('copy') will work, but only on semi trusted events. See here https://www.w3.org/TR/2014/WD-clipboard-apis-20140313/#semi-trusted-events

Like this for example:

document.getElementById('copy').onmousedown = function() {
  console.log(document.execCommand('copy'))
}

document.getElementById('cut').onmousedown = function() {
  console.log(document.execCommand('cut'))
}
<textarea></textarea>
<button id="copy">Copy</button>
<button id="cut">Cut</button>

On Chrome document.execCommand('paste') will work the same way but requires an extension to be installed to allow it. Without extension, it won't work. To allow it, you need to include this "clipboarRead" in the permissions in your manifest.json file. Like this:

permissions: {
    ...
    "clipboardRead"
    ...
}
Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57