Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从选项卡获取HTML内容_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 从选项卡获取HTML内容

Javascript 从选项卡获取HTML内容,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我在这里完全不知所措。我想从Chrome的标签中获取html内容 manifest.json { "manifest_version": 2, "name": "Test",is a test.", "version": "1.0", "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "background": { "scripts": ["main.j

我在这里完全不知所措。我想从Chrome的标签中获取html内容

manifest.json

{
  "manifest_version": 2,
  "name": "Test",is a test.",
  "version": "1.0",
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "background": {
      "scripts": ["main.js"],
      "persistent": false
  },
  "permissions": [
  "tabs",
  "https://www.google.com"
  ]
}
{
    "manifest_version": 2,
    "name": "Test is a test.",
    "version": "1.0",
    "content_scripts": [
        {
            "matches": [
                "<all_urls>"
            ],
            "js": [
                "content.js"
            ]
        }
    ],
    "background": {
        "scripts": [
            "background.js"
        ],
        "persistent": false
    },
    "browser_action": {
        "default_title": "title"
    },
    "permissions": [
        "tabs",
        "<all_urls>"
    ]
}
main.js

var timerObj = new Timer({'interval':5000});

chrome.runtime.onStartup.addListener(timerObj.start(mainF));

function mainF() {
    chrome.tabs.query( {} ,function (tabs) {
    for (var i = 0; i < tabs.length; i++) {
        var url = tabs[i].url;
        if (url != null) {
             console.log(tabs[i].url);
             //I want to get html source here
        }
    }
  });
};

function Timer( obj ){
var timerObj=新计时器({'interval':5000});
chrome.runtime.onStartup.addListener(timerObj.start(mainF));
函数mainF(){
chrome.tabs.query({},函数(tabs){
对于(变量i=0;i

最后一行
函数计时器(obj){
为简洁起见被截断。
console.log(制表符[i].url);
用于测试。对于每个选项卡,我希望获得html源代码。有了该源代码,我将解析标记和其他内容。我看到其他资源提到了
sendMessage
onMessage
,但我没有真正了解它。许多其他资源都提到了不推荐使用的
sendRequest
onRequest
>.

据我所知,有三种方法可以实现这一点

  • .我们可以使用将内容脚本注入网页,在回调中可以得到返回值

  • 我们还可以以
    manifest.json
    的方式注入内容脚本,然后使用
    chrome.runtime.sendMessage
    chrome.runtime.onMessage
    传输数据

  • .是的,这也是一种方式,我们可以直接在后台页面进行ajax调用来获取网页的源代码,因为我们可以很容易地获取url。显然,与上述两种方法相比,我们需要启动另一个http请求,但这也是一种方式

  • 在下面的示例代码中,我只使用
    browserAction
    触发事件,然后您可以切换不同的方法,通过注释掉其他两种方法来获取网页的源代码,并且只保留一种

    manifest.json

    {
      "manifest_version": 2,
      "name": "Test",is a test.",
      "version": "1.0",
      "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
      "background": {
          "scripts": ["main.js"],
          "persistent": false
      },
      "permissions": [
      "tabs",
      "https://www.google.com"
      ]
    }
    
    {
        "manifest_version": 2,
        "name": "Test is a test.",
        "version": "1.0",
        "content_scripts": [
            {
                "matches": [
                    "<all_urls>"
                ],
                "js": [
                    "content.js"
                ]
            }
        ],
        "background": {
            "scripts": [
                "background.js"
            ],
            "persistent": false
        },
        "browser_action": {
            "default_title": "title"
        },
        "permissions": [
            "tabs",
            "<all_urls>"
        ]
    }
    

    方法2不工作,我有“TypeError:无法读取未定义的”属性“sourceCode”在IE上运行吗?我有一个类似的问题,我必须使用IE。