Custom Search Engine with Multiple Parameters
-
I recently learnt the basics of bookmarklets. I thought it was possible to pass a parameter to the JS function and use it as a custom search engine. Here is my approach:
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 tree&type=code in the current page. A search for "gm radix tree; commits; " will open https://github.com/search?q=radix tree&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 this goal would be to ditch the custom search engine idea and use a scripting tool like AutoHotkey (Windows) to replace search strings by URLs. The custom search engine can also be used to do things other than search, like maybe a find and replace on a web page. This might the way to actually create commands for Quick Commands.
Related post: https://forum.vivaldi.net/topic/25949/placeholders-as-commands-on-custom-search-engines
-
@aqm said in Custom Search Engine with Multiple Parameters:
Note that this function would fail if %s contains single quote(s).
this ain't right
FWIW you can escape them likethis ain\'t right