1

So i want to get html of a selected tab when i press OK. But something doesnt work. What am i missing?

manifest.json

{
  "name": "extension",
  "version": "1.0",
  "description": "discription",
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["contentscript.js"]
    }
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "popup": "popup.html"
  },
  "permissions": [
    "http://ajax.googleapis.com/",
    "tabs"
  ]
}

popup.html

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
</head>
<body>
<script>

function getHTML()
{

chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getHTML"}, function(response) {
        if(respond.method="getHTML"){               
            alert(response.html);  
      }
    });
});

}
</script>
 <input type="submit" value="OK" onclick ="getHTML()" />
</body>

contentscript.js

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if(request.method == "getHTML"){
            sendResponse({html: document.all[0].outerHTML});
        }
    }
);
3
  • It's hard to see the problem as it is. 1) What do you mean by "does not work"? What do you see. 2) Have you tried debugging your extension? did you come up with a more focused point-of-failure ?
    – ronme
    Jan 13, 2012 at 16:40
  • there is no alert message even, when i ckick ok nothing happens
    – Stals
    Jan 13, 2012 at 16:59
  • 1
    have you tried debugging it with the chrome debugger ?
    – ronme
    Jan 13, 2012 at 17:14

2 Answers 2

2

I see two errors: 1) popup.html is checking for "respond" instead of "response". 2) popup.html is checking for response.method to be "getHTML", but the content_script never sets the "method" field.

2
  • I strongly suggest you use the chrome debugger to see what's going on. It'll save you a lot of time in future.
    – ronme
    Jan 14, 2012 at 19:40
  • Well done! i hope you move forward swiftly. I'm also going to delete my older comments to leave this question "clean" for future users. I suggest you do the same :)
    – ronme
    Jan 15, 2012 at 16:45
0

document.all is undefined in Google Chrome!

Replace it with document.getElementsByTagName("html")[0].innerHTML:

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if(request.method == "getHTML"){
            sendResponse({html: document.getElementsByTagName("html")[0].innerHTML});
        }
    }
);
1
  • I'm not sure that's correct - trying document.all and document.all[0] on the chrome debugger seems to work perfectly. Even otherwise, he can probably use document.documentElement instead.
    – ronme
    Jan 13, 2012 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.