Skip to content

Instantly share code, notes, and snippets.

@kramtark
Forked from cmccormack/chromesearchmultvar.js
Created October 29, 2019 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kramtark/a2fd839abff4e436e8a22b1217514475 to your computer and use it in GitHub Desktop.
Save kramtark/a2fd839abff4e436e8a22b1217514475 to your computer and use it in GitHub Desktop.
Google Chrome Search Engine with multiple search strings
/**
* @desc this snippet will allow multiple arguments to a search query in Google Chrome
* examples include https://www.reddit.com/r/%s/search?q=%s
* @author Chris McCormack mccormack.christopher@gmail.com
* @required Google Chrome. Replace all values in brackets ([]) with valid entries.
* To add to Chrome, go to Settings > Search [Manage search engines...] > Other search engines.
* At the bottom of this section, there are three required fields:
* [Add a new search engine] [Keyword] [URL with %s in place of query]
* - Add a new search engine: Descriptive name of your search
* - Keyword: used to trigger search.
* Example: typing maps.google.com then hitting [tab] or [space] replaces the search bar with a Google Maps search.
* - URL with %s in place of query: This is where the javascript code below is entered.
*/
/** Javascript code broken out with comments: */
javascript:
var search='%s'; // '%s' is the search string provided in the Omnibox
url='[URL including at least one %s]'; // example https://www.reddit.com/r/%s/search?q=%s
query=''; // leave blank
urlsegments=url.split('%s');
searchsegments=search.split('[string separator]'); // replace with separator (ex: ';', ' ', ',')
for(i=0; i<searchsegments.length; i++) query+=urlsegments[i]+searchsegments[i];
query+=urlsegments[urlsegments.length - 1]; // appends the remaining url if any
location.replace(query);
/** Short version without comments: */
javascript: var search='%s'; url='[URL including at least one %s]'; query=''; urlsegments=url.split('%s'); searchsegments=search.split('[string separator]'); for(i=0; i<searchsegments.length; i++) query+=urlsegments[i]+searchsegments[i]; query+=urlsegments[urlsegments.length - 1]; location.replace(query);
/** Example code using a reddit search including subreddit name and search query.
* In this example, 'rs' was used as the keyword. Typing 'rs[space]' then 'news syria'
* returns the top results from the past year for 'syria' from reddit.com/r/news.
*/
javascript: var search='%s'; url='https://www.reddit.com/r/%s/search?q=%s&sort=top&restrict_sr=on&t=year'; query=''; urlsegments=url.split('%s'); searchsegments=search.split(' '); for(i=0; i<searchsegments.length; i++) query+=urlsegments[i]+searchsegments[i]; query+=urlsegments[urlsegments.length - 1]; location.replace(query);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment