-
-
Save cmccormack/8c468d74aa2d22e7ef7812ffbe732ce2 to your computer and use it in GitHub Desktop.
/** | |
* @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 [email protected] | |
* @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); |
I managed to make it work but it only works when pressing 'Shift+Enter' i.e. in a new window.
@iamMG great catch! I made a few simplifications and explicitly added the window object - can you try the snippet below and let me know if it works for you? If so I'll update the code in the original gist.
javascript: const search = '%s'; const s = search.split(' '); const url = `https://www.reddit.com/r/${s[0]}/search?q=${s[1]}&sort=top&restrict_sr=on&t=year`; window.location.replace(url);
Edit: a little more research and found this may be an issue only in new tabs in chrome, if you try it from a tab the a page already loaded it should work but new tabs may not. I'll dig in a little more but not sure there is much I can do about the new tab scenario.
This simplification functions just as well. I think I know what the problem is. This script won't run normally, wherever javascript is not allowed to run. Tried it on the Chrome settings page (chrome://settings/searchEngines). Although, 'Shift-Enter' trick works everywhere.
'cmd-enter' too, if you want it in a new tab vs a new window.
'cmd-enter' too, if you want it in a new tab vs a new window.
Do you mean in Mac?
yup, for windows it should be ctrl/alt+enter?
Hi,
I found this script while searching for something like this. After some testing I got it to work. Just 1 'feature request' I guess. Would it be possible to add support for strings containing a space?
Nevermind, I realised as I was typing that I could change the separator to anything I want. So for others: I changed search.split(' ')
to search.split('"')
. So now I can type aaa"bbb bbb"ccc to use 3 strings.
Hi @cmccormack. Recently I have been using Microsoft Edge (based on Chromium) and found out that it supports this script very well! Don't need to shift-enter or all that stuff. I wonder how the search engine implementation in Edge is different from Chrome, Brave etc.? Can you please have a look into it if you have some free time?
Doesn't seem to work in the latest version of Chrome (126.x.x.x
)-- not as written, anyway.
@jehrenzweig-captionhealth check @cmccormack 's reply above. The script doesn't work on New Tab page; you need to press shift + enter
.
Thanks for this script. However, it isn't working on Chrome v76.0.3809.132. I have followed all steps correctly. After entering two queries separated by a space and hitting enter, nothing happens.
Please look into it as it is a very helpful and needed script.
Thanks.