Here is an example of what I'd like to do, it's been simplified for the purpose of posting here. I'd like to add several contex开发者_运维百科t menu items which actually call the same function, and then inside the function I'd like to be able to differentiate which one was called and act upon it accordingly.
This example adds two context menu items, one for shortening a link with bit.ly
and one for using tinyURL
.
chrome.contextMenus.create({'title': 'Shorten with bit.ly',
'contexts': ['all'],
'onclick': shortenLink});
chrome.contextMenus.create({'title': 'Shorten with tinyURL',
'contexts': ['all'],
'onclick': shortenLink});
The receiving function looks something like this, and by default both info
and tab
are sent along with the request, but I don't think I can figure out which context menu called the function just from that.
function shortenLink(info, tab){
}
Here's a console dump of those two variables:
I know I can set up separate functions for each item, but there are a lot of context menu items I want to add and they all share a lot of the same code so I'd really like to just have one "dispatch" function that they all use. How can I do that?
Edit
Perhaps I can figure out what menu item it was called from by using the menuItemId
variable, but can I pass an additional parameter or some additional data such as {method:'bitly'}
?
chrome.contextMenus.create({'title': 'Shorten with bit.ly',
'contexts': ['all'],
'onclick': function(info, tab) {
shortenLink(info, tab, {method:'bitly'});
});
function shortenLink(info, tab, methodObj){
...
}
精彩评论