How to get contentWindow of the frame after the change of its cross-origin source by a script?

扶醉桌前 提交于 2020-04-30 09:27:30

问题


I am developing a Chrome/Firefox extension, which is intended to be used only with my own Web-application that should semi-automatically process different Web-pages in a certain specific way. In the context of this question my application is just a single Web-page, containing a single iFrame, which source is initially from the same domain. When an application's user chooses the URL of a page to be processed, this URL is assigned as the new source of the frame. To process the chosen page, when it is loaded in the frame, I need to get access to the frame's contentWindow from a certain (content?) script.

I try to implement the approach described here. However I believe that in my case I need neither background nor main.js content scripts in my extension as I can just add all necessary functions to my usual my.application.js. It seems the only thing I need is sub.js content script, injected in the iFrame and containing code like this:

if (!window.isTop) {
    window.addEventListener("message", (event) => {
        if (event.source == window &&
            event.data &&
            event.data.sender == "url_load_checker") {
                let nodes = window.document.childNodes;
                processUrl(nodes);
        }
    }
}

This event handler is really invoked, when I send the needed message from the main window. The problem is the nodes I get are from my main window too, rather than from the frame, while I can see in the browser's inspector that the needed nodes from the requested URL are present in this frame.

How could this be fixed in a proper way? Should I call tabs.executeScript() to inject a content script after each frame source change?

来源:https://stackoverflow.com/questions/61450109/how-to-get-contentwindow-of-the-frame-after-the-change-of-its-cross-origin-sourc

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