Internet Explorer Extensions

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

I created an IE extension from this source: How to get started with developing Internet Explorer extensions? And it work great. But i want to change

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

to this:

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

But when i build it, and click on the button. I doesn't give me an alert message. I want just only inject a script. Any idea or trick... to fix this problem

回答1:

It is not working because script tags added to the document are not evaluated automatically.

You must eval the script manually, like the following:

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

Also, you don't need to add the script tag at all... just execute the script using window.execScript.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!