Skip to content

Greasemonkey xhr redirection in Firefox 3

I found that my rssget script is not working in Firefox 3 beta for some of the link. After some investigation, the cause seems to be the difference in redirection handling. In Firefox 2, when GM_xmlhttpRequest() is fired, it would follow status like 302 to do the redirection to return the correct page response to the onload function. However, in Firefox 3, the onload would receive a status like 302 and thus the script would fail.

I don’t know whether it is the responsibility for the browser or the script to handle redirection like this. I have changed the script itself to follow the redirection. The code is like:


 GM_xmlhttprequest({
    method:"GET",
    url: url,
    onload: function(response){
        if(response.status  301 || response.status  302 || response.status == 303){
            var loc = /Location: ([^n]*)n/.exec(response.responseHeaders)[1];
            GM_xmlhttprequest({
                method:"GET", 
                url:loc,
                onload:arguments.callee
            });
            return;
        }
        //go on with normal parsing...
    }
 })

I’ve only included status 301 to 303 for the time being. Then it will try to get the ‘Location’ from its header and finally invoke another GM_xmlhttpRequest(). A little trick here is to use arguments.callee to recur itself.

The updated script is tagged as 1.5 in the code site . You will only need it if your script fail in Firefox 3.

Jacky29 Apr 08 12:57 am 寫關於科技

Tags: , , [ 永久連結 | 引用 | 回應 RSS ]

發表回應