Skip to content Skip to sidebar Skip to footer

Chrome Extension Context Menu Only Works Once

I'm writing my first Chrome extension, it uses context menus (right-click and options come up). When text is highlighted and the ExtensionName option is selected from the right-cli

Solution 1:

The issue here is that you're trying to submit a form several times with different values. Based on Submitting HTML form multiple times with different values, all you have to do is to give the target of your forms a unique value and open a new tab prior to submitting the form:

rightclick.js

var i = 0;

functioncreateDirectoryRequest(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();
}

Note that you could create only one form, and simply change its attributes before submitting, rather than creating a brand new form every time:

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);   

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

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

Post a Comment for "Chrome Extension Context Menu Only Works Once"