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:

JavaScript:
  1. GM_xmlhttprequest({
  2.     method:"GET",
  3.     url: url,
  4.     onload: function(response){
  5.         if(response.status == 301 || response.status == 302 || response.status == 303){
  6.             var loc = /Location: ([^\n]*)\n/.exec(response.responseHeaders)[1];
  7.             GM_xmlhttprequest({
  8.                 method:"GET",
  9.                 url:loc,
  10.                 onload:arguments.callee
  11.             });
  12.             return;
  13.         }
  14.         //go on with normal parsing...
  15.     }
  16.  })

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.


回覆

*必需
*必需 (不會公開)