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
It is not working because script tags added to the document are not evaluated automatically.
You must eval the script manually, like the following:
Also, you don't need to add the script tag at all... just execute the script using
window.execScript
.