puneet
puneet

Reputation: 622

Update the clipboard data before paste (not the 'paste' event)

I have a requirement where a text copied onto the clipboard, has to be transformed into some other text before it gets pasted. E.g on button click I do a clipboard copy programmatically and some text like "Before Change" gets copied onto the clipboard. The paste event which I have tied to my editor cannot change the clipboard data and its always the data existing on the clipboard ("Before Change") that gets copied.

$("#Editor1").on("paste", createIncludedScriptPath);
function createIncludedScriptPath(e) {
    var pastedData = e.originalEvent.clipboardData.getData('text');
    var path = createPath(pastedData);
    textToCopy = "." + " " + "'" + path + "'";
    copyTextToClipboard(textToCopy);
}

Here, in the above code, I am trying to get the existing clipboard data, change it and copy the changed data on the clipboard. However, it is always the old data i.e. (in the pastedData variable) which gets pasted.

I learnt, that in the clipboard paste API, you cannot reset the clipboard.

How do I update the clipboard data, so that the updated data gets eventually pasted?

Upvotes: 1

Views: 3611

Answers (1)

David Ibl
David Ibl

Reputation: 911

You could set the editor content directly and call e.preventDefault();

I think updating the clipboard data will not work.

Upvotes: 2

Related Questions