Google chrome extension 如何将html从当前选项卡获取到弹出窗口

Google chrome extension 如何将html从当前选项卡获取到弹出窗口,google-chrome-extension,Google Chrome Extension,我使用document.body.outerHTML从选项卡获取html,但我做不到 manifest.json: { "name": "SEO Analytic", "description": "Adds a print button to the browser.", "version": "1.2", "manifest_version": 2, "background": { "scripts": ["background.js

我使用
document.body.outerHTML
从选项卡获取html,但我做不到

manifest.json:

{
    "name": "SEO Analytic",
    "description": "Adds a print button to the browser.",
    "version": "1.2",
    "manifest_version": 2,

    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js":      ["content.js"]
        }
    ],

    "permissions": ["tabs", "http://*/*", "https://*/*"],

    "browser_action": {
        "default_title": "SEO Analytic",
        "default_popup": "popup.html"
    }
}
chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getText"}, function(response) {
        if (response.method == "getText") {
            alltext = response.data;
            alert(alltext);
        }
    });
});
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
    if (request.method == "getText") {
        sendResponse({
            data: document.body.outerHTML,
            method: "getText"   // same as innerText
        });
    }
});
content.js:

{
    "name": "SEO Analytic",
    "description": "Adds a print button to the browser.",
    "version": "1.2",
    "manifest_version": 2,

    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js":      ["content.js"]
        }
    ],

    "permissions": ["tabs", "http://*/*", "https://*/*"],

    "browser_action": {
        "default_title": "SEO Analytic",
        "default_popup": "popup.html"
    }
}
chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getText"}, function(response) {
        if (response.method == "getText") {
            alltext = response.data;
            alert(alltext);
        }
    });
});
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
    if (request.method == "getText") {
        sendResponse({
            data: document.body.outerHTML,
            method: "getText"   // same as innerText
        });
    }
});

问题似乎是您正在使用一些不推荐的功能。具体地说,使用您的代码,我在控制台日志中得到以下错误:

响应选项卡时出错。getSelected:错误:sendRequest和onRequest已过时。请改用sendMessage和onMessage

尽管有些不推荐的东西可能仍然受到支持,但它们肯定不会持续很久,所以最好迁移到最新的API和方法。即:

  • 不要使用chrome.tabs.getSelected,而是使用
  • 使用代替chrome.tabs.sendRequest
  • 使用代替chrome.extension.onRequest
有关如何从选项卡获取一些HTML内容到弹出窗口(或背景页面)的详细答案,请参见相关问题的



顺便说一句,如果您没有使用
background.js
,您可以从清单中删除“background”键。

问题似乎在于您使用了一些不推荐使用的功能。具体地说,使用您的代码,我在控制台日志中得到以下错误:

响应选项卡时出错。getSelected:错误:sendRequest和onRequest已过时。请改用sendMessage和onMessage

尽管有些不推荐的东西可能仍然受到支持,但它们肯定不会持续很久,所以最好迁移到最新的API和方法。即:

  • 不要使用chrome.tabs.getSelected,而是使用
  • 使用代替chrome.tabs.sendRequest
  • 使用代替chrome.extension.onRequest
有关如何从选项卡获取一些HTML内容到弹出窗口(或背景页面)的详细答案,请参见相关问题的



顺便说一句,如果您没有使用
background.js
,您可以从清单中删除“background”键。

您尝试过我建议的解决方案吗?如果对你有用吗?你尝试过我提出的解决方案吗?如果对你有用吗?