The HTML response of a website with a domain like http://www.example.com has many javascript files. One of them refers to a javascript file in another domain, and this script tag has the crossorigin="anonymous" attribute:
<script crossorigin="anonymous" src="//cdn.example.net/script.js"></script>
I tried redirecting the request to a different URL using the Google Chrome extension:
chrome.webRequest.onBeforeRequest.addListener(function(info) { return { redirectUrl: "http://example.org/custom.js" }; }, { urls: [ "*://cdn.example.net/script.js" ], types: ["script"] }, ["blocking"]);
However, when I try to load the site, I get an error in the javascript console:
Redirection at the beginning of http://cdn.example.net blocked from downloading by Cross-Origin resource sharing policy: an incorrect answer was received.
The origin of http://www.example.com is therefore not permitted.
If I intercept the HTML response manually (outside of Chrome) and crossorigin="anonymous" attribute, it works as expected.
I have a file in http://example.org/custom.js to submit:
Access-Control-Allow-Origin: *
I also tried removing / changing the response headers from www.example.com , but that does not seem to matter. Response headers (for reference) from the main frame:
HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Vary: Accept-Encoding Status: 200 OK X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff X-UA-Compatible: chrome=1 Cache-Control: no-cache, no-store X-Frame-Options: SAMEORIGIN P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR" Content-Encoding: gzip
There are other javascript files that load after this, and depend on it, and I would like to leave them as they are.
Again, the main problem seems to be the crossorigin="anonymous" tag (which, from what I can say, supposedly has the main purpose of choosing whether the error information will be displayed )
How to write a Google Chrome extension to override javascript that runs from this source?