nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

We don't execute document.execCommand() this time, because it is called recursively.

Open broderix opened this issue 11 years ago • 4 comments

Hi, folks! I'm trying to clean paste-data like this: <textarea id="edit456"></textarea>

var onpaste = function(e) {
    e.preventDefault();
    var text = e.clipboardData.getData("text/plain");
    document.execCommand("insertHTML", false, text);
};
var edit = document.getElementById('edit456');
edit.addEventListener('paste', onpaste);

Then I paste data via context menu (or you can do it on console for tests)

    document.execCommand("paste");

and got warning from console: "We don't execute document.execCommand() this time, because it is called recursively."

The data will not be pasted.

broderix avatar Apr 24 '15 13:04 broderix

I was looking for info on the same console message that's appearing when trying to cut-to-clipboard within a nw app. I came across this link which, though old, may help in your case? They use setTimeout() to get around the issue -- so I don't know if it's a bug or a security "feature".

https://code.google.com/p/chromium/issues/detail?id=363288#c3

TrevorPT avatar May 01 '15 17:05 TrevorPT

@TrevorPT I have tried it, didn't help

broderix avatar May 06 '15 20:05 broderix

It works in my case but I didn't try to set up your code. Where did you invoke the setTimeout, like this?

setTimeout( function () {
    document.execCommand("insertHTML", false, text);
}, 0 );

TrevorPT avatar May 07 '15 19:05 TrevorPT

@TrevorPT thank you very much! Found out that setTimeout not necessary - works without it, also helped gui.Clipboard:

var gui = require('nw.gui');
var clipboard = gui.Clipboard.get();
document.execCommand("insertHTML", false, clipboard.get()); //or insertText

broderix avatar May 12 '15 20:05 broderix