如何从该方法内部识别调用 window.external.method 的 Web 浏览器控件实例?
How to identify a web browser control instance called for window.external.method from inside this method?
我可以请你帮忙吗?
我在表单上有一组网络浏览器控件。
所有网络浏览器都设置为 webBrowser.ObjectForScripting = this;
因此所有脚本调用都在表单的代码中进行管理。
在我说的代码中:
public void Method1(string title)
{
WebBrowser wb = (WebBrowser)some-object;
MessageBox.Show(title + wb.Url.ToString());
}
如果通过 JavaScript 从 Web 浏览器控件之一调用,如何获取调用此方法的 Web 浏览器实例(某些对象):
<script>
window.external.Method1('Hello');
</script>
谢谢!!
我看到了一些常见的选项:
- 通常,调用者有责任向被调用者介绍自己。
- 当只有一个呼叫者可以呼叫时,被呼叫者知道它是呼叫者。
示例 1 - 调用者自我介绍
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var txt =
@"<html>" +
@"<body>" +
@"<a href=""#"" " +
@"onclick=""window.external.Method1(this, 'Hello');"">" +
@"Click here.</a>" +
@"</body>" +
@"</html>";
webBrowser1.ObjectForScripting = this;
webBrowser2.ObjectForScripting = this;
webBrowser1.DocumentText = txt;
webBrowser2.DocumentText = txt;
webBrowser1.Document.Window.Name = webBrowser1.Name;
webBrowser2.Document.Window.Name = webBrowser2.Name;
}
public void Method1(object sender, string s)
{
//sender is the anchor element
dynamic window = ((dynamic)((dynamic)sender).document).defaultView;
var windowName = window.Name;
var control = this.Controls.Find(windowName, true);
MessageBox.Show(s);
}
}
示例 2 - 被呼叫者认识呼叫者
private void Form1_Load(object sender, EventArgs e)
{
var txt =
@"<html>" +
@"<body>" +
@"<a href=""#"" " +
@"onclick=""window.external.Method1('Hello');"">" +
@"Click here.</a>" +
@"</body>" +
@"</html>";
webBrowser1.ObjectForScripting = new ScriptingObject(webBrowser1);
webBrowser2.ObjectForScripting = new ScriptingObject(webBrowser2);
webBrowser1.DocumentText = txt;
webBrowser2.DocumentText = txt;
}
[ComVisible(true)]
public class ScriptingObject
{
WebBrowser webBrowser;
public ScriptingObject(WebBrowser w)
{
webBrowser = w;
}
public void Method1( string s)
{
//This class knows which `WebBrowser` control calls its methods.
MessageBox.Show(s);
}
}
我可以请你帮忙吗?
我在表单上有一组网络浏览器控件。 所有网络浏览器都设置为 webBrowser.ObjectForScripting = this; 因此所有脚本调用都在表单的代码中进行管理。
在我说的代码中:
public void Method1(string title)
{
WebBrowser wb = (WebBrowser)some-object;
MessageBox.Show(title + wb.Url.ToString());
}
如果通过 JavaScript 从 Web 浏览器控件之一调用,如何获取调用此方法的 Web 浏览器实例(某些对象):
<script>
window.external.Method1('Hello');
</script>
谢谢!!
我看到了一些常见的选项:
- 通常,调用者有责任向被调用者介绍自己。
- 当只有一个呼叫者可以呼叫时,被呼叫者知道它是呼叫者。
示例 1 - 调用者自我介绍
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var txt =
@"<html>" +
@"<body>" +
@"<a href=""#"" " +
@"onclick=""window.external.Method1(this, 'Hello');"">" +
@"Click here.</a>" +
@"</body>" +
@"</html>";
webBrowser1.ObjectForScripting = this;
webBrowser2.ObjectForScripting = this;
webBrowser1.DocumentText = txt;
webBrowser2.DocumentText = txt;
webBrowser1.Document.Window.Name = webBrowser1.Name;
webBrowser2.Document.Window.Name = webBrowser2.Name;
}
public void Method1(object sender, string s)
{
//sender is the anchor element
dynamic window = ((dynamic)((dynamic)sender).document).defaultView;
var windowName = window.Name;
var control = this.Controls.Find(windowName, true);
MessageBox.Show(s);
}
}
示例 2 - 被呼叫者认识呼叫者
private void Form1_Load(object sender, EventArgs e)
{
var txt =
@"<html>" +
@"<body>" +
@"<a href=""#"" " +
@"onclick=""window.external.Method1('Hello');"">" +
@"Click here.</a>" +
@"</body>" +
@"</html>";
webBrowser1.ObjectForScripting = new ScriptingObject(webBrowser1);
webBrowser2.ObjectForScripting = new ScriptingObject(webBrowser2);
webBrowser1.DocumentText = txt;
webBrowser2.DocumentText = txt;
}
[ComVisible(true)]
public class ScriptingObject
{
WebBrowser webBrowser;
public ScriptingObject(WebBrowser w)
{
webBrowser = w;
}
public void Method1( string s)
{
//This class knows which `WebBrowser` control calls its methods.
MessageBox.Show(s);
}
}