2012-11-21 78 views
1

我从此源创建了IE扩展: How to get started with developing Internet Explorer extensions? 而且它工作得很好。但我想改变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; 
    } 

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

回答

1

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

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

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

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

+0

不,这是行不通的。 – user1731468

+0

它显示自己错误的“文档” – user1731468

+0

文档必须转换为“IHTMLDocument2”,以便您有权访问'execScript'方法...如果它仍然无法正常工作,请发布确切的错误消息。 –

相关问题