Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Insert some java script using HTMLDocumentClass

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??

Upvotes: 0

Views: 2935

Answers (3)

Fraser
Fraser

Reputation: 17039

There is no way directly in .NET to set a script element in the document head. As a workaround 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 as shown below...

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!'

Upvotes: 1

shahkalpesh
shahkalpesh

Reputation: 33476

HtmlDocument has a Window property.

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

Upvotes: 0

i_am_jorf
i_am_jorf

Reputation: 54600

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

Upvotes: 0

Related Questions