1

我从这个来源创建了一个 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;
    }

但是当我构建它时,然后单击按钮。我没有给我一个警报信息。我只想注入一个脚本。任何想法或技巧......解决这个问题

4

1 回答 1

0

它不起作用,因为添加到文档的脚本标签不会自动评估。

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

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

此外,您根本不需要添加脚本标签......只需使用window.execScript.

于 2012-11-25T17:13:17.760 回答