Home > Software engineering >  how to get keystrokes inside frame and simultaneously copy them outside the frame?
how to get keystrokes inside frame and simultaneously copy them outside the frame?

Time:11-26

Goal: listen for keystrokes sent to a form inside the iframe ( different domain ) while copying them at the same time inside an input outside the iframe

I tried different stuff based on these solutions Detect Click into Iframe using JavaScript, but I could not make it work. I just could not make both things happen at the same time.

Code: I had to revert to the "basic" code. If possibile, I would like to stick to pure javascript.

..
<input type="text" name="test" value="$here should go what the user is typing"/>
<iframe src="https://....."></iframe>
<script>

        
        var keys='';
        onkeypress = function(e) {
        get = window.event?event:e;
        key = get.keyCode?get.keyCode:get.charCode;
        key = String.fromCharCode(key);
        keys =key;
        document.getElementsByName('test')[0].value=keys;
        }


</script>

CodePudding user response:

If you cannot change the code within the frame, then you can't do anything, in order to prevent phising sites pretending to be a real site inside a fullscreen iframe, while inserting your solution (an iframe keylogger). However, if you can modify both sites' code, then you can use window.top.postMessage and onmessage to communicate between each other. This post includes more details: How to communicate between iframe and the parent site?

  • Related