代码之家  ›  专栏  ›  技术社区  ›  Chen Kinnrot

使用HTMLDocumentClass插入一些java脚本

  •  0
  • Chen Kinnrot  · 技术社区  · 16 年前

    我想在页面的头部插入自定义javascript

    有人知道怎么做吗?

    有任何安全限制吗???

    我可以更改页面附带的元素ID吗??

    3 回复  |  直到 16 年前
        1
  •  1
  •   Patrick D'Souza ob1    12 年前

    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);
            }
        }
    }
    

    您可以这样使用它,其中myDoc是您的html文档。。。

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

    然后像这样测试它。。。

    myDoc.InvokeScript("foo", new object[] { "Hello!" }); // alerts 'hello!'
    
        2
  •  0
  •   i_am_jorf    16 年前

    使用HTMLDocument::Window属性获取HTMLWindow类,使用HTMLWindow::DomWindow属性获取本机IE接口。然后调用IHTMLWindow2::execScript。

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

        3
  •  0
  •   shahkalpesh    16 年前

    HtmlDocument有一个 Window 所有物

    或者,您可以使用HtmlDocument的 CreateElement