2008年11月28日 星期五

O'REILLY InsideRIA AJAX framework 投票結果

雖然 Richard 似乎強力推薦 YUI,不過還沒拿到 YUI 的投影片,也沒打算去 survey。不過 O'REILLY 最近的投票結果出爐,還是 Dojo 奪魁。

2008年11月27日 星期四

Firefox 書籤工具列消失;或是一片空白

症狀是:
「書籤工具列」顯示時一片空白。

解決方法

工具列上按右鍵→自訂,將「書籤工具列」圖示拖拉到工具列上即可

2008年11月20日 星期四

IE7Pro Compatibility Script

IE7Pro Compatibility Script 將 Firefox 的 GreaseMonkey 語法移植到 IE7 上的 script。直接安裝後即可使用。
支援語法:
  • GM_setValue
  • GM_getValue
  • GM_log
  • GM_openInTab
  • GM_addStyle
  • GM_xmlhttpRequest
// FremyCompany Script
// version 0.1 build 4 ALPHA!
// 28/03/07
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// IE Pro 9.12 or later (see at : http://www.iepro.com)
// Then restart Internet Explorer and revisit this script.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          .FremyCompany Script
// @description   Scripts made by FremyCompany to make IE Pro more compatbile with GreaseMonkey
// @include       *
// ==/UserScript==

(function(w,d) {
    w.GM_setValue=function(name, value) {
        return PRO_setValue(name, value);
    };
    w.GM_getValue=function(name, defaultValue) {
        return PRO_getValue(name, defaultValue);
    };
    w.GM_log=function(text) {
        return PRO_log(text);
    };
    w.GM_openInTab=function(url) {
        return PRO_openInTab(url);
    };
    w.GM_addStyle=function(css) {
        return PRO_addStyle(css);
    };
    w.GM_xmlhttpRequest=function (d) {
        // url, method, headers, data, onload, onerror, onreadystatechange;
        if (!d.url) { GM_log("URL is missing in xhr-call"); return false; }
        if (!d.method) { d.method="POST"; }
        if (!d.headers) { d.headers=new Array(); }
        var xhr={"readyState":0,"status":0,"statusText":"not loaded"};
        var xhr_value=xhr;
        try {
            xhr=GM_xmlhttpRequest._createRequest();
            
            // OPENING of the xhr
            xhr.open(d.method, d.url, true);
            for (h in d.headers) {
                try { xhr.setRequestHeader(h, d.headers[h]); } catch (ex) {}
            }
            
            // CallBack gesture
            xhr.onreadystatechange=function() {
                GM_xmlhttpRequest._createValue(xhr,xhr_value);
                try { d.onreadystatechange(xhr_value); } catch (ex) {}
                if (xhr.readyState==4) {

                    if (xhr.status==200||xhr.status==0) {
                        try { d.onload(xhr_value); } catch (ex) {}
                    } else {
                        try { d.onerror(xhr_value); } catch (ex) {}
                    }
                }
            }
            
            // SENDING the data
            xhr.send(d.data?d.data:null)
        } catch (ex) {
            try { d.onerror(xhr_value, ex); } catch (ex2) {}
        }
    };
    w.GM_xmlhttpRequest._createRequest=PRO_xmlhttpRequest;
    w.GM_xmlhttpRequest._createValue=function(x,v) {
        v.abort=function() { x.abort(); }
        v.getResponseHeader=function(n) { return x.getResponseHeader(n);  }
        v.getAllResponseHeaders=function() { return x.getAllResponseHeaders(); }
        v.responseText=x.responseText;
        v.responseHeaders=x.getAllResponseHeaders();
        v.responseText=x.responseText;
        v.responseBody=x.responseBody;
        v.readyState=x.readyState;
        v.status=x.status;
        v.statusText=x.statusText;
    };
})(window,document);

2008年11月10日 星期一

GM_xmlhttpRequest 和 unsafeWindow 的限制

詳細的限制請參考 http://wiki.greasespot.net/0.7.20080121.0_compatibility 的描述。主要的症狀是你會發現錯誤訊息出現:
Greasemonkey access violation: unsafeWindow cannot call GM_xmlhttpRequest. 

解決方法如下:

unsafeWindow.someObject.registerCallback(function(asin) {
  window.setTimeout(function() {  // 重點是利用 window.setTimeout
    GM_xmlhttpRequest({
       method: "GET",
       url: "http://www.amazon.com/asin/" + asin;
    });
  }, 0);    // <<<
});

實際例子

$("span[name!=NOT_AVAILABLE]").click(function(){
  GM_openInTab(entrezGene+$(this).attr("name"),1);
}).mouseover(function(){
  var url=url+$(this).attr("name")+".htm";
  window.setTimeout(function(ref){
    GM_xmlhttpRequest({
      method: "GET",
      url: url+"",
      onload: 
      function(xhr) {
 ref.attr("title","Information from Entrez Gene|"+xhr.responseText).cluetip(
 {width:400,splitTitle: '|',arrows: true,dropShadow: false, cluetipClass: 'gene', waitImage: true}
        );
      }
    });
  },0,$(this));  // 用這種方法可以將 $(this) 傳進去
});

2008年11月6日 星期四

Greasemonkey and Internet Explorer

目前看來最 active 開發的就是 IE7Pro。查了一下,從 2007 年三月起的 0.9.12 版,IE7Pro 開始支援 userscript。搭配上 Compatibility Script for IE7Pro,應該是目前跟 GreaseMonkey 本尊支援度最好的。使用上與語法也很類似 Greasemonkey。安裝好 Compatibility script 後,甚至可以直接使用 GM_ 系列的 API

其它 Greasemonkey4IE 工具