Trick to handling %s in bookmark keyword

Talk about add-ons and extension development.
Post Reply
User avatar
CyberSlug
Posts: 327
Joined: May 6th, 2003, 5:41 am
Location: Kentucky (USA)

Trick to handling %s in bookmark keyword

Post by CyberSlug »

I love the bookmark keyword quicksearch feature of Mozilla/Firefox, but I've sometimes wanted to override what happens if %s is null.

For example, change the built-in Wikipedia Quicksearch bookmark to point to

Code: Select all

javascript:if('%s'==String.fromCharCode(37,115)) location.href='http://en.wikipedia.org/wiki/Main_Page'; else location.href='http://en.wikipedia.org/wiki/Special:Search?search=%s';

Not only can you type in wp something to look up something, but you can also just type wp to go directly to Wikipedia's homepage!


Similar example for slashdot:

Code: Select all

javascript:if('%s'==String.fromCharCode(37,115)) location.href='http://slashdot.org/'; else location.href='http://slashdot.org/search.pl?query=%s';

And google:

Code: Select all

javascript:if('%s'==String.fromCharCode(37,115)) location.href='http://www.google.com/'; else location.href='http://www.google.com/search?q=%s';


This trick obviously requires javascript to be enabled, and you'll notice the entire bookmarklet will appear in the URL bar until the web page loads.
Hope this is useful to someone :)
Zoolcar9
Posts: 2225
Joined: November 9th, 2004, 6:45 pm
Location: Jakarta, Indonesia (UTC+7)
Contact:

Post by Zoolcar9 »


or add a prompt

Code: Select all

javascript:if('%s'!=String.fromCharCode(37,115)) location.href='http://www.google.com/search?q=%s'; else { q=window.prompt('Query:'); if(q!=null) location.href='http://www.google.com/search?q=' + q; }; void 0


My Firefox information | Add-ons | GitHub

"With great power, comes great desire to show it off."
User avatar
CyberSlug
Posts: 327
Joined: May 6th, 2003, 5:41 am
Location: Kentucky (USA)

Post by CyberSlug »

Thanks for the addition, Zoolcar9

I just realized it would be shorter to replace String.fromCharCode(37,115) with '%'+'s'.

Code: Select all

javascript:if('%s'=='%'+'s') location.href='http://en.wikipedia.org/wiki/Main_Page'; else location.href='http://en.wikipedia.org/wiki/Special:Search?search=%s';

In case you are wondering how this works, when you type the keyword searchterm into the location bar, the %s is replaced with any searchterm. If nothing is provided, %s literally equals percent sign lowercase s. So we check for the %s condition and update the location bar with whatever address we want to go to.
User avatar
logan
Posts: 3453
Joined: May 22nd, 2003, 3:51 pm
Location: NGC 2403
Contact:

Post by logan »

https://bugzilla.mozilla.org/show_bug.cgi?id=175419
https://bugzilla.mozilla.org/show_bug.cgi?id=253457

I imagine these will be fixed by a prompt rather than a dialog with something complicated/confusing, like being able to define multiple actions depending...

You could shorten 'em up a bit like so:

Code: Select all

javascript:location.href=('%s'=='%'+'s')?'http://host/':'http://host/q=%s';

Good stuff, I imagine I'll be making use of these shortly. :)
User avatar
logan
Posts: 3453
Joined: May 22nd, 2003, 3:51 pm
Location: NGC 2403
Contact:

Post by logan »

Anyone having problems on two subsequent runs? It'll work the first time in a new tab without fail, but on the second run nothing happens... After that, I cannot get another to work in the same tab, even after visiting a non-js URL.

I tried to come up with a simple testcase like:

Code: Select all

javascript:location.href='http://www.mozilla.org/';

and a few minutes ago the only way I could get this to work reliably was to append <i>void(0);</i>, but now it seems to be working ok.

I ran a quick test in 1.0.7 and didn't experience any problems... Is there something bad with bookmarklets in ~1.5b2? Is it just me?

-- edit

It seems to be caused by my caps rules disabling javascript for www.google.com and imdb.com...
mariansigler
Posts: 1
Joined: April 26th, 2006, 10:48 am

Post by mariansigler »

At Wikipedia, they have made [url=http://en.wikipedia.org/wiki/%s]/wiki/%s[/url] as a redirect to the main page exactly to fix this problem.
-- Microsoft broke Volkswagen's world record: Volkswagen made only 22 million bugs.
Samus_
Posts: 1
Joined: February 28th, 2009, 6:51 am

Re: Trick to handling %s in bookmark keyword

Post by Samus_ »

recent versions of Firefox do not add a literal %s when no keyword is present, it is very good for some cases but there's still the problem of getting a search for an empty string.

for that I propose this alteration of the original idea:

Code: Select all

javascript:if('%s') location.href='http://en.wikipedia.org/wiki/Special:Search?search=%s'; else location.href='http://en.wikipedia.org/wiki/Main_Page';
Post Reply