Visit Mozilla.org

Using the Places tagging service

From MDC

This article covers features introduced in Firefox 3


The tagging service, offered by the nsITaggingService interface, provides methods to tag and untag a URI, to retrieve URIs for a given tag, and to retrieve all tags for a URI.

Contents

[edit] Initiating the tagging service

Before using the tagging service, you need to obtain a reference to an instance of it:

var taggingSvc = Components.classes["@mozilla.org/browser/tagging-service;1"]
                           .getService(Components.interfaces.nsITaggingService);

[edit] Tagging a URI

The tagURI() method tags a URL with the given set of tags. Current tags set for the URL persist, and tags which are already set for the given URL are ignored.

taggingSvc.tagURI(uri("http://example.com/"), ["tag 1"], 1); //First argument = URI
                                                             //Second Argument = Array of tag(s)

[edit] Untagging a URI

untagURI() removes tags from a URL. From the given set of tags, a tag is ignored if it isn't set for the given URL.

tagginSvc.untagURI(uri("http://example.com/"), ["tag 1"], 1); //First argument = URI
                                                              //Second Argument = Array of tag(s)

[edit] Finding all URLs with a given tag

The getURIsForTag() method returns an array of all URLs tagged with the given tag.

var tag1uris = taggingSvc.getURIsForTag("tag 1"); //"tag 1" = given tag

[edit] Getting all tags associated with a URL

The getTagsForURI() method returns an array of all tags set for the given URL.

var tags = taggingSvc.getTagsForURI(uri("http://example.com/", {})); 
  //tags = an array of tags stored in http://example.com/

[edit] See also