tags:

views:

616

answers:

4

i would like to insert custom javascript to the head of the page all i have is this HTMLDocumentClass object,

does anyone know how to do that?

are there any security restrictions???

can i change ids of elemnts that came with the page??

A: 

Use the HTMLDocument::Window property to get the HTMLWindow class, the use the HTMLWindow::DomWindow property to get the native IE interface. Then call IHTMLWindow2::execScript.

http://msdn.microsoft.com/en-us/library/aa741364(VS.85).aspx

jeffamaphone
there is no window property in HTMLDocumentClass...
Chen Kinnrot
and if i try realy hard i get a invalid cast exception
Chen Kinnrot
A: 

HtmlDocument has a Window property.

Alternatively, you could use HtmlDocument's CreateElement method to inject script into the current document.

shahkalpesh
+1  A: 

Hi,

There is no way directly in .NET to set a script element in the document head. As a work around you could reference the mshtml.dll and use the IHTMLDocument2 interface. Also, you could also just use a wrapper class to expose the functionality you require. (i.e. the Text or src properties of the script element so you can set you script code). Then you just need a method that implements the custom wrapper interface. Something like this...

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


/// <summary>
/// A COM interface is needed because .NET does not provide a way
/// to set the properties of a HTML script element.
/// This class negates the need to refrence mshtml in its entirety
/// </summary>
[ComImport, Guid("3050F536-98B5-11CF-BB82-00AA00BDCE0B"),
InterfaceType((short)2),
TypeLibType((short)0x4112)]
public interface IHTMLScriptElement
{
    /// <summary>
    /// Sets the text property
    /// </summary>
    [DispId(1006)]
    string Text
    {
     [param: MarshalAs(UnmanagedType.BStr)]
     [PreserveSig,
     MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime),
     DispId(-2147417085)]
     set;
    }

    /// <summary>
    /// Sets the src property
    /// </summary>
    [DispId(1001)]
    string Src
    {
     [param: MarshalAs(UnmanagedType.BStr)]
     [PreserveSig,
     MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime),
     DispId(-1001)]
     set;
    }
}

// Inject script element
public static void InjectJavascript(string javascript, HTMLDocument doc)
{
    if (doc != null)
    {
     try
     {
      // find the opening head tag
      HtmlElement head =  doc.GetElementsByTagName("head")[0];
      // create the script element
      HtmlElement script =  doc.CreateElement("script");
      // set it to javascirpt
      script.SetAttribute("type", "text/javascript");
      // cast the element to our custom interface
      IHTMLScriptElement element = (IHTMLScriptElement)script.DomElement;
      // add the script code to the element
      element.Text = "/* <![CDATA[ */ " + javascript + " /* ]]> */";
      // add the element to the document
      head.AppendChild(script);
     }
     catch (Exception e)
     {
      MessageBox.show(e.message);
     }
    }
}

You would use it like this, where myDoc is your html document...

InjectJavascript("function foo(bar) { alert(bar); }", myDoc); // inject the 'foo' function

and test it like this...

myDoc.InvokeScript("foo", new object[] { "Hello!" }); // alerts 'hello!'

Fraser
do you know of any security restrications?
Chen Kinnrot
A: 

Hey Fraser . thanks a billion man.. u saved my life.. else i had to carry the 7 mb mshtml crap with my application.