Chrome扩展程序上下文菜单仅适用一次

时间:2013-10-15 16:14:41

标签: google-chrome javascript-events google-chrome-extension

我正在编写我的第一个Chrome扩展程序,它使用上下文菜单(右键单击和选项出现)。当突出显示文本并从右键单击菜单中选择ExtensionName选项时,它会打开一个新选项卡并将其作为查询传递给我的组织所拥有的目录。问题是它只能工作一次。突出显示文本并右键单击时,仍会显示上下文菜单中的选项,但选择ExtensionName选项无法生成包含目录结果的另一个选项卡。

我希望用户能够在浏览器会话中根据需要多次使用此功能,就像大多数其他Chrome扩展程序一样。重新加载Chrome扩展程序菜单中的扩展程序可解决此问题。我假设它与我的后台脚本rightclick.js中发生的事件处理有关。我不熟悉JavaScript中的事件处理或Chrome的上下文,所以任何帮助都会受到赞赏。

的manifest.json

{
    "name": "ExtensionName",
    "description": "Right-click text to search the given directory.",
    "manifest_version": 2,
    "version": "1.0",
    "permissions": ["contextMenus"],
    "icons": {
        "16": "icon-bitty.png",
        "48": "icon-small.png",
        "128": "icon-large.png"
    },
    "background": {
        "scripts": ["rightclick.js"]
    }
}

rightclick.js

var selection_callbacks = []; 

chrome.extension.onMessage.addListener(function (request) { 
    var callback = selection_callbacks.shift(); 
    callback(request); 
});


function getSelection(callback) { 
    selection_callbacks.push(callback); 
    chrome.tabs.executeScript(null, { file:"selection.js" }); 
};

function createDirectoryRequest(selectText) {
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "THE URL FOR THE DIRECTORY GOES HERE");
    form.setAttribute("target", "_blank");

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("id", "search_generic_search_terms);");        
    hiddenField.setAttribute("name", "search[generic_search_terms]");
    hiddenField.setAttribute("value", selectText);
    form.appendChild(hiddenField);
    document.body.appendChild(form);        
    form.submit();

}

chrome.contextMenus.create({title: 'ExtensionName "%s"', 
                    contexts:["selection"], 
                    onclick: function(info, tab){ createDirectoryRequest(info.selectionText); }})

selection.js

chrome.extension.sendResponse(window.getSelection().toString());

我知道这可能不是一个难题,我对此非常新,所以任何帮助都会受到赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

这里的问题是您尝试使用不同的值多次提交表单。基于Submitting HTML form multiple times with different values,您只需在提交表单之前为表单的目标提供唯一值并打开新标签:

<强> rightclick.js

var i = 0;

function createDirectoryRequest(selectText) {
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "THE URL FOR THE DIRECTORY GOES HERE");

    // unique target for each form; open it in a new tab
    form.setAttribute("target", "window" + i);
    window.open("THE URL FOR THE DIRECTORY GOES HERE", "window" + i);
    i++;

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("id", "search_generic_search_terms);");        
    hiddenField.setAttribute("name", "search[generic_search_terms]");
    hiddenField.setAttribute("value", selectText);
    form.appendChild(hiddenField);
    document.body.appendChild(form);        
    form.submit();
}

请注意,您只能创建一个表单,只需在提交前更改其属性,而不是每次都创建一个全新的表单:

var i = 0;

var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "THE URL FOR THE DIRECTORY GOES HERE");

var hiddenField = document.createElement("input");
hiddenField.setAttribute("id", "search_generic_search_terms);");        
hiddenField.setAttribute("name", "search[generic_search_terms]");
form.appendChild(hiddenField);
document.body.appendChild(form);   

function createDirectoryRequest(selectText) {
    form.setAttribute("target", "window" + i);
    window.open("THE URL FOR THE DIRECTORY GOES HERE", "window" + i);
    i++;

    hiddenField.setAttribute("value", selectText);    
    form.submit();
}