h3mm3's blog

Stories from behind the keyboard

  • RSS
  • Twitter

One of the feature I miss most when I use Internet Explorer in comparison of Google Chrome, is the contextual menu command “Go to <url>” that appears when you select a text that resembles an URL and then right-click the selection (see picture).

image

In Italy we have a saying:

«He who works by himself does the work of three (people)» (Chi fa da sé, fa per tre)

that’s to say: if Microsoft hasn’t implemented such a feature yet, you’ll do better implement it on your own (or change your browser!). Since I really like Internet Explorer 9, I chose the former. 

In order to achieve this result, I configured Internet Explorer to open an HTML file and I wrote Javascript code inside that file, so that IE will open a new window using the selected text as an URL address. In the following picture you can see how my IE9 reacts when I right-click on a selected text.

image

The good old RegEdit

In order to customize Internet Explorer’s context menu, I added a key in the Registry to HKCU\Software\Microsoft\Internet Explorer\MenuExt. I called this key “&Navigate…” (the ampersand is used to denote the “N” as the keyboard shortcut for command menu).

Under this key I set two values, the default string value (REG_SZ) and a 32 bit value (REG_DWORD) called Contexts.

image

The default value is the path to an HTML document that will be loaded when the user clicks the Navigate context menu item. The Contexts value indicates «which contexts your entry should appear in the standard context menu by using a bit mask[...]». The hexadecimal value that I used (0x10) is the fifth bit of the mask and it means “Text selection”. All the possible values are explained in the following table (which I copied-and-pasted from an interesting MSDN tutorial)

image

Of course if you want to skip all the point-and-click-and-type stuff, you can just create a text file called “NavigateFromIE9.reg” and type the following code inside it:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\&Navigate...] @="C:\\Windows\\navigate.html" "Contexts"=dword:00000010

Then you just execute it (but first you promise not to sue me if your computer will implode Con la lingua fuori)

The good old Notepad

So far I simply configured Internet Explorer to load the HTML document “C:\Windows\Navigate.html”, but what’s inside this file? Here it is my code:

<script language="JavaScript"> 
  var parentwin = external.menuArguments;
  var doc = parentwin.document;
  var sel = doc.selection;
  var rng = sel.createRange();
  var str = new String(rng.text);

  if(0 < str.length)
  {
    if (str.indexOf("http")!=0)
        window.open("http://"+str, "_blank");
    else 
        window.open(str, "_blank");
  }
</script>

The script checks the selected text and builds an URL from it. Then it opens a new browser window addressed to the URL. If you want to get deeper I suggest you to read the MSDN article About the Browser since it «explains the architecture of Microsoft Internet Explorer 4.0 and later […]».

By the way, I put Navigate.html in my %SYSTEMROOT% folder in order to better protect it and avoid malicious Javascript attacks.

Happy programming!

2 comments:

Anonymous said...

Right here is the perfect site for everyone who
would like tto find out about this topic. You understand a whole lot its almost hard to argue with you (not
that I really would want to…HaHa). You definitely put a fresh spin on a topic that's been discussed for ages.
Excellent stuff, just excellent!

My blog nerd

Anonymous said...

I have wanted this for sooooo long - Thank You!

For IE 11, you get the selected text from:

external.menuArguments.window.getSelection().getRangeAt(0).toString()

(obviously this does no error checking!)