我正在创建一个 Google Chrome 扩展程序,我需要将页面 HTML 作为字符串导入到我的扩展程序中,以便可以使用它。
我已经尝试过:
document.getElementById('container');
但我得到了一个“null”作为返回。我已确定它是正确的 div id。
我使用的代码有问题还是这个方法根本不起作用?
最佳答案
要将容器 div 作为字符串,您应该使用 innerText 或 innerHTML。
如下:
var asText = document.getElementById('container').innerText;
var asHTML = document.getElementById('container').innerHTML;
如果您在未使用任何方法的情况下得到空答案,则意味着您的 div 不存在或未加载。
编辑:
不要忘记在manifest.json 文件中指定应该执行javascript 的时间。 您可以使用 run_at 参数来执行此操作。
manifest.json
{
"name": "The name of your extension",
"version": "0.0.1",
"manifest_version": 2,
"description": "Your description goes here",
"homepage_url": "http://example.com/",
"content_scripts": [
{
"run_at": "document_end",
"all_frames": false,
"matches": [
"*"
],
"js": [
"*"
]
}
],
"web_accessible_resources": [
"*"
],
"permissions": [
"storage"
]
}
关于javascript - Google Chrome 扩展 - 获取字符串中的选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255781/