Chop
Chop

Reputation: 4559

Prevent copy on an empty iFrame (GWT RichTextArea)

I am required to prevent copy from a form. Using a oncopy handler works just fine on all <input/>-type fields.

Yet I fail to apply it to our "richtextarea", which is basically an empty iframe (src="about:blank" for what I have been able to gather; the page is GWT-generated, and the people before me developped quite an extensive framework around it).

I am able to get the iframe in the JavaScript, but I fail to have a correct handler (I tried adding one that logs, but it never does).

I have tried frame.oncopy, frame.contentWindow.oncopy, frame.contentWindow.document.oncopy, frame.contentDocument.oncopy. None of these does log to the console when I copy the iframe's content.

Does somebody have any lead for me? Any help appreciated (I've been stuck on this for some days now).

Having a cross-compatible solution would of course be ideal, but the main target is Firefox (the page is only open via a custom container based on Firefox 10).

Edit 2015-03-24

For those who want to try some debug script, the component I have trouble with is the one demonstrated here.

I have some native methods in the Java project to execute some custom JavaScript on it.

Below is some of the JavaScript I have unsuccessfully tried.

var frame = document.getElementsByTagName('iframe')[0];

function disallowCopy() {
    alert('Gotcha!');
    return false;
}

frame.oncopy = disallowCopy;
frame.contentWindow.oncopy = disallowCopy;
frame.contentWindow.document.oncopy = disallowCopy;
frame.contentWindow.document.body.oncopy = disallowCopy;
frame.contentDocument.oncopy = disallowCopy;

Upvotes: 1

Views: 412

Answers (1)

krampstudio
krampstudio

Reputation: 3631

even though oncopy is a non standard event https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/oncopy and that there is no reliable way to prevent copying text,

you can check out the following bin

  1. ensure the frame is loaded
  2. use iframe.contentDocument.body to attch the event

Upvotes: 1

Related Questions