Personal tools

Mobile/Fennec/Extensions/UserInterface/Context Menu

From MozillaWiki

Jump to: navigation, search

Contents

Extending the Context Menu

fennec-contextpanel-02-300x239.png

The context menu appears when a user does a "long tap" (touch and hold) on a link, image, or other page/UI element. See Mark Finkle's blog post for more details.

To add a new command to the context menu, add a richlistitem element to the #context-commands list. For example, the Show Image Title add-on uses a XUL overlay like this:

 <richlistbox id="context-commands">
   <richlistitem id="showtitle-context-command"
                 class="context-command"
                 type="image"
                 onclick="ShowTitle.onclick();">
     <label value="Show Title"/>
   </richlistitem>
 </richlistbox>

Target types

The type attribute contains a space-delimited list of type names. If the the target element (the page element the user touched) matches any of the included types, then the menu item will be visible. Otherwise it will be hidden.

The available types are:

image
An image (<img>) element.
image-loaded
An image that has finished loading succesfully.
link
A link (<a href="...">) element.
mailto
A link to a mailto: URI.
callto
A link to a telphone URI (callto:, tel:, sip:, or voipto:).
video
(new in Fennec 2.0b1) A <video> element.
link-saveable
(new in Fennec 2.0a1) Any link type except mailto:, javascript:, and news:.
link-shareable, image-shareable, video-shareable
(new in Fennec 2.0a1) Any link type except chrome:, about:, file:, javascript:, or resource:.

Adding new target types

New in Fennec 2.0a1: If the built-in types do not work for your add-on, you can use ContextHandler.registerType to add one of your own:

 ContextHandler.registerType(name, callback); // Requires Fennec 2.0a1 or higher

registerType takes two arguments: the type name (a string), and a callback function. The callback should take two arguments: the popupState object and the target DOM element. It can modify the popupState to add new data that will be passed to the commands' onclick handlers (see below). The callback can return true if the target element "matches" the new type, or false otherwise.

ContextHandler.registerType must be called in the content process using a loadFrameScript.

For example, if we want to change the "Show Image Title" example above to show the menu item only for images that actually have titles, then we could change type="image" to type="show-title" in the XUL, and add the following code in a content script:

 ContextHandler.registerType("show-title", function(popupState, element) {
   if (element.title) {
     popupState.title = element.title;
     return true;
   }
   // Otherwise, do not show commands with type="show-title":
   return false;
 });

Handling context command clicks

When the user taps a context command, its onclick handler is called. This handler can use the ContextHelper object to get more information about the element that was the target of the long-tap gesture.

ContextHelper is changing in Fennec 2.0. The "popupNode" property is no longer accessible because of Electrolysis, so it is replaced by the new "popupState" object.

The "Show Image Title" example above uses a handler like this, which works in both 1.1 and 2.0:

ShowTitle.onclick = function() {
  var text = ContextHelper.popupNode.title || ContextHelper.popupNode.title || 'no text';
  setTimeout(function() {
    var promptService =
      Components.classes['@mozilla.org/embedcomp/prompt-service;1'].
        getService(Components.interfaces.nsIPromptService);
    promptService.alert(null, 'Image Title', text);
  }, 0);
};

ContextHelper properties

Fennec 1.1

ContextHelper.popupNode
The DOM element that received the long-tap gesture.
ContextHelper.linkURL
The address of the link.
ContextHelper.mediaURL
The address of the image.
ContextHelper.popupState.linkProtocol
The URI scheme of the link address.
ContextHelper.onSaveableLink
true if the link is not a javascript/mailto/news link.

Fennec 2.0

ContextHelper.popupState
An object containing more information about the target element.
ContextHelper.popupState.types
An array of the type names that the target element matched.
ContextHelper.popupState.label
A string describing the target element.
ContextHelper.popupState.linkTitle
The text of the link.
ContextHelper.popupState.linkURL
The address of the link.
ContextHelper.popupState.linkProtocol
The URI scheme of the link address.
ContextHelper.popupState.mediaURL
The image or video's source address.

Changes in Fennec 2.0

Here are some tips for updating Fennec 1.1 add-ons to support Fennec 2.0:

  • Add class="context-command" to your richlistitem elements.
  • To support both 1.1 and 2.0, check for properties in both popupNode and popupState (see above for an example).
  • If you use registerType to create a custom type in Fennec 2.0, you can continue to support Fennec 1.1 using a built-in type. Here's a code snippet from Show Image Title:
if ("popupNode" in ContextHelper) {
  // Fennec 1.1
  document.getElementById("show-title-command").setAttribute("type", "image");
} else {
   // Fennec 2.0
  messageManager.loadFrameScript("chrome://showtitle/content/content.js", true);
}

Sample Code