RPT
RPT

Reputation: 748

MutationObserver on iFrame javascript

I have an iframe containing a source www.abc/main.html

<iframe id="my-iframe" src="http://www.abc/main.html"></iframe>

abc/main.html imports and executes some js files. I want to use a MutationObserver to get the name of the js files it is importing.

var observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                [].filter.call(mutation.addedNodes, function (node) {
                    return node.nodeName == 'IFRAME';
                }).forEach(function (node) {
                    node.addEventListener('load', function (e) {
                        console.log('loaded', node.src);
                    });
                });
            });
        });
observer.observe(document.body, { childList: true, subtree: true });

This is what I am using. Referring: SO answer

But I am unable to get the name of the js file. Does it have to do with the CSP set on the source of the iframe?

Note: The iframe content is not of the same origin as the parent document.

Upvotes: 0

Views: 2701

Answers (1)

SLaks
SLaks

Reputation: 887453

MutationObserver only observes your own document.

Without explicit CORS support from the remote site, it is completely impossible to observe or interact with a frame from a different origin.

Upvotes: 1

Related Questions