如何使用浏览器控件将javascript注入iframe中
How to inject javascript into an iframe with webbrowser control?
我试图阻止某些javascript函数向用户显示警报。
我目前正在使用这个代码,但它不工作:
private void InjectAlertBlocker()
{
string alertBlocker = @"window.alert = function () { };
window.print = function () { };
window.open = function () { };
window.onunload = function () { };
window.prompt = function () { };
window.confirm = function() { };
window.onbeforeunload = function () { };";
wb.Document.InvokeScript("execScript", new Object[] { alertBlocker, "JavaScript" });
}
网页浏览器控件在后台运行,一些需要访问的网站显示警报,随机弹出给用户。
我已经尝试过类似的代码,但无法使它们任何一个工作
经过一些检查后,似乎这段代码确实阻止了警报。然而,在我的特殊情况下,警报来自iframe,这就是为什么我的代码没有阻止它。w3schools上的javascript警报示例就是一个完美的例子:http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert
如何重写iframe内的脚本?
更新2:我已经尝试注入javascript代码(由Buzinas在他的回答中提供)到主页,这将遍历所有iframe并覆盖它们的警报功能,但由于同源策略,它大多数时候会导致错误。
您可以将其附加为<script>
元素,而不是调用脚本:
HtmlElement head = wb.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = wb.Document.CreateElement("script");
IHTMLScriptElement el = (IHTMLScriptElement)scriptEl.DomElement;
el.text = @"window.alert = function () { };
window.print = function () { };
window.open = function () { };
window.onunload = function () { };
window.onbeforeunload = function () { };";
head.AppendChild(scriptEl);
要确保所有iframe都被阻塞,您可以这样做:
HtmlElement head = wb.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = wb.Document.CreateElement("script");
IHTMLScriptElement el = (IHTMLScriptElement)scriptEl.DomElement;
el.text = @"window.alert = function () { };
window.print = function () { };
window.open = function () { };
window.onunload = function () { };
window.onbeforeunload = function () { };
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
var win = iframes[i].contentWindow;
win.alert = function () { };
win.print = function () { };
win.open = function () { };
win.onunload = function () { };
win.onbeforeunload = function () { };
}";
head.AppendChild(scriptEl);
相关文章:
- 在 iFrame 中注入 JS 文件,通过 Google chrome 扩展名中的 ContentScript 进行
- 将 Javascript 从文本区域注入 Iframe
- 如何将数据从注入的内容脚本传递到iframe
- 将自定义CSS注入WP Admin登录弹出iframe
- 在iframe中注入js
- 奇怪的chrome扩展程序问题:将iframe注入Gmail
- 将内容脚本注入到不同扩展名的操作弹出窗口中的 iframe 中
- 将代码注入 iFrame 以隐藏特定元素
- 如何将 Jquery 对象从父 iframe 注入到嵌入式 iframe
- 如何将 HTML、CSS 和 jQuery 实时注入 iframe
- 将 HTML 源代码注入 iframe 以避免跨站点脚本问题
- 为什么在iframe中注入的JavaScript使用父窗口作为其全局作用域
- 将样式表注入Iframe(Facebook Like Box)
- 我可以将HTML注入iFrame吗
- 在加载时将代码注入iframe
- 如何使用浏览器控件将javascript注入iframe中
- 将javascript函数注入Iframe中
- 如何将javascript代码注入iframe
- 将芦荟注入iframe中
- 如何将html注入iframe,并在jquery-ui对话框中显示