How to get unique Google Gadget ID from a gadget added to a iGoogle website, with Javascript?
I'm not 100% sure what you mean, but it seems that the URL for Google Gadgets is something like this: http://hosting.gmodules.com/ig/gadgets/file/unique_id/name.xml..., and after Google is done with their JavaScript, they will have added an <a>-tag with an onclick attribute containing amonst others this URL. So maybe you could try grabbing all <a>-tags (with class delbox) from the page and use a regular expression on the onclick attribute to grab the unique ID. You just have to make sure that your code executes after Google's.
Something like this could work (not tested):
/**
* 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;
}
If you are trying to access the ID of a gadget from JavaScript within the gadget itself, you can use __MODULE_ID__
. This is replaced automatically by the iGoogle server with the actual module ID, and is used in a number of the standard libraries as a common constructor parameter (see the Gadgets API Reference).
If you're using the new gadgets API (part of the OpenSocial spec and currently only available on iGoogle in the iGoogle Sandbox) then you have another option: gadgets.Prefs.getModuleId()