相关疑难解决方法(0)

从chrome扩展获取当前页面的源HTML

我有一个chrome扩展名.我需要从当前页面的HTML源代码进行分析.我在这里找到了各种带有背景页面和内容脚本的解决方案,但没有人帮助我.这是我到目前为止:
manifest.json:

{
  "name": "Extension",
  "version": "1.0",
  "description": "Extension",
  "browser_action": {
    "default_icon": "bmarkred.ico",
    "popup": "Test.html"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["content.js"]
    }
  ],
  "background": {
    "page": "backgroundPage.html"
  },
  "permissions": [
    "cookies",
    "tabs",
    "http://*/*", 
    "https://*/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

background.html:

<html>
<head>
<script type="text/javascript">
    try {
        chrome.tabs.getSelected(null, function (tab) {
            chrome.tabs.sendRequest(tab.id, {action: "getSource"}, function(source) {
                alert(source);
            });
        });
    }
    catch (ex) {
        alert(ex);
    }
</script>
</head>
</html>
Run Code Online (Sandbox Code Playgroud)

content.js:

chrome.extension.onRequest.addListener(function(request, sender, callback) {
    if (request.action == "getSource") {
        callback(document.getElementsByTagName('html')[0].innerHTML);
    }
});
Run Code Online (Sandbox Code Playgroud)

警报始终警告未定义.即使我在content.js文件中将回调函数更改为: …

html google-chrome-extension

74
推荐指数
1
解决办法
9万
查看次数

标签 统计

google-chrome-extension ×1

html ×1