I am making a userscript for the TamperMonkey Chrome extension (it is the same than making a userscript for GreaseMonkey).
In my script, I am iterating over some document external resources (img, css, links, scripts) via document.getElementsByTagName()
and I am changing their src
or href
attributes to another url.
For the majority of cases, the new url is loading properly but in some cases, it ends on a 404 or 403 error from my proxy.
Could you give me some pointers about how to handle (if possible) properly the "resource cannot be loaded with the new url" ? Also, in the case where the asked resource result in a redirected url, can I be told that the resource was redirected (how to check for the 302) ?
I am using bare javascript for now, no jquery. But if jQuery can help me, I am willing to adopt it.
I considered about testing the headers from XMLHttpRequest result for every resource url, but it looks to me like using a artillery gun to kill a fly. I would rather use a proper handler that could be triggered just in case of unavailable resource.
Edit : alternatively, I would be ok if chrome would have a sort of status property for each loaded object that I could check...
I would check the document.styleSheets in javascript.
Here is a working jsfiddle for stylesheets:
http://jsfiddle.net/BQxBz/4/
For javascript I would use object detection: thus see if a certain object that should be loaded by a certain script. For example to detect jQuery:
http://jsfiddle.net/TqQtE/
It's just an idea, I'm not sure about the viability but it might be worth trying. Explaining in code:
I'm not sure if the
onload
,onerror
handlers work when you change thesrc
of an existingscript
tag.Please report your findings. ;)
well... that's what i did, but our scripts have different purposes.
and then
check mine here: RandomProxyHeader