如何处理从内容脚本到背景脚本的嵌套帧?
我正在为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"
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理Woxxom评论,我终于知道,我可以在注射过程中简单地获取所需的信息,然后将其保存在背景脚本中。
我的
subtest.json
相应地进化:我完全无法使用
.contentDocument
。编辑:结果会变得更好:
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 :I am completely unable to use
.contentDocument
.Edit : Results get even better with this :