【问题标题】:Internet Explorer ExtensionsInternet Explorer 扩展
【发布时间】:2012-11-21 12:46:13
【问题描述】:

我从这个来源创建了一个 IE 扩展: How to get started with developing Internet Explorer extensions? 而且效果很好。但我想改变

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

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

【问题讨论】:

    标签: c# internet-explorer windows-8


    【解决方案1】:

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

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

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

    另外,您根本不需要添加脚本标签...只需使用window.execScript 执行脚本即可。

    【讨论】:

    • 必须将文档转换为IHTMLDocument2,以便您可以访问execScript 方法...如果仍然不起作用,请发布确切的错误消息。
    • 现在,它不会显示 Build 错误。我打开 IE 并单击该按钮。什么都没有出现。
    • 嗨,我已经编辑了我的另一个答案(在 Developing Internet Explorer Extensions? 上),所以现在您可以在 Exec 方法中访问 browser 对象...这是错误的!看一看:stackoverflow.com/a/5740004/195417
    • 任何想法我怎么能做到这一点也可以注入 CSS 样式代码
    • 你可以用 javascript 做的一切,你也可以用 C# 做......对象是一样的。看看这个:How to create a <style> tag with Javascript
    猜你喜欢
    • 2012-11-01
    • 2011-08-04
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多