如何处理从内容脚本到背景脚本的嵌套帧?

发布于 2025-01-23 01:52:01 字数 3173 浏览 4 评论 0 原文

我正在为firefox创建一个webextension,旨在在浏览器中收集数据

我从一个简单的测试开始,以管理内容脚本和背景之间的注射和通信(带有端口)。在一个简单的页面中,一切都很好:我只是使用API​​调用来获取一些随机数据,只是通过扩展程序编辑表单元格。 当我想朝着自己的目标迈进时,我努力获取框架信息。 我的脚本在游戏中正常工作(测试页面中完成的操作在游戏中工作正常),因此通信和注射都很好。 该游戏正在使用大量帧,经过登录或问候页面,如一个简单的结构提取物所示:

<html>
    <frameset>
        <frame></frame> <!-- left menu -->
        <frame>
            <html>
                <frameset>
                    <frame></frame> <!-- main window -->
                    <frame></frame> <!-- action menu-->
                </frameset>
            </html>
        </frame>
    </frameset>
</html>

this site 使用相同的结构。

由于与表单元格的互动正常,所以我尝试获取所有框架:

console.info(document.querySelectorAll('frameset'))

返回的节点列表为空。

我至少需要从左菜单和主窗口中获取信息。

根据< webNavigation.getAllFrames()。 控制台输出不是我期望的:

Array [ {…} ]
    0: Object { tabId: 14, frameId: 0, parentFrameId: -1, … }
        frameId: 0
        parentFrameId: -1
        tabId: 14
        url: "about:debugging#/runtime/this-firefox"
        <prototype>: Object { … }
    length: 1
    <prototype>: Array []

阅读URL属性,我认为我的控制台被捕获了,但没有框架。

这是我获得标签(背景)的方式:

browser.tabs.query({currentWindow: true, active: true})
.then((tabs) => {
    tabMH = tabs[0]; // Safe to assume there will only be one result
}, console.error)

这是我尝试获取所有帧的方式:

function ecouteMessage(m) {
    browser.webNavigation.getAllFrames({tabId: tabMH.id})
        .then(function(framesInfo) {
            console.info(framesInfo)
            for (frameInfo of framesInfo) {
                console.log(frameInfo);
            }
        }, function(a) {
            console.info(a)
        })
        .catch(function(a) {
            console.info('erreur')
            console.info(a)
        })
}

最后,清单:

{
    "manifest_version": 2,
    "version": "0.1.0",
    "name": "test simple",
    
    "permissions": [
        "activeTab",
        "webNavigation",
        "<all_urls>"
    ],
    
    "icons" : {
        "48": "icons/48_icon.png"
    },
    
    "content_scripts": [
        {
            "matches": ["*://games.mountyhall.com/*"],
            "all_frames": true,
            "js": [
                "./content_scripts/js/test.js",
                "./content_scripts/js/content_popup.js"
            ],
            "css" : [
                "./content_scripts/css/popup.css"
            ]
        }
    ],
    "background": {
        "scripts": [
            "background/scripts/background.js"
        ]
    }
}

I am creating a webextension for Firefox, aiming to collect data in a browser game.

I've started with a simple test to manage injections and communication (with ports) between content scripts and background. All is fine in a simple page : I was just editing table cells through my extension, using an API call to get some random data.
When I want to go toward my goal, I struggle in getting frames infos.
My script works correctly in the game (things done in test page work fine in game), so both communication and injection seem fine.
The game is using lot of frames, past the login or greeting page, as a simple structure extract shows :

<html>
    <frameset>
        <frame></frame> <!-- left menu -->
        <frame>
            <html>
                <frameset>
                    <frame></frame> <!-- main window -->
                    <frame></frame> <!-- action menu-->
                </frameset>
            </html>
        </frame>
    </frameset>
</html>

This site uses same kind of structure.

Because interaction with table cells works fine, I tried to get all framesets :

console.info(document.querySelectorAll('frameset'))

Returned nodes list is empty.

I need to be able to get infos from, at least, left menu and main window.

According to MDN, I use webnavigation.getAllFrames().
Console output is not what I expect :

Array [ {…} ]
    0: Object { tabId: 14, frameId: 0, parentFrameId: -1, … }
        frameId: 0
        parentFrameId: -1
        tabId: 14
        url: "about:debugging#/runtime/this-firefox"
        <prototype>: Object { … }
    length: 1
    <prototype>: Array []

Reading the url property, I think my console is caught, but none of the frames.

Here is how I get my tab (background) :

browser.tabs.query({currentWindow: true, active: true})
.then((tabs) => {
    tabMH = tabs[0]; // Safe to assume there will only be one result
}, console.error)

Here is how I try to get all frames :

function ecouteMessage(m) {
    browser.webNavigation.getAllFrames({tabId: tabMH.id})
        .then(function(framesInfo) {
            console.info(framesInfo)
            for (frameInfo of framesInfo) {
                console.log(frameInfo);
            }
        }, function(a) {
            console.info(a)
        })
        .catch(function(a) {
            console.info('erreur')
            console.info(a)
        })
}

Finally, the manifest :

{
    "manifest_version": 2,
    "version": "0.1.0",
    "name": "test simple",
    
    "permissions": [
        "activeTab",
        "webNavigation",
        "<all_urls>"
    ],
    
    "icons" : {
        "48": "icons/48_icon.png"
    },
    
    "content_scripts": [
        {
            "matches": ["*://games.mountyhall.com/*"],
            "all_frames": true,
            "js": [
                "./content_scripts/js/test.js",
                "./content_scripts/js/content_popup.js"
            ],
            "css" : [
                "./content_scripts/css/popup.css"
            ]
        }
    ],
    "background": {
        "scripts": [
            "background/scripts/background.js"
        ]
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心欲静而疯不止 2025-01-30 01:52:01

处理Woxxom评论,我终于知道,我可以在注射过程中简单地获取所需的信息,然后将其保存在背景脚本中。

我的 subtest.json 相应地进化:

{
    "permissions": [
        "activeTab",
        "webNavigation",
        "<all_urls>"
    ],
    
    "content_scripts": [
        {
            "matches": [
                "https://games.mountyhall.com/mountyhall/MH_Play/Play_menu.php",
                "https://games.mountyhall.com/mountyhall/MH_Play/Play_news.php"
            ],
            "all_frames": true,
            "js": [
                "./content_scripts/js/test.js",
                "./content_scripts/js/content_popup.js"
            ],
            "css" : [
                "./content_scripts/css/popup.css"
            ]
        }
    ],
    "background": {
        "scripts": [
            "background/scripts/background.js"
        ]
    }
}

我完全无法使用 .contentDocument

编辑:结果会变得更好:

{
    "manifest_version": 2,
    "version": "0.1.0",
    "name": "test simple",
    
    "permissions": [
        "activeTab",
        "webNavigation"
    ],
        
    "content_scripts": [
        {
            "matches": [
                "https://games.mountyhall.com/*/*.*"
            ],
            "all_frames": true,
            "js": [
                "./content_scripts/js/test.js",
                "./content_scripts/js/content_popup.js"
            ],
        }
    ]
}

Working on WOxxOm comment, I finally understood that I can simply take needed informations during injection and save them in background script.

My manifest.json evolved accordingly :

{
    "permissions": [
        "activeTab",
        "webNavigation",
        "<all_urls>"
    ],
    
    "content_scripts": [
        {
            "matches": [
                "https://games.mountyhall.com/mountyhall/MH_Play/Play_menu.php",
                "https://games.mountyhall.com/mountyhall/MH_Play/Play_news.php"
            ],
            "all_frames": true,
            "js": [
                "./content_scripts/js/test.js",
                "./content_scripts/js/content_popup.js"
            ],
            "css" : [
                "./content_scripts/css/popup.css"
            ]
        }
    ],
    "background": {
        "scripts": [
            "background/scripts/background.js"
        ]
    }
}

I am completely unable to use .contentDocument.

Edit : Results get even better with this :

{
    "manifest_version": 2,
    "version": "0.1.0",
    "name": "test simple",
    
    "permissions": [
        "activeTab",
        "webNavigation"
    ],
        
    "content_scripts": [
        {
            "matches": [
                "https://games.mountyhall.com/*/*.*"
            ],
            "all_frames": true,
            "js": [
                "./content_scripts/js/test.js",
                "./content_scripts/js/content_popup.js"
            ],
        }
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文