Catch all events in iframe jQuery and replicate

廉价感情. 提交于 2019-12-08 07:45:32
migg

The short answer is: you cannot.

You cannot access the document object of "any web page" with JavaScript on your page if it is on a different domain, which I assume if you say "any web page", because of the Same Origin Policy. You would need the website in the IFRAME to cooperate with your script to achieve this, which will not happen with "any web page".

IFRAME on same domain
If your web page is on the same domain then you can access the events of the IFRAMEs body element by adding a listener for every event you want to catch like described here. This is possible for all events that bubble up to the body. So if you have Events that are prevented from bubbling upwards to the body, they will not be catched with this solution.

jQuery('#website body').on('click mouseover mouseout', function(event) {
    console.log(event.type);
});

Assuming you have an IFRAME with the id website you can catch the events you wish by listing them separated with spaces as above. The example catches click, mousover and mouseout.

Maybe this is closer to what you want?

Add event handlers to your divs. For your example you could use

$('#div').mouseover(function(e) { ... }) 

or

('#div').on('mouseover', function(e) { ... })

For 'replication', you'd have to store information about past events in some object. You could even store the event object itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!