Custom Browser Search Engines and Multiple Parameters

948 views Asked by At

I have been working with custom search engines in Chrome and Vivaldi, which have been absolutely fantastic. However, I have only been able to successfully perform searches that replace a single parameter (e.g., the %s in the following URL: http://www.thesaurus.com/browse/%s) with my search term (e.g., http://www.thesaurus.com/browse/trifocal).

Is it possible to replace multiple parameters for a custom browser search engine so I could not only specify a search term but also the webpage to search? For example, I would type the custom search engine shortcut "d" followed by "thesaurus, trifocal", which would input the two parameters into a predefined URL and search the word "trifocal" on "thesaurus.com".

Basically, I want to be able to search for multiple values at two different points in the custom search engine for scenarios where multiple sites use the same "base" url where the only difference is a word or two.

Please let me know if you have any questions.

1

There are 1 answers

1
arunkumaraqm On

You can use JavaScript. Take the entire string as input to the function. Split it based on some character and them trim the parts. Construct the link and then open it within the current tab or in a new tab, depending on which suits you.

javascript:(function myFunction(str) {
    var part = ";";
    var res = str.split(part);
    
    var search_query = res[0].trim(); 
    var category = res[1].trim();
    var new_tab = res[2];

    var res_url = "https://github.com/search?q=" + search_query + "&type=" + category;

    // open in new tab if str contains a third parameter that is composed of zero or more spaces
    if ((new_tab !== undefined) && (new_tab.trim() === "")) 
    {
        window.open(res_url, "_blank");
    }
    else // open in current tab
    {
        window.location.href = res_url;
    }
})('%s');

On minifying, the custom search engine URL would be javascript:(function myFunction(str){var part=";";var res=str.split(part);var search_query=res[0].trim();var category=res[1].trim();var new_tab=res[2];var res_url="https://github.com/search?q="+search_query+"&type="+category;if((new_tab!==undefined)&&(new_tab.trim()==="")){window.open(res_url,"_blank")}else{window.location.href=res_url}})('%s');. The function is called with '%s' as the parameter. It splits based on the semicolon character.

Usage: Let's nickname this custom search engine as, say, "gm". Then a search for "gm binary tree; code" will open https://github.com/search?q=binary%20tree&type=code in the current page. A search for "gm radix tree; commits; " will open https://github.com/search?q=radix%20tree&type=commits in a new tab.

Note that this function would fail if %s contains single quote(s). It also doesn't work if called from an internal page like Settings.

An alternative to accomplish your goal would be to ditch the custom search engine idea and use a scripting tool like AutoHotkey (Windows) to replace search strings by URLs.