1

My chrome extension reacts on clicks / double clicks inside a webpage. I'm catching all the events with

window.addEventListener('dblclick', event => {
    // code
});

It doesn't fire on events inside an iframe. Is it possible to add listener to any iframe, so I could process all the clicks inside webpage?

As I understand, I can't get any iframe using getElementById. I've tried to get all the iframes by tag and add event listeners with no success:

document.getElementsByTagName('iframe').forEach(frame => {
    frame.contentWindow.document.body.addEventListener('dblclick', event => {
        kango.console.log('dbl click')
    })
})
Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
  • This question has been answered before. Please see [How to add click event to a iframe with JQuery](https://stackoverflow.com/questions/1609741/how-to-add-click-event-to-a-iframe-with-jquery) – Alexios Tsiaparas Oct 06 '18 at 09:35
  • @AlexiosTsiaparas, thank you for your comment! I'll update my question – Vladyslav Zavalykhatko Oct 06 '18 at 09:38
  • `contentWindow.document.body` is available only for same-origin iframes. For a universal solution you'll need to declare your content script with `"all_frames": true` in manifest.json so that each iframe gets an instance of the content script where you can trivially access iframe's `window` and `document` directly. – wOxxOm Oct 06 '18 at 09:43
  • @wOxxOm, thanks for the comment. can you please explain more how can I access each of the iframe's instances? – Vladyslav Zavalykhatko Oct 06 '18 at 09:45
  • @wOxxOm, works, thanks – Vladyslav Zavalykhatko Oct 06 '18 at 10:09

0 Answers0