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

About these ads

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: