如何在chrome.contextMenus.create onClick函数中获取TITLE的引用?

时间:2012-05-06 08:53:55

标签: javascript google-chrome-extension

我想在onClick函数中获取chrome.contextMenus.create title属性的引用。

例如:

chrome.contextMenus.create({"title": "sometitle", "contexts":["selection"], "onclick": searchSelection});

function searchSelection(info, tab){
    var query = "<i want title (sometitle) here>"+info.selectionText;
    var url = "http://www.google.com/search?q="+query;
    chrome.tabs.create({url: url});
}

我搜索过,但没有选择这样做。


+1问题:有没有办法内联编辑contextMenu项目?或者在CM元素后附加一个可编辑的输入字段?我认为没有,但值得提问:)

1 个答案:

答案 0 :(得分:0)

没有现有的API方法来检索有关给定菜单项的信息,因为这些信息很少需要,并且已经可供扩展程序的开发人员使用。

这是实现结果的通用功能:

/**
 * Creates a menu item using chrome.contextMenus.create.
 * When the second argument is specified, the click handler receives a
 *  third argument: The original creation data.
 * When the "onclick" property is set in the creationData, the "onclick"
 *  event does not receive a third parameter.
 *
 * @param object creationObject    Basic creation object
 * @param function onclickHandler  "click" property of the creationObject
 */
function createMenuItem(creationObject, onclickHandler) {
    if (onclickHandler) {
        creationObject.onclick = function(onClickData, tab) {
            onclickHandler(onClickData, tab, creationObject);
        };
    }
    return chrome.contextMenus.create(creationObject);
}

// Usage:
createMenuItem({"title": "sometitle", "contexts":["selection"]}, searchSelection);
function searchSelection(info, tab, creationData) {
    var query = "<i want title " + creationData.title + " here>" + info.selectionText;
    var url = "http://www.google.com/search?q=" + query;
    chrome.tabs.create({url: url});
}

不,没有办法添加可在线编辑的菜单项(the documentation, chrome.contextMenus.create中提到了唯一可用的选项)。