I have a chrome extension which monitors the browser in a special way, sending some data to a web-server. In the current configuration this is the localhost. So the content script contains a code like this:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data)...
xhr.open('GET', url, true);
xhr.send();
where url parameter is ‘http://localhost/ctrl?params’ (or http://127.0.0.1/ctrl?params – it doesn’t matter).
Manifest-file contains all necessary permissions for cross-site requests.
The extension works fine on most sites, but on one site I get the error:
XMLHttpRequest cannot load http://localhost/ctrl?params. Origin http://www.thissite.com is not allowed by Access-Control-Allow-Origin.
I’ve tried several permissions which are proposed here (*://*/*, http://*/*, and <all_urls>), but no one helped to solve the problem.
So, the question is what can be wrong with this specific site (apparently there may be another sites with similar misbehaviour, and I’d like to know the nature of this), and how to fix the error?
(tl;dr: see two possible workarounds at the end of the answer)
This is the series of events that happens, which leads to the behavior that you see:
<script>tag that asynchronously loads the Facebook Connect script:(function() { var e = document.createElement('script'); e.type = 'text/javascript'; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }());DOMContentLoadedevent fires. Since your content script uses"run_at" : "document_end", it gets injected and run at this time.loadevent fires):window.onload = function() { // code that eventually does the cross-origin XMLHttpRequest };loadevent handler, which it adds with this snippet:(function() { var oldonload=window.onload; window.onload=function(){ // Run new onload code if(oldonload) { if(typeof oldonload=='string') { eval(oldonload); } else { oldonload(); } } }; })();(this is the first key part) Since your script set the
onloadproperty,oldonloadis your script’s load handler.loadevent handler fires.loadhandler is run, which run its own code, and then invokesoldonload. (this is the second key part) Since the page is invoking yourloadhandler, it’s not running it in your script’s isolated world, but in the page’s “main world”. Only the script’s isolated world has cross-originXMLHttpRequestaccess, so the request fails.To see a simplified test case of this, see this page (which mimics http://www.wix.com), which loads this script (which mimics Facebook Connect). I’ve also put up simplified versions of the content script and extension manifest.
The fact that your
loadhandler ends up running in the “main world” is most likely a manifestation of Chrome bug 87520 (the bug has security implications, so you might not be able to see it).There are two ways to work around this:
"run_at" : "document_end"and aloadevent handler, you can use the default running time (document_idle, after the document loads) and just have your code run inline.loadevent handler by setting thewindow.onloadproperty, usewindow.addEventListener('load', func). That way your event handler will not be visible to the Facebook Connect, so it’ll get run in the content script’s isolated world.