Context menu api - conditional context menu item?

765 views
Skip to first unread message

rojer

unread,
Sep 27, 2011, 11:51:41 AM9/27/11
to Chromium-extensions
I have a small extension that adds a context menu item.
However, it has a very specific use case in which the item it adds may
actually be used.
When context menu is being draw, I'd like to be able to decide whether
to display my item or not - most of the time it should not be
displayed. Currently the contextMenus API doesn't seem to allow any
sort of conditional operation: you create() an item and register an
action callback, and the item is included in every context menu from
then on.
What I would like to do is to register a "tasting" callback that would
be called each time a context menu is about to be drawn, and have a
chance to decide whether my item is useful in that particular context
or not.
Does it make sense to make such a feature request?

Boris Smus

unread,
Sep 28, 2011, 10:58:06 AM9/28/11
to rojer, Chromium-extensions
Hi Rojer,

The context menu API provides some built-in functionality to let your context menu load conditionally, such as the `contexts`, `documentUrlPatterns` and `targetUrlPatterns` fields.

If your condition is relatively simple, you may be able to use one of the fields I mentioned to implement the behavior you want. 

- Boris


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


rojer

unread,
Sep 28, 2011, 12:24:50 PM9/28/11
to Chromium-extensions
sure, and i already target "selection". but i'd like to "taste" what's
selected before deciding whether i have anything useful to offer.
my test would be simple - a regex match.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

Paolo Casaschi

unread,
Sep 28, 2011, 2:58:00 PM9/28/11
to Chromium-extensions
You might want to star and/or comment to this bug

http://crbug.com/77023

The idea is to let the content script examine the link and decide
whether to add a context menu or not. It should help you, but you
might want to describe your use case.

rojer

unread,
Sep 28, 2011, 4:26:55 PM9/28/11
to Chromium-extensions
On Sep 28, 7:58 pm, Paolo Casaschi <news...@casaschi.net> wrote:
> You might want to star and/or comment to this bug
>
> http://crbug.com/77023

um... no, that's not quite what i want. filed http://crbug.com/98442
instead.

Paolo Casaschi

unread,
Sep 28, 2011, 4:35:43 PM9/28/11
to Chromium-extensions
There's also some related discussion here: http://crbug.com/60758

As you can read, some developer seem reluctant adding a callback
function to the context menu, being afraid that a poorly coded
callback function might affect the responsiveness of chrome.

On Sep 28, 9:26 pm, rojer <roj...@gmail.com> wrote:
> On Sep 28, 7:58 pm, Paolo Casaschi <news...@casaschi.net> wrote:
>
> > You might want to star and/or comment to this bug
>
> >http://crbug.com/77023
>
> um... no, that's not quite what i want. filedhttp://crbug.com/98442

Paolo Casaschi

unread,
Sep 29, 2011, 4:51:09 AM9/29/11
to Chromium-extensions
Before your feature request is addressed, an alternative approach
could be to use a page action instead of (or in parallel to) the
context menu. Page actions are designed to alert the user and offer
actions that apply in a limited number of pages under certain
conditions.
You'd need a content script detecting when the page meets your
criteria (and you have full flexibility to check whatever you want
here), in case then you trigger a page action with a popup offering a
button (or similar) to execute the action you currently assign to the
context menu.
This approach would also have the advantage that the user would be
alerted (by the page action icon in the omnibar) when that specific
action is available instead of having to know beforehand when it's
available and right click to get the context menu.
Just an idea.
Reply all
Reply to author
Forward
0 new messages