0

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

3 Answers 3

1

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!'
Sign up to request clarification or add additional context in comments.

1 Comment

do you know of any security restrications?
0

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

1 Comment

and if i try realy hard i get a invalid cast exception
0

HtmlDocument has a Window property.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.