2008-12-19 66 views

回答

4

如果你想在小工具中使用JavaScript訪問一個小工具的ID,你可以使用__MODULE_ID__。這由具有實際模塊ID的iGoogle服務器自動替換,並在許多標準庫中用作公共構造函數參數(請參見Gadgets API Reference)。

如果您使用的new gadgets API(OpenSocial的規範的一部分,目前只在iGoogle中的iGoogle沙箱),那麼你有另一種選擇:gadgets.Prefs.getModuleId()

2

我不是100%肯定你的意思,但似乎對谷歌小工具的網址是這樣的:http://hosting.gmodules.com/ig/gadgets/file/unique_id/name.xml ...,和谷歌正與他們的JavaScript完成後,他們將增加一個<一> -tag帶有onclick屬性,其中包含其他此網址。 所以,也許你可以嘗試從頁面抓取所有<a> -tags(with class delbox),並使用onclick屬性的正則表達式來獲取唯一ID。你只需確保你的代碼在Google之後執行。

像這樣的東西可以工作(未測試):

/** 
* Grabs the id of a Google Gadget from an iGoogle page. 
* 
* @param name the name of the targeted Google Gadget. If omitted the first 
*    Gadget is used 
* @return the id of the targeted Google Gadget or null if it could not be found 
*/ 
function grab_id (name) { 
    var links = document.getElementsByClass("delbox"); 
    var url = "http://hosting.gmodules.com/ig/gadgets/file/"; 
    var id_length = 21; 
    var regex = url + "[\d]{" id_length} + "}/" + name; 
    for (var i = 0; i < links.length; i++) { 
     var match = links[i].getAttribute("onclick").match(regex); 
     if (match) { 
      return match.substring(url.length+1, url.length+1+id_length); 
     } 

    } 
    return null; 
}