互联网资源管理器扩展(Internet Explorer Extensions)

2019-08-03 01:44发布

我创建这个来源的IE扩展: 如何开始与发展中国家的Internet Explorer扩展? 它工作的伟大。 但我想改变

int IOleCommandTarget.Exec(IntPtr pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
    {
        var form = new HighlighterOptionsForm();
        form.InputText = TextToHighlight;
        if (form.ShowDialog() != DialogResult.Cancel)
        {
            TextToHighlight = form.InputText;
            SaveOptions();
        }
        return 0;
    }

为此:

int IOleCommandTarget.Exec(IntPtr pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
    {
        HTMLDocument document = (HTMLDocument)browser.Document;

        IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)
                               document.all.tags("head")).item(null, 0);
        IHTMLScriptElement scriptObject =
          (IHTMLScriptElement)document.createElement("script");
        scriptObject.type = @"text/javascript";
        var text = @"alert('hello') ";
        scriptObject.text = "(function(){" + text + "})()";
        ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);
        return 0;
    }

但是,当我建立它,然后单击按钮。 我不给我一个警告消息。 我想仅仅只注入一个脚本。 任何想法或技巧:为了解决这个问题

Answer 1:

它不工作,因为添加到文档中的脚本标记不会自动评估。

您必须手动EVAL脚本,如下所示:

var document = browser.Document as IHTMLDocument2;
var window = document.parentWindow;
window.execScript(@"(function(){ alert('hello') })()");

此外,你并不需要在所有添加脚本标签...只是使用执行脚本window.execScript



文章来源: Internet Explorer Extensions