Skip navigation.

exploreopera

| Help

Sign up | Help

Customize your Search Engines with Javascript Trickery

See Search Engines Sharing Thread in the official Opera forums.

I put all relevant information in there, so this article is quite redundant by now, and I won't spend time to update.

The basic idea of the search engines presented here is to turn your address bar into a javascript-based commandline, that takes written commands plus arguments. This way, you can create search engines to translate the active page, start a specific radio station on last.fm, etc. This opens much more possibilities than just ordinary searches.

Before Opera 9, it was necessary to edit the search.ini file, or use an external editor, called "Opsed", to create your own search engines. This also meant that a search engine code needs to fit into one line of text. With the new gui features to edit search engines inside Opera, it is possible to copy & paste a multi-line code into the search engine editor. It will still appear as a one-line code in the search.ini, but it helps me to present the code in a readable way.

At the moment I am interested in creating searches that append multiple result pages in one bigger page ("antipagination"). This could be solved by "inline frames", but I don't like to have additional scrollable elements in the page. So, if you know about a javascript hack that helps to append different pages, I would like to see your comment! The finished engine can then be added to the "Search engines sharing thread" in the official forums.

Javascript code allows to 'abuse' the search engine feature to turn the adress field into a versatile commandline!

Since the new feature "Create search..." in Opera 9, we can easily integrate almost any search engine in Opera. Old-school ini hackers, however, should know that search engines can do much more than just replicating search fields on webpages. Javascript code allows to 'abuse' the search engine feature to turn the adress field into a versatile commandline. Translate the active page, start playing a web radio, combine different results in one page, whatever you like - if someone is skilled enough to hack a solution.
Now, as search engines can be created more easily, anyone can take advantage of these hacker tricks!
In this article, I present some special search engines that I was able to figure out, without being a javascript expert. Much of this has already been available as custom buttons or similar in the Opera wiki. While adapting the code of these buttons as search engines, I also tried to make it more readable. With the more comfortable search editing, it is no longer necessary to squeeze everything in one line, which is a blessing for those who want to adapt the code for their own purpose.
I know these engines are far from perfect - my primary intention is to make more people think about the possibilities, and create their own engines - so, any further contributions and testing feedback are welcome! Maybe it would even be more useful if this page was a wiki..

So far, the article contains search engines for
- Google search reduced to the site/domain/folder you are currently visiting
- Translation of the active page using babelfish (type "translate en fr")
- Simultaneous search in different engines, using frames or different pages.
- Start a last.fm tag radio (type "lastfm experimental" or "lstfm rock")




Google Site Search (for active page or special sites)

Copy the link adress or js code, and paste as search adress in a newly created search.

Java documentation (Java 2 Platform SE 5.0) - "j"

Opera forums site search (my.opera.com/community/forums) - "of"
Opera domain search (opera.com) - "od"
Opera Wiki search (operawiki.info) - "ow"

Site search on active page - "s"
javascript:(
    function() {
        open(
            "http://www.google.com/search?client=opera&rls=en&q=%s    site:"
            + window.location.hostname
            + "&sourceid=opera&ie=utf-8&oe=utf-8"
        );
    }
)()

Domain search on active page - "d"
javascript:(
    function() {
        open(
            "http://www.google.com/search?client=opera&rls=en&q=%s    site:"
            +window.location.hostname.match(/%5b^%5c.%5d+%5c.%5cw{2,4}$/)
            +"&sourceid=opera&ie=utf-8&oe=utf-8"
        );
    }
)()

Folder search on active page - "f"
javascript:(
    function() {
        var splitarray=document.location.href.split('/');
        splitarray.pop();
        open(
            "http://www.google.com/search?client=opera&rls=en&q=%s    site:"
            + splitarray.join('/')
            + "&sourceid=opera&ie=utf-8&oe=utf-8"
        );
    }
)()



Translate active page with Babelfish

The following search will translate the active page between the languages of your choice, using the babelfish translation tool. With this search installed, you can visit the website of your choice, type "t en fr" to get the french version (if t is the search nickname).
Just add a custom search with the following code:
javascript:(
    function() {
        open(
            "http://babelfish.altavista.com/babelfish/trurl_pagecontent?lp="
            +'%s'.split('+').join('_')
            +"&url="
            +document.location.href
        );
    }
)()




Start playing a last.fm tag radio

No guarantees - this needs some testing!
javascript:(
    function() {
        var s = escape("%s");
        radioPopup=window.open(
            "http://www.lastfm.de/webclient/popup/?radioURL=lastfm://globaltags/" + s,
            "flashRadioPopup",
            "toolbar=no, location=no, directories=no, status=no,menubar=no, scrollbars=no, resizable=no, width=375, height= 145"
        );
        radioPopup.moveTo(200,200);
        radioPopup.focus();
    }
)()

Some tags I know are electronic, rock, experimental .. or just have a look on the last.fm website.



Simultaneous Searches - Combining Search Engines

The following code will open one new tab for google, one for yahoo and one for altavista search, all with the same search arguments. However, one of them might be blocked by the popup blocker!!
javascript:(
    function() {
        var s=escape("%s");
        document.location.href="http://www.yahoo.com/r/sx/*-http://search.yahoo.com/search?p="+s+"&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8";
        open("http://www.google.com/search?q="+s+"&sourceid=opera&num=%i&ie=utf-8&oe=utf-8");
        open("http://www.altavista.com/web/results?itag=ody&q="+s+"&kgs=1&kls=0");
    }
)()

The same using frames
javascript:(
    function() {
        var s = escape("%s");
        var t = "Multisearch - "+s;
        var yahoo  = "http://www.yahoo.com/r/sx/*-http://search.yahoo.com/search?p="+s+"&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8";
        var google = "http://www.google.com/search?q="+s+"&sourceid=opera&num=%i&ie=utf-8&oe=utf-8";
        var googlemaps = "http://maps.google.de/maps?f=q&hl=de&q="+s+"&btnG=Adressen+suchen";
        var googleimages = "http://images.google.com/images?hl=de&q="+s+"&btnG=Bilder-Suche";
        document.write(
            "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Frameset//EN' 'http://www.w3.org/TR/html4/frameset.dtd'><html><head><title>"
            + t
            + "</title></head><frameset rows='50%,50%' cols='50%, 50%'><frame src='"
            + google
            + "'><frame src='"
            + yahoo
            + "'><frame src='"
            + googlemaps
            + "'><frame src='"
            + googleimages
            + "'></frameset></html>"
        );
    }
)()


Of course, any combinations of searches are imaginable! But how do you
customize this code ?


You need to look for the lines where the single search engine's adress is specified. Then you need to consider how the different pieces of strings are put into variables!

A usual google search looks like that:
http://www.google.com/search?q=%s&sourceid=opera&num=%i&ie=utf-8&oe=utf-8

(it depends on how it is created, this one is Opera's predefined 'g' search)


The important thing is the %s, this is the place where the user search string is inserted into the code.

In my code, I first excape the user search string into a variable (var s = escape("%s");), then replace the %s with a "+s+" (reading the string from the variable). This is necessary, because you can escape the %s only once (and here we need to reuse it for different engines).

This means, if you change one of the search engines, you need to find the %s and replace it with "+s+"


In the second piece of code (the one with frames), I first store the engines' adresses in variables yahoo, google, googlemaps and googleimages, and later use these variables. So, if you want to change one of the engines, you should ideally do this in the place where the variable is set.


How exactly does this code work?

Usually the search engines don't use javascript. This means, the naked search url becomes your search code.

With javascript, you need to pack the search url into a command, with quotes.

Here, "document.location.href=...;" will open the search in the same tab, while "open(...);" will open a new tab with the search. The lower example (using frames) uses the command "document.write(...)" to create a page with different frames.

All of these things I extracted from code in the operawiki and other resources. My own js and web programming skills are quite low. (writing a widget will be hard, I guess)



Create your own Searches with predefined Extra Arguments

You can create your own google site search by taking the Opera domain search url for instance, and replacing the "opera.com" by the domain adress of your choice.

Btw, the "%s" in the search url is a placeholder for what you type after the nickname.

More general,
- create a search from the search field of your choice.
- edit this search, and replace the "%s" with "%s arg0 arg1 arg2 ..", where "argX" is the extra argument of your choice (like "site:opera.com"). The extra spaces between "%s" and the arguments just make your search look better.



My favourite conventional custom searches

All you need to do is
- Choose a search below, and copy the link adress
- create a new search (Tools > Preferences > Search > Add..)
- paste the search string in the adress field (Details > Adress)
- Choose a name and nickname as you like. Below you see the nicknames that I use at home. Sometimes it may be necessary to change the nicknames of some of the default search engines.

LEO Deutsch-Englisches Wörterbuch - "leo"
Wikipedia (the german one) - "w" or "wiki"

Personal Wishes and Suggestions"Spacers" in the menu bar

Comments

avatar
Anonymous writes:

Finally some useful information on advanced searches in Opera. This is a very important topic, especially for people like me who translate for a living.

Specifically, I have been looking for a way to conduct simultaneous searches at multiple sites. That means: enter a key followed by a search term and have it looked up in dictionaries from different sites, without retyping a new key plus the search term each time. Is there a way to do this using extra arguments? Obviously, the results would have to be displayed on separate pages.

I have also looked for widgets with this function, but could not find any that are capable of simultaneous searches.

Thanks for any infos you can provide.

By anonymous user, # 15. November 2006, 09:31:12

avatar
I am sure simultaneous searches are possible. I will try to figure out. Maybe we can even generate a big page containing all search results! (antipagination)

Btw, when it comes to translation, I would prefer to have instant translations in the sidebar while typing an email or in a form.

Anyway, you can discuss these issues in my thread in the Opera community forum,
Customizing Opera » Custom Searches with Javascript Magic

By Schneemann, # 17. November 2006, 15:22:09

avatar
eye/photoAllergics writes:

Hello.

I have been experimenting with a similar feature, and i run over one pretty obvious problem, the %s placeholder is not sufficent, because it is not escaped.

The construct "%s" is what makes the problem. If the search line contains " , then what javascript gets is """, which is open string, close string, open striing, then... the rest of the script is eaten up in an unclosed string, syntax error.

What i'm trying to do now, is define google site search like:

javascript:location=("http://www.google.com/search?q=site:" + escape(location.hostname) + escape(" %s")); void 0

It needs some tweaking, but the real problem i get is that %s is not syntax-safe. We need a placeholder, say, %e, which would replace ' with \' , " with \" , \ with \\ , escape it so to say. Or as Hex like is usually done with URLs.

By anonymous user, # 16. January 2007, 20:06:45

avatar
Maybe we need to the js "replace(..)" command. I will play around with that when I have time.

ps. you should better login before you post!! (this page seems to be somehow out of the community.. don't know why)

By Schneemann, # 17. January 2007, 20:28:05

avatar
A simple html trick I like to use for all at once
pages is inline frames. It's useful for searches which
I do over and over each day for the same items. Click on
1 link and as many searches as you've preset into the html
below will be searched. Save the text below as an html
page using no formatting (dos, ascii, plain, txt) text.


5 pages of stock quotes for 2 charts on each page
stock 'BEWA' - and a 2nd one (i think) graphed with BEWA.

When you click on the html
link for this page you get 5 pages loading at 1 time.
The size height / width are selfexplanitory or if not
save it , click it and observe it and it will make sense.

Obviously to use different urls is easy. Just replace the
url with the ones of your own choosing.
------begin-----

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>

<META NAME="Author" CONTENT="">
<META NAME="GENERATOR" CONTENT="Mozilla/3.04Gold (Win95; U) [Netscape]">
</HEAD>
<BODY>

<IFRAME src="http://quotes.barchart.com/texpert.asp?sym=FNMCF&code=BEWA" width="750" height="550" frameborder="10"> </IFRAME>
<IFRAME src="http://quotes.barchart.com/texpert.asp?sym=AU&code=BEWA" width="750" height="550" frameborder="10"> </IFRAME>
<IFRAME src="http://quotes.barchart.com/texpert.asp?sym=NMDMY&code=BEWA" width="750" height="550" frameborder="10"> </IFRAME>
<IFRAME src="http://quotes.barchart.com/texpert.asp?sym=pdg&code=BEWA" width="750" height="550" frameborder="10"> </IFRAME>
<IFRAME src="http://quotes.barchart.com/texpert.asp?sym=ncmgy&code=BEWA" width="750" height="550" frameborder="10"> </IFRAME>

</BODY>
</HTML>


-------------------end above here---------

By andre1000, # 14. April 2007, 14:58:55

avatar
Thanks! I tried how this looks like.

The problem is, I'd like to get rid of the scrollbars. For the horizontal one, I just need to set width=100%. But to make the vertical scrollbar disappear, I need to make assumptions about the length of the page I want to embed.

Btw, a similar solution is to use
<object data="..." ..> </object>
, but it has the same problems.

What about loading the pages in "invisible" (size=zero) inline frames, and then gluing them together? Maybe each one can get an additional line of jscode, that executes when it's finished loading, and pastes the entire htmlcode into the main document?

By Schneemann, # 14. April 2007, 19:19:28

avatar
Hello,
I'm really happy to find this post. I learned a lot! Thanks!

I enjoy a lot this search engine:

http://www.google.com/search?hl=en&q=%s&btnI=kudos

I call it "goto". It's the quickest shortcut I know for all my web-apps (they simply wouldn't fit all in my personal bar). Finally, some use was found for the "feeling lucky" button!

goto reader
goto gmail
goto gcalendar
goto zoho

What's your favorite shortcut?

Cheers,
Guillaume Riflet
http://webtopmania.blogspot.com

By griflet, # 17. September 2007, 10:00:30

avatar
i want to make a search entry with javascript but i have a problem.
if I write en entry like
javascript:q="%s"; array=q.split(" ");cnt=q.match(/\+/g).length;
location.href="www.blah.de/?s="+array[0]+"&l="+array[1]+"&num="+cnt;

So i want to split the search-text %s into sepperated words.
But also i want to count the + in the search-text %s.

The problem is if i type
"hallo world ++" as search text, %s looks like
"hallo+world+++" is there any solution to discriminate + from 'space'

I also have tried to use escape unescape-Funktion - no success.

thanks

By davey69, # 29. January 2008, 11:21:57

avatar
Looks like the '+' is turned into a '%2B' in the url string. But when turning it to a javascript string, it all becomes '+' again.

Sorry, looks like there is no solution.

By Schneemann, # 31. January 2008, 00:07:39

avatar
Regarding Simultaneous Searches - Combining Search Engines:
Please inform what changes need to be made "Is post=1" and "Query" value exists. Thank you.


I have found the following to work perfectly otherwise:
URL=javascript:var s='%s';window.location.href='Page with %s Susbstitued by '+s+';open('Page with %s Susbstitued by '+s+'');open('Page with %s Susbstitued by '+s+'');

By Percy Shroff, # 4. July 2008, 08:29:58

Write a comment

You must be logged in to write a comment. if you're not a registered member, please sign up.