Categories
Firefox JavaScript

Wikipedia Bookmarklet


We shall be creating a Wikipedia bookmarklet, The bookmarklet will either take whatever is selected on the screen as the string to be found or will prompt you.

As usual we’ll write the code as a normal JavaScript code, except that we start of with the string

javascript:

If you are unfamiliar how to create a bookmarklet, take a look here.

Here is the code…

javascript:
//Declare 2 variables
var selection;
var s;

// Grab whatever selection is present
if (window.getSelection)
{
selection = window.getSelection();
} else if (document.getSelection)
{
selection = document.getSelection();
}
if (selection.toString().length > 0)
{
s = selection.toString();
} else {
s = prompt(“Search”, “Martina Hingis”);
}

// If variable is valid string, lets find it.
if (s.length > 0)
{
window.location = “http://en.wikipedia.org/wiki/”+s;
}

Now that we’ve completed the code, its time to replace, the return charecters “\n” with empty strings… and your bookmarklet is ready.

javascript:var selection;var s;if (window.getSelection){selection = window.getSelection();} else if (document.getSelection){selection = document.getSelection();}if (selection.toString().length > 0){s = selection.toString();} else {s = prompt(“Search”, “Martina Hingis”);}if (s.length > 0){window.location = “http://en.wikipedia.org/wiki/”+s;}

You can also, set a keyword for it like wiki as I’ve done…

wikipedia-bookmarklet.png

By ruturajv

developer, traveler...

Leave a comment