5
我试图从用户正在查看的页面拉出数据并将该数据发送到扩展弹出窗口。我在从当前页面提取信息时遇到了麻烦。在审查了谷歌代码教程后,我设置了我的清单和内容脚本,如下所示。当我进入一个测试页面,其中id =“comments”的跨度时,我注入的内容脚本中的变量总是以null结尾。任何人都知道我可能会错过什么?谢谢!Chrome扩展内容脚本无法访问其注入的页面的DOM
manifest.json
=================
{
"name": "Sample Extension",
"version": "0.0.1",
"description": "Sample extension",
"icons": {"128": "icon.png"},
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_icon": "browseraction.png",
"default_title": "Sample",
"popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "<all_urls>" ],
"js": ["scripts/contentscript.js"]
}
]
}
contentscript.js
===================
var comments = document.getElementById("comments");
alert(comments.innerText);
+1,但他应该使用'textContent'。 –
谢谢,伙计们!我利用这两个建议,使用“run_at”和“textContent”,他们完美地工作。 – sojo2600
在Chrome扩展开发者手册中提倡'innerText'。 - 没有必要在这里避免它。 –