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



我正在为Firefox创建一个web扩展,旨在收集浏览器游戏中的数据。

我从一个简单的测试开始,以管理内容脚本和后台之间的注入和通信(使用端口)。在一个简单的页面中一切都很好:我只是通过我的扩展编辑表单元格,使用API调用来获取一些随机数据。当我想朝着我的目标前进时,我很难获得帧信息。我的脚本在游戏中正常工作(在测试页面中完成的事情在游戏中工作得很好),所以通信和注入看起来都很好。游戏使用了大量的帧,过去的登录或问候页面,作为一个简单的结构摘录显示:

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

这个网站使用相同的结构

因为与表格单元格的交互工作得很好,我试图得到所有的框架集:

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

返回的节点列表为空。

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

根据MDN,我使用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"
]
}
}

在WOxxOm注释的工作中,我终于明白了我可以简单地在注入过程中获取所需的信息并将其保存在后台脚本中。

我的manifest.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"
],
}
]
}

最新更新