#StandWithUkraine

Make Opera custom search site-aware with JavaScript

One function of Quix that made me instantly want it in Opera custom search is ability to use current URL as parameter. This can greatly simplify some searches, which I often do and find very annoying to type out every time.

So I hit some Opera blogs, took deep look on how Quix does that and after figured out how to set up custom search engine that is aware of site you run query on with bit of JavaScript.

Custom search limitation

Search engines in Opera can only process single parameter in query – whatever you type, even if that is multiply words. It is represented in engine settings as %s. Whatever you input after engine shortcut will replace %s in resulting URL (even if there are several %s used).

So natively there is no way to use such things as current site as search parameter. At best you can edit URL or copy/paste what is needed.

Inline JavaScript

My starting point was to search if there is a way to make it work with multiply parameters at all. Unsurprisingly I found a post at Tamil’s blog (best blog on Opera configuration and tweaks) that shows how to use JavaScript for split search query in multiply parameters.

Basically Opera executes search engine URL literally. That means it can easily be inline JavaScript command and it will be executed perfectly.

Use current URL

After few test I got this line to use as search engine (should be use in single line):

javascript:window.location.href='http://www.google.com/search?q=%s
	site:'+document.location;

This will run Google search with your query as usual, but also append filter that limits results to site you are currently on.

Use selection from page

JavaScript can just as easy use text, selected in current page. Overall command will be same and JavaScript function to fetch selection is window.getSelection().

However I hit another roadblock here – you can’t run custom search with selection alone. You need to type something in as query or it won’t be recognized as search attempt.

I will brood on this some more, but for now I used this code for some simple bookmarklets. For example I often check URLs in comments for moderation with BackType. So bookmarklet to run such check on selection will be:

javascript:window.location.href='http://www.backtype.com/www/'
	+window.getSelection();

Overall

JavaScript is more tricky to use for custom search engines comparing to regular URL commands. But it greatly increases number of possible parameters and their combinations to allow much more complex searches.

What would you like to use as search in Opera, but currently can’t? Share your ideas in the comments.

Related Posts

7 Comments

  • Saurabh #

    I was unable to get your site search to work. The one line should be this "javascript:window.location.href='http://www.google.com/search?q=%ssite:'+document.location;" or with a space between %s and site or something else? This is the only search that seems non-overkill for my uses. :)
  • Rarst #

    @Saurabh Yep, there should be space after %s By the way I made one more tweak to use domain instead of whole URL, more convenient this way. javascript:window.location.href='http://www.google.com/search?q=%s site:' +document.location.hostname;
  • Saurabh #

    Yeah now its working for me. The problem was that I had "Reuse current tab" disabled. So the search opened in a new tab where the javascript didn't do its job. After enabling it,everything runs fine.
  • Saurabh #

    and one more thing :there should be a + after site:' in the second tweak.
  • Rarst #

    @Saurabh Ops, thanks! Copy/pasted it from Opera, but it is hard to post code in WordPress - everything is sanitized to near-death for security. :)
  • purestmagic #

    How about creating simoultaneous searches using post requests in javascript ? anyone got an idea ?
  • Rarst #

    @purestmagic Launching several searches won't be too hard... But I am not good enough with JS to deal with POST requests, sorry. :) Maybe when I get time to read up on JS at last.