mozilla/addon-sdk — pull request #599 by Mossop (2012-10-04 22:15:14+00:00)
Title: Bug 788324 - Re-implement context-menu API

This is the new version of the context-menu API that I've been working on. It works and passes all tests but I want to think about future improvements before doing a full review, but I think it is about time I at least heard some feedback on the way I've done this. I've maintained API compatibility aside from a couple of pieces:

* Currently contexts and content scripts are only valid for top-level Item and Menu objects. My code makes them work for Separators as well as anything in the menu hierarchy. This fixes bug 729843.
* When a menu doesn't contain anything visible I've made it hide the menu.
* Items are no longer sorted, instead they appear in the order created and all items for one add-on stay together. This fixes bug 784611.
* Added an exported contentContextMenu which is a cut down Menu and holds all the top-level items. The parentMenu property for those items no returns this rather than null as they would before. This allows re-arranging menus (where before once added to a submenu there would be no way to move an item back to the top-level). It is also a stepping stone to allowing us to target other context menus (bug 795522 and bug 795523 f.e.).
* content scripts are now first executed when the context menu requiring them is first opened. Previously as far as I can see they are run for every window and frame for every page ever opened which seems pretty costly and is also the source of a lot of issues (since the old code doesn't detect new frames well and waits until the page completes loading before executing).
* I've changed the classes used for menu items to avoid conflicts with the old module. I used classes instead of ids for the separator and overflow menu to allow it to work with multiple context menus in the same window.

The code is set up to be able to target other context menus in the future and with a couple of line changes you can add things to the tab context menu already, but this needs more work and thought and I want to play with it before getting full review as it might require further changes I haven't thought of yet.