Customize contextual menu items for your Safari App Extension.
You can add items to the contextual menu that Safari shows when a user Control-clicks or right-clicks in a webpage. These actions trigger the following sequence of events:
Safari sends an event to the web document, informing it that a contextual menu is about to be displayed.
Safari displays the contextual menu.
If the user chooses one of the items associated with your app extension, Safari calls your app extension’s handler to perform the command.
To learn how to add a contextual menu item, see About Contextual Menu and Toolbar Item Keys.
Localize the Contextual Menu Item
To localize a contextual menu, provide an
Info file and include localized text for a custom key based on your command string. For example, to provide a Polish translation for a menu item whose command string is
Add, you’d add the following entry to your strings file:
For more information, see Localizing Property List Values.
Validate and Change Text in a Contextual Menu Item
validate method to validate a contextual menu item. The command parameter is the Command string you provided in the
Infoentry for the menu item. The
validation parameter is a code block where you specify whether the menu item should be hidden and what text to use for it. Using this option, you can dynamically change the text of a context menu item.
To change the menu text:
To use the default text:
To hide the menu item:
Send Custom Information to Your App Extension
contextmenu event. Your script can add a listener for this event, and react to it by inserting information into the user info dictionary. If the user later selects one of your contextual menu items, Safari sends this dictionary when it calls your app extension.
For example, to capture the currently selected text in a webpage:
Handle Contextual Menu Item Commands in Your App Extension
When the user selects one of your contextual menu items, Safari sends the
context method to your
SFSafari subclass. The
command parameter is the
Command string you provided in the
Info entry for the menu item.
For example, if you add the
Command string in your
Info entries for a contextual menu item, you might implement your context menu method to perform different actions for different commands:
You can use the