Extending the Internet Explorer context menu


In a comment, Darrell Norton asked for a "View in Mozilla" option for Internet Explorer.

You can already do this.

Internet Explorer's context menu extension mechanism has been in MSDN for years. Let me show you how you can create this extension yourself.

First, create the following registry key:

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\View in Mozilla]
@=REG_SZ:"C:\some\path\to\ViewInMozilla.htm"
Contexts=REG_DWORD:1

Of course, you need to change C:\some\path\to to an actual path.

How did I know to do this? Because steps 1, 2 and 3 in the "Implementation Steps" section tell me (1) what key to create, (2) what to set the default value to, and (3) what to set Contexts to. I chose a Context value of 1, which means "Default".

Okay, now to write the script ViewInMozilla.htm. Well, the documentation says that I can access context from the menuArguments property of the external object. So let's start with that.

<SCRIPT>
alert(external.menuArguments);
</SCRIPT>

Okay, let's run this puppy. Launch IE, right-click on a blank space in the web page, select "View in Mozilla", and you get...

    [object]

Woo-hoo! This is a major accomplishment: Something happened at all. Doing things in small steps makes it easy to identify where a problem is. If we had run full steam ahead to completion and then it didn't work, we wouldn't have known whether it was due to a bug in the script, a bad registration, a bad filename...

Now that I have the menu arguments, I can use that to suck out information about the item that the context menu applies to. Let's try this:

<SCRIPT>
alert(external.menuArguments.document.URL);
</SCRIPT>

Woo-hoo, now it gives me the URL. Almost there. All that's left to do is to run a program with that URL as the command line parameter.

<SCRIPT>
var shell = new ActiveXObject("WScript.Shell");
shell.run("mozilla \"" + external.menuArguments.document.URL + "\"");
</SCRIPT>

Mission accomplished.

Now you too can create Internet Explorer context menu extensions.

In fact, go ahead and do it, since Darrell asked for it: Create an Internet Explorer context menu extension that operates on anchors and opens the linked-to page in Mozilla.

(Bonus: Tony Schreiner cooked up something similar for zooming.)

Comments (46)
  1. Mulla Bill says:

    You’re fired! =)

  2. This is great. Thanks! Now to figure out how to go the other way as well. :) ViewInIE (For Plugins that don’t quite work)

  3. Ben Hutchings says:

    Chris: See http://ieview.mozdev.org/ (this is for Firefox though).

  4. http://www.rpi.edu/~willij3/hacks/IEContextMenu_Reg.txt

    here’s a couple of fun additions to the context menu that will take highlighted text and do one of the following:

    – search Dictionary.com

    – search google

    – open in new window (for non linked URLs)

    – search slashdot

    I haven’t used them for a while, so the URLs might have changed… But the funcationality is cool none the less.

  5. I’m glad to see I’m not the only one who debugs these things in a series of alert() statements.

    Do I have the object yet? Check. Revise. Does this property have what I want? Check. Revise. Okay now call the function that I wanted to call in the first place.

  6. Eric says:

    Reg file:

    —-

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMenuExtView in Firefox]

    "Contexts"=hex:20,00,00,00

    @="C:\Utilities\ViewInFirefox.htm"

    —-

    ViewInFirefox.htm:

    —-

    <SCRIPT>

    var shell = new ActiveXObject("WScript.Shell");

    shell.run("firefox "" + external.menuArguments.event.srcElement + """);

    </SCRIPT>

    —-

    This will do it for firefox; replace all occurrences of "firefox" with "mozilla" to do it for moz instead.

  7. Marc Wallace says:

    This would have been a cleaner way for me to get ‘view source’ to dump into a real editor (with line numbers). I had to poke around the registry to figure out how to change the default behaviour; it never occurred to me that I could just add new behaviours. Very cool.

  8. I find that IE’s context menu fills up fast, and there seems to be no way to push extensions into a sub-menu.

    However, there IS a mechanism for popping up a dialog with whatever GUI you want. That’s what this tool takes advantage of:

    http://slingfive.com/pages/code/ssPowerTools/. It’s a ton of context menu extensions all rolled into one context menu item.

    My favorite developer feature is the "Show Full Page Weight", which grabs and totals the byte-size of the HTML and all linked images/css/scripts/objects.

  9. Marc Wallace says:

    Is this only available if it’s opening a file, rather than immediate mode? I try:

    javascript:alert(external.menuArguments);

    from the open dialog, and it returns undefined. Ditto if I include that in a MenuExt extension.

  10. Friend says:

    Don’t read Slashdot.

  11. David Candy says:

    D. Shows how to change Tools – Show Related to use google instead of Alexa or MSN. C just seems somewhat relevent.

    c/ Both Google Toolbar (http://toolbar.google.com/) and the IE5 Powertoys (http://www.microsoft.com/Windows/IE/WebAccess/ie5tools.asp ignore warnings on page) allow selecting a word in a web page then searching on it.

    d/ If you want the Show Related Sites (Tools menu) to use Google rather than MSN or Alexa then

    Type in Start Run

    notepad %windir%webrelated.htm

    Change the line

    //RelatedServiceURL="http://related.msn.com/related.asp?url=&quot;;

    to

    RelatedServiceURL="http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=related:&quot;;

  12. Via Bink: "Mission accomplished. Now you too can create Internet Explorer context menu extensions. In fact, go ahead and do it, since Darrell asked for it: Create an Internet Explorer context menu extension that operates on anchors and opens the linked-to page in Mozilla."…

  13. Some time ago, I wrote a little on this, and calling a .net component as the context menu action. See the article here

    http://weblogs.asp.net/nleghari/articles/5319.aspx

  14. Rhys says:

    Context menus are fantastic, I can’t see why more isn’t made of them. There are a few on my site and Microsoft has a collection here:

    http://www.microsoft.com/windows/ie/previous/webaccess/webdevaccess.asp

    Now if only firefox could implement something like that my life would be complete.

  15. Jim says:

    Well its there in the full Mozilla suite and its been there for years.

    Just go to Preferences – > Advanced -> DOM Inspector

    And click the Install button.

    Once installed it will appear in your Sidebar (hit F9 if you can’t see the sidebar at the moment) or under the Tools menu in Web Developer -> DOM Inspector

    And there is a plugin on mozdev.org that installs this for Firefox, but I cba to look for it.

  16. Jim says:

    Forgot to mention that pretty much everything else in that download is in Firefox and enabled by default eg View Partial Source (aka View Selection Source) and searching for words on a page after highlighting them.

  17. Jan Söderback says:

    Jim: Actually, the FireFox installer includes the DOM Inspector. Just choose the custom install.

  18. "I’m glad to see I’m not the only one who debugs these things in a series of alert() statements.

    Do I have the object yet? Check. Revise. Does this property have what I want? Check. Revise. Okay now call the function that I wanted to call in the first place."

    I do this in practically all my code in any language. Instead of writing a whole pile of code and then trying to test it, I write the smallest amount of code that could do something leading toward my final goal. Once I see that it works, I add a little more and try it, and so on.

    I’m much happier when I can go from working code to working code instead of dealing with a big heap of non-working code.

  19. David Candy says:

    Michael Geary

    Do other people do it differently. I’ve always been an individual programmer (one use custom programs in a language where variables are named after the line number they were created on). I’m compiling within a minute of my first character typed. I use msgbox even in the debugger. I ignore the debugger mostly though sometimes I use breakpoints.

  20. jb says:

    On a somewhat related topic,it’s also quite easy to extend the context menu of the Windows explorer. For instance, look up my Win32::ShellExt perl extension on CPAN, that allows you to write some in perl.

    I’m sure it should be possible to put perl code in the SCRIPT tag to extend IE.

  21. T says:

    You wouldn’t believe how hard it is to explain debugging with alert(); or msgbox() to folks that didn’t learn it that way.

    Me? I started out on an Apple][+ and AppleBASIC where the primary method of output was the PRINT statement. I can’t imagine not using some form of print/alert()/msgbox()/response.write for debugging, especially web apps.

    But I work with some folks that can have a bug pop up and ask me what’s wrong. When I ask what the value of variable X is, they say "It’s supposed to be…." To which I invariably reply, "Well, we know what it should be, but how do you know what it really is? Have you printed it out?" To which the reply is usually a blank stare. Well, it used to be. I’ve indoctrinated them. ;-)

    Anyway, it seems like the folks that learned to code with VB and have never used anything but VS tend to be less likely to be "alerters", while those that learned to code in a console environment tend to be avid "alerters".

    That’s just an off-the-cuff observation based on a few years dealing with various coders and would-be coders.

    I could be wrong.

  22. David: Believe it or not, I have run into people who write a huge mess of code and then start testing it, instead of taking little steps at a time. Um, I think I used to do that too, although I wouldn’t admit it! :-)

    I’m not a "unit test only, never use a debugger" type of guy, though. I like to use unit tests and a good debugger. Of course, for an example like Raymond’s, you don’t really have a debugger available, and the alert box is the way to go, using tiny steps with tests along the way.

  23. Kevin says:

    Thanks all! I’m using this to make an Urbandictionary.com search for myself!

  24. Kevin says:

    Ok so acutally it’s not working. I get a javascript error with:

    external.menuArguments.document.selection.createRange().text

    and

    @="javascript: alert(external.menuArguments.document);"

    is null and:

    @="javascript: alert(external.menuArguments);"

    is undefined. Does anyone know a better way of capturing the highlighted text? (I’m using Win2k and WinXP and ie 6)

    Thanks!

    -Kevin

  25. Raymond Chen says:

    You don’t put the script directly into the registry. The registry points to an HTML file, and you put the script into the HTML file.

  26. Kevin says:

    Ok I got it to work as a separate file, but is it possible to launch right away? The fourth comment on this page (Josh Williams) claims you can, but I can’t get his code to work either.

  27. Raymond Chen says:

    You need to exit IE and restart it for it to pick up the new extensions (but you can edit an existing extension without needing to restart).

  28. Kevin says:

    Ok I got it to work as a separate file, but is it possible to launch right away? The fourth comment on this page (Josh Williams) claims you can, but I can’t get his code to work either.

  29. Win Bent says:

    @="javascript: alert(external.menuArguments);"

    "You don’t put the script directly into the registry. The registry points to an HTML file, and you put the script into the HTML file." (Raymond Chen)

    It works on Win98 with the script in the registry, but fails on XP. Is there a way to put the script in the registry on XP? That would greatly simplify installation!

  30. Raymond Chen says:

    If there is I don’t know how.

  31. Hi all,

    thanks for exlaining about context menus.

    I just implemented the context menu example with slight problems. As I run Norton Antivirus, it intercepts the "malicious" script, so you don’t get to see wether it works unless you disable the "script blocking" feature. You can enable the "ask for action to be taken" option, but in my case it would not wait long enough to let me answer.

    The URL given is MSDN explaining in depth which possibilities there are in configurating IE.

    I now use

    <script>

    var shell = new ActiveXObject("WScript.Shell");

    shell.run("mozilla -inspector "" + external.menuArguments.document.URL + """);

    </script>

    to start the DOM inspector from within IE, please be aware that this DOM is Mozilla DOM not MS ( i.e. "textContent" instead of "innerHTML" and the like.

    Cheers, Casual

  32. Casual Programmer says:

    P.S.

    I am running XP SP2 / IE 6.0 / Mozilla 1.8a2

  33. Dumky says:

    I used this technique as well, but with a little variation: you can host the script in a remote file. So all you need to do to install the menu is to install the registry key.

    More details at:

    http://blog.monstuff.com/archives/000033.html

  34. Mike says:

    this doesnt work for me..:(

    I inserted the code into the registry, created the .htm file and put it into the right place, but I still dont have a new entry in IE’s context menu (= supposing, that context menu means, you right-click in the IE window on a web page and the menu that appears is the context menu…)

    btw, I have a German XP Pro, but that shouldnt make the difference…

    any ideas?

  35. Raymond Chen says:

    Did you remember to launch a new copy of IE from the Start menu?

  36. Mike says:

    yeah, I rebooted since then about 5 times…

    still cant see anything..:(

  37. Raymond Chen says:

    All I can think of is some subtle typo.

  38. Mike says:

    well, I did the following:

    put this in a file:

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMenuExtView in Mozilla]

    @=REG_SZ:"C:ViewInMozilla.htm"

    Contexts=REG_DWORD:1

    executed it, the value was inserted with a folder symbol into the registry, created the file c:ViewInMozilla.htm with the following content:

    <SCRIPT>

    var shell = new ActiveXObject("WScript.Shell");

    shell.run("mozilla "" + external.menuArguments.document.URL + """);

    </SCRIPT>

    then rebooted…and nothing happened…double checked everything about 3 times…

  39. Raymond Chen says:

    If you take a look at the registry you’ll see that your .reg file didn’t work. That’s because what I gave above was not a .reg file; it was just a description of a registry key. I’m assuming people are thinking and not just blindly cutting/pasting.

  40. bruce says:

    can some possibly package this ‘view in mozilla’ thing into a simple reg file for normal humans to use? much like firefox simply lets you download an xpi?

  41. Raymond Chen says:

    Um, Eric already did in one of the comments.

    Remember, my target audience is *not* normal users. It’s advanced programmers.

  42. Raymond Chen says:

    Commenting on this entry has been closed.

  43. FireFoxView Mozilla extension for IE

  44. Browser peace and where (VS) 2005 can help.

Comments are closed.