View Selection Source for IE (level: beginner/intermediate)

I find this quite useful for debugging things in IE, add a "View Selection Source" option to the right click context menu in IE6 (it alerts the selected range generated HTML, just in a javascript alert not a text editor, well… you can’t have everything).

You'll need to do the folowing

  • Save the AlertSelectedText.html
  • Edit the regisery

step 1:

Aave the AlertSelectedText.html in an appropriate place.

<SCRIPT LANGUAGE="JavaScript" defer>
  var parentwin = external.menuArguments; 
  var doc = parentwin.document;
  var sel = doc.selection;
  var rng = sel.createRange();
  var str = new String(rng.text);
  var html = new String(rng.htmlText);
  var ops = "width=500,height=300,status=0,toolbar=0,menubar=0,resizable=1";
  
  viewSourceWin = parentwin.open("about:blank","viewselectionscr",ops);

  // open document for further output
  viewSourceWin.document.open();

  // create document
  viewSourceWin.document.write("<html><head><title>selection source</title></head>");
  viewSourceWin.document.write("<body style='margin:0;padding:0;overflow:hidden;'>");
  viewSourceWin.document.write("<textarea wrap=off style='width:100%;height:100%;'>");
  viewSourceWin.document.write(html);
  viewSourceWin.document.write("</textarea>");
  viewSourceWin.document.write("</body></html>");

  // close the document 
  viewSourceWin.document.close();  
  viewSourceWin.focus();
</SCRIPT>

step 2:

Dive into regedit and add your menu item, which is a bit scary, normally I wouldn’t go anywhere near the Registry but in this case I think it’s worth it.

Start > Run > regedit >


HKEY_CURRENT_USER 
	Software 
		Microsoft 
			Internet Explorer 
				MenuExt

In regedit navigate to MenuExt, right click on it and select New > Key call it "View Selection Source" a new text data key will be added, right click on it and select modify, in the "value data" area type in the path to the attached HTML page. Should look something like this...

That should do it close regedit quickly, and close all IE browsers.

Alternativly:

If you don’t want to go to the bother of crashing your machine and reinstalling the OS, you could just save the javascript as a link, but really, that's not quite so much fun!

Right click on the following link View Selection Source and choose Add to Favourites... save it in your Links folder. If your links toolbar is visible, you'll get a "View Selection Source" link, now select something on the page and click the link.

- ends -