Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome Chrome扩展上下文菜单只工作一次_Google Chrome_Javascript Events_Google Chrome Extension - Fatal编程技术网

Google chrome Chrome扩展上下文菜单只工作一次

Google chrome Chrome扩展上下文菜单只工作一次,google-chrome,javascript-events,google-chrome-extension,Google Chrome,Javascript Events,Google Chrome Extension,我正在编写我的第一个Chrome扩展,它使用上下文菜单(右键单击并显示选项)。当突出显示文本并从右键单击菜单中选择ExtensionName选项时,它将打开一个新选项卡,并将其作为查询传递到我的组织拥有的目录。问题是它只能工作一次。当文本高亮显示并右键单击时,上下文菜单中的选项仍会出现,但选择ExtensionName选项无法生成另一个包含目录结果的选项卡 我希望用户能够像大多数其他Chrome扩展一样,在浏览器会话中根据需要多次使用它。在Chrome扩展菜单中重新加载扩展可以解决此问题。我假设

我正在编写我的第一个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());

我知道这可能不是一个困难的问题,我只是难以置信的新手,所以任何帮助都将不胜感激。谢谢。

这里的问题是,您尝试多次提交具有不同值的表单。基于,您所要做的就是为表单的目标指定一个唯一的值,并在提交表单之前打开一个新选项卡:

右击.js

var i=0;
函数createDirectoryRequest(选择文本){
var form=document.createElement(“表单”);
form.setAttribute(“方法”、“帖子”);
setAttribute(“action”,“目录的URL在这里”);
//每个表单的唯一目标;在新选项卡中打开它
form.setAttribute(“目标”、“窗口”+i);
打开(“目录的URL在这里”,“窗口”+i);
i++;
var hiddenField=document.createElement(“输入”);
setAttribute(“id”、“搜索\通用\搜索\术语”);
setAttribute(“名称”、“搜索[通用搜索词]”);
setAttribute(“值”,选择文本);
表格.appendChild(hiddenField);
文件.正文.附件(表格);
表单提交();
}
请注意,您只能创建一个表单,在提交之前只需更改其属性,而不是每次都创建一个全新的表单:

var i=0;
var form=document.createElement(“表单”);
form.setAttribute(“方法”、“帖子”);
setAttribute(“action”,“目录的URL在这里”);
var hiddenField=document.createElement(“输入”);
setAttribute(“id”、“搜索\通用\搜索\术语”);
setAttribute(“名称”、“搜索[通用搜索词]”);
表格.appendChild(hiddenField);
文件.正文.附件(表格);
函数createDirectoryRequest(选择文本){
form.setAttribute(“目标”、“窗口”+i);
打开(“目录的URL在这里”,“窗口”+i);
i++;
setAttribute(“值”,选择文本);
表单提交();
}

我使用您的第二个选项。我处理过的唯一一个问题是
window.open(“目录的URL在这里”,“window”+I),我必须将URL部分留空,它才能正常工作。我不知道这是否是一个普遍的问题,或者只是因为我的目录才发生的事情,但不管怎样,谢谢你的帮助!