Internet Explorer扩展 [英] Internet Explorer Extensions
问题描述
我从以下来源创建了IE扩展程序: 如何开始开发Internet Explorer扩展? 而且效果很好.但是我想改变
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;
}
对此:
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:
var document = browser.Document as IHTMLDocument2;
var window = document.parentWindow;
window.execScript(@"(function(){ alert('hello') })()");
此外,您根本不需要添加脚本标签...只需使用window.execScript
执行脚本.
Also, you don't need to add the script tag at all... just execute the script using window.execScript
.
这篇关于Internet Explorer扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!