Greasemonkey xhr redirection in Firefox 3

Tuesday, April 29, 2008

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.

Focus first text field using jQuery

Monday, April 14, 2008

When it comes to focus the first text field on a page using jQuery , the usual solution would be:

JavaScript:
  1. $(“:text:visible:enabled:eq(0)”).focus();

This query means to find the first of input text field(s), which is/are visible and enabled. However, this would also extract text field(s) which is/are under a display:none parent(s). So a more complete way would be:

JavaScript:
  1. $(“:text:visible:enabled”).filter(function(){
  2.      return $(this).parents(“:hidden”).size()==0;
  3.  }).slice(0,1).focus();

The filter here would try to determine if the text field is a descendant of a hidden ancestor. The usage of slice(0,1) instead of get(0) is to keep the chainability. A test page is put up here .

Finally, you may wrap it up with a plugin:

JavaScript:
  1. (function($){
  2.     $.fn.focusFirstField = function(){
  3.         $this = this;
  4.         $this.find(“:text:visible:enabled”).filter(function(){
  5.             return $(this).parents(“:hidden”).size() == 0;
  6.         }).slice(0,1).focus();
  7.         return this;
  8.     }
  9. })(jQuery)

So that you can do this:

JavaScript:
  1. $(“#form1,#form2”).focusFirstField(); //form1 or form2 may be ‘hidden’ dynamically

I just wonder if there is any ‘selector-only’ solution for this?

Update: It turns out that the child’s visiblity would override parent’s visibility. So the filter should only check display none:

JavaScript:
  1. $(":text:visible:enabled").filter(function(){
  2.      return $(this).parents.filter(function(){
  3.          return this.style.display == "none"; })
  4.     .size()==0;
  5.  }).slice(0,1).focus();

I’ve updated the demo page .

jQuery validation 的 addMethod

Thursday, March 27, 2008

jQueryValidation plugins 是很強大的,基本上可以滿足各種需要。剛剛被一個小問題困擾了好一陣子,特此記下。我寫了類似的東西:

HTML:
  1. <div id=“error” style=“display:none”><ul></ul></div>
  2.  <form id=“form1”>
  3.      <input type=“hidden” name=“fff” id=“fff” value=“” class=“special” />
  4.  </form>

JavaScript:
  1. $.validator.addMethod(“special”, function(value, element, params){
  2.     //validate….
  3.     return result;
  4.  }, “This is a special checking”);
  5.  
  6.  $(document).ready(function(){
  7.      $(“#form”).validate({
  8.         messages:{ fff: {special: “This is a special checking!”} },
  9.         errorContainer: “#error”,
  10.         errorLabelContainer: “#error ul”,
  11.         wrapper: “li”
  12.      });
  13.  });

結果怎樣也出不到那個自訂的 validation rule,苦苦深研追踪 source code 後才發現,問題在於 addMethod 裏的 method 參數問題,而正確的寫法是:

JavaScript:
  1. $.validator.addMethod(“special”, function(){
  2.     //validate…
  3.     return result;
  4.  }, “This is a special checking”);

那個 method 的參數數量如果大於 3,plugins 會認為你所加的是有參數的 validation,即在 element class 可能是 {special:abc} 之類,所以當看到 input 裏只是 special 就不會偵測到。

這樣很「正路」吧?但花了不少時間去看才知道。

蘋果動新聞 RSS 加取全文油猴更新

Wednesday, February 20, 2008

蘋果日報最近推出了 動新聞 不過又沒有 RSS,所以特地用 Dapper 打造了其首頁連結的 RSS ,大家可以拿來用。

美中不足的是只有標題連結,沒有內文,所以我更新了 取全文油猴腳本 ,可以照辦煮碗按 ‘G’ 取全文,大家可以點選安裝: Google Reader 版Bloglines 版Bloglines Beta 版

網摘流程

Tuesday, February 19, 2008

(RSS用戶可能看不到影片,請點到本站看 screencast)

這次試用一下 freescreencast.com 的服務,來介紹一下我做網摘的流程,一 take 過,中間有少許「蝦碌」,反應有些遲鈍,不過懶得再錄了。錄的過程還算是相當簡單,只不過 mouse cursor 怪怪的。

這不能算是教學影片,因為一些都是很 tailor-made 給我自用的,例如我的 wordpress 裏有使用 textile,所以會輸出格式為 textile 的連結。不過重點是:

  • 使快捷的工具,例如使用 del.icio.us bookmark toolbar 就比起用 bookmarklet 快得多
  • 在摘時寫 description,將工作分散成很多小份,留在最後一定寫不了的
  • 可以自動化的地方就自動化,例如用 bookmarklet 幫手轉格式