Creating a hidden iframe in a chrome extension
Is it possible to create an iframe on the background.html page in a google-chrome extension and have it make the request to the website? I am not sure how exactly the extension works. Does chrome automatically ignore all display tags or will it run it开发者_Go百科 invisibly?
It will load the iframe all right, but I don't think it's possible to read anything out of an iframe due to same-origin restrictions. For example, this background script fails:
<body>
<script>
var iframe = document.body.appendChild(document.createElement('iframe'));
iframe.src = 'http://www.google.com';
iframe.onload = function() {
console.log(iframe.contentDocument.innerHTML);
};
</script>
with error: "Unsafe JavaScript attempt to access frame with URL http://www.google.com/ from frame with URL chrome-extension://elbpnmnjddamdmjmdnmhankaimaldhmf/background.html. Domains, protocols and ports must match."
My understanding is that the background page of an extension is never displayed, but it should "run" as expected (e.g. src
attribute of an img
tag will cause the image data to be fetched). I'm new to implementing extensions, but the architecture overview seems to imply this, as do the samples I've seen.
You can click on the link to background.html in the extensions control panel and inspect the source/dom/etc.
After testing myself: Apparently google-chrome does run iframes. It just wont display them.
精彩评论