ChrisNZL / Fauxbar

An alternative to Chrome's Omnibox.
https://chrome.google.com/webstore/detail/fauxbar/hibkhcnpkakjniplpfblaoikiggkopka
MIT License
89 stars 13 forks source link

search engine that sends POST request has its data URL-encoded twice #37

Closed warren-bank closed 5 years ago

warren-bank commented 6 years ago

for example:

the request includes the header: Content-Type: application/x-www-form-urlencoded
..so the data should be URL-encoded, but only once

warren-bank commented 6 years ago

here is the culprit..

just moving a few lines from above the if(method=="get") statement into its truthy block should do the trick..

function submitOpenSearch(query) {
    var selectedMenuItem = '.menuitem[shortname="'+(window.keywordEngine ? window.keywordEngine.shortname : window.openSearchShortname)+'"]';
    var searchUrl = $(selectedMenuItem).attr("searchurl");
    var openSearchInputVal = query ? query : (window.keywordEngine ? $("#awesomeinput").val() : $("#opensearchinput").val());

    if (localStorage.option_recordsearchboxqueries == 1 && openDb()){
        window.db.transaction(function(tx){
            tx.executeSql('INSERT INTO searchqueries (query) VALUES (?)', [openSearchInputVal.trim()]);
        }, function(t){
            errorHandler(t, getLineInfo());
        });
    }

    if ($(selectedMenuItem).length && $(selectedMenuItem).attr("method") && $(selectedMenuItem).attr("method").length && $(selectedMenuItem).attr("method").toLowerCase() == 'get') {
        searchUrl = str_replace('{searchTerms}', urlencode(openSearchInputVal), searchUrl);

        var encoding = "";
        if (window.keywordEngine) {
            encoding = window.keywordEngine.encoding;
        }
        else if (window.openSearchEncoding) {
            encoding = window.openSearchEncoding;
        }

        if (encoding == "other") {
            searchUrl = str_replace("+", "%20", searchUrl);
        }

        if (window.keywordEngine) {
            window.executingKeywordSearch = true;
        } else {
            window.usedSearchBox = true;
        }
        goToUrl(searchUrl);
    }
    else {
        searchUrl = str_replace('{searchTerms}', openSearchInputVal, searchUrl);

        $("#tempform").remove();
        $("body").append('<form method="post" action="'+ searchUrl.split('?')[0] +'" id="tempform" style="position:absolute;z-index:-999;opacity:0"></form>');
        var bits = explode('?', searchUrl);
        var params = explode('&', bits[1]);
        var elName = '';
        var elVal = '';
        for (var p in params) {
            elVal = '';
            elName = params[p].split('=')[0];
            if (params[p].split('=').length == 2) {
                elVal = params[p].split('=')[1];
            }
            $('#tempform').append('<input type="hidden" name="'+elName+'" value="'+elVal+'" />\r\n');
        }
        if (window.altReturn) {
            delete window.altReturn;
            $("#tempform").attr("target","_blank");
            if (window.middleMouse) {
                chrome.tabs.getCurrent(function(tab){
                    $("#tempform").submit();
                    setTimeout(function() {
                        chrome.tabs.update(tab.id, {selected:true});
                    }, 1);
                });
            } else {
                $("#tempform").submit();
            }
        } else {
            $("#tempform").submit();
        }
    }
}
ChrisNZL commented 5 years ago

Thanks for the fix. Added to v1.8.0.