1

I use document.body.outerHTML to get html from a tab, but I can't do it

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"
    }
}

popup.js:

chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getText"}, function(response) {
        if (response.method == "getText") {
            alltext = response.data;
            alert(alltext);
        }
    });
});

content.js:

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
    if (request.method == "getText") {
        sendResponse({
            data: document.body.outerHTML,
            method: "getText"   // same as innerText
        });
    }
});
gkalpak
  • 47,844
  • 8
  • 105
  • 118

1 Answers1

1

Seems like the problem is that you are using some deprecated features. Specificaly, using your code, I get the following errors in the console log:

Error in response to tabs.getSelected: Error: sendRequest and onRequest are obsolete. Please use sendMessage and onMessage instead.

Although some of the deprecated stuff are possibly still supported, they sure won't be for long, so it is a good idea migrate to up-to-date APIs and methods. Namely:

For a detailed answer on how to get some HTML content from a tab to the popup (or background page), see my answer to a related question.


BTW, if you are not using background.js, you can remove the "background" key from the manifest altogether.

Community
  • 1
  • 1
gkalpak
  • 47,844
  • 8
  • 105
  • 118