Getting the source code of a page from a googlechrome extension
I am writting an extension for googlechrome to display a list of items from a website. The problem I have is that I cannot get the source code of the page I am looking for. When I tried putting it in an iframe, it had code that would change the location of the window. XMLhttpRequest is also only allowed on your own domain. Is 开发者_JAVA技巧there any way I can get the source code for a site from a googlechrome extension through javascript?
here is the manifest:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions": [
"http://api.flickr.com/",
"http://search.twitter.com/"
]
}
It's all described in details here. Long story short:
- Declare domain permissions in the manifest.
- Make cross-domain request from a background page.
- Transfer results to a content script using message passing, if needed.
UPDATE
Here is code that works for me:
Manifest - exactly as yours.
popup.html:
<html>
<head>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
alert(xhr.status);
if (xhr.status == 200) {
alert(xhr.responseText);
}
}
}
var url = "http://search.twitter.com/trends/current.json?exclude=hashtags";
xhr.open("GET", url, true);
xhr.send();
</script>
</head>
<body></body>
</html>
can you run the site in an iframe and then use some javascript to run:
sourceCode=[iframename].document.body.parentNode.innerHTML;
sourceCode='<html>'+sourceCode+'</html>'
I don't know if this works across domains
精彩评论