Ⅰ 如何用c#本地代碼實現與Webbrowser中的javaScript交互
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1. = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
// Uncomment the following line when you are finished debugging.
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
}
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
}
}
鏈接0:codeproject中VB和js的交互
鏈接1:自定義數據類型的參數傳遞
代碼:
dynamic data = webBrowser1.Document.InvokeScript("eval", new[] {
"(function() { return { latitude: 1, longitude: 2 }; })()" });
MessageBox.Show("Data: " + data.latitude + ", " + data.longitude);
鏈接:添加js到已載入的網頁
代碼:
private void addScript(HtmlElement head, string scriptSource)
{
HtmlElement lhe_script = head.Document.CreateElement("script");
IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement;
script.src = scriptSource;
head.AppendChild(lhe_script);
}
addScript(Webbrowser.Head, @"<Change File Path here>jquery.min.js");
addScript(WebBrowser.Head, @"InjectMonitor.js");
Selenium則是一個利用http協議,來實現js和其他語言之間的通信,他強大的地方是js部分。
ide/main/src/content/selenium-runner.js
// overide _executeCurrentCommand so we can collect stats of the commands executed
_executeCurrentCommand : function() {
/**
* Execute the current command.
*
* @return a function which will be used to determine when
* execution can continue, or null if we can continue immediately
*/
var command = this.currentCommand;
LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |");
var handler = this.commandFactory.getCommandHandler(command.command);
if (handler == null) {
throw new SeleniumError("Unknown command: '" + command.command + "'");
}
command.target = selenium.preprocessParameter(command.target);
command.value = selenium.preprocessParameter(command.value);
LOG.debug("Command found, going to execute " + command.command);
updateStats(command.command);
this.result = handler.execute(selenium, command);
this.waitForCondition = this.result.terminationCondition;
},
selenium-api,CommandHandlerFactory是Api核心,在selenium-api.js,selenium-commandhandlers.js文件中實現。
Ⅱ WebBrowser和JS交互時怎麼使用VBarray 傳遞數組
轉化為字元串,之後在分割就對了
Ⅲ c#的webbrowser調用本地javascript腳本
你好!
你的意思是調用你自己寫的JS文件,而不是鏈接過去頁面裡面的腳本對吧!
其實你可以換個思路的。
webBrowser1.Navigate(http://gd.10086.cn/);
webBrowser1鏈接這個網頁後,它的DocumentText 裡面就有內容了。這個時候你可以把你的腳本動態寫到網頁上去的。示例:
webBrowser1.DocumentText + = " <script type='text/javascript'>function Alert_{ alert('hello world');} </script>";
然後再使用方法webBrowser1.Document.InvokeScript("Alert_");調用即可
如果是http://gd.10086.cn/本身存在的腳本,直接用上面的方法就行了。
Ⅳ WPF中WebBrowser 如何在頁面載入的時候注入js腳本
object[] objects = new object[2]; //寫入腳本方法,暫時專保留屬
objects[0] = 1615;
objects[1] = EnumDefenceAreaStatus.DefenceAreaStatus1;
web.InvokeScript("changeStatus", objects);
Ⅳ C# webbrowser 里如何調用網頁里的js函數
webBrowser1.Navigate("javascript:alert('hello');");
說明來
webBrowser1.Navigate("javascript:[你要執行的javascript語句源];");
如果你要執行那個函數代碼如下:
webBrowser1.Navigate("javascript:test.work('1','0','5');");
Ⅵ webbrowser 裡面的js怎麼調用C#
通過webBrowser實現#和javascript互調
實現步驟:
一、新建一個窗體,加入webBrowser控制項
控制項名:webBrowser1
二、在窗體後台代碼加入如下定義
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
...
}
三、載入網頁
webBrowser1.Navigate(Application.StartupPath + "/UpDateReport.htm");
webBrowser1.ObjectForScripting = this; //這句必須,不然js不能調用C#
四、調用腳本方法
/// <summary>
/// 腳本方法
/// </summary>
/// <param name="tag">JS函數名|參數1|參數2</param>
/// <returns></returns>
private object EXEC_JS(System.Windows.Forms.WebBrowser webBrowser, string tag)
{
string[] args = tag.Split('|');
if (args.Length == 1)
{
return webBrowser.Document.InvokeScript(args[0], null);
}
else
{
object[] objects = new object[args.Length - 1];
Array.Copy(args, 1, objects, 0, objects.Length);
return webBrowser.Document.InvokeScript(args[0], objects);
}
}
五、C#調用腳本方法例子,可以有返回值
//JS方法
<script language="javascript">
function js_fun(args)
{
alert("我是通過js腳本彈出的。你輸入的內容是:"+args);
return "JAVASCRIPT";
}
</script>
//C#代碼
object returnvalue = EXEC_JS(webBrowser1, "js_fun|參數字元串");
MessageBox.Show("js方法返回值是:" + returnvalue.ToString());
六、JS調用C#方法的例子
//C#方法
public string Test(string args)
{
return "你輸入的是:"+args;
}
//JS代碼
<script language="javascript">
window.onload = function()
{
var CS_returnvalue= window.external.Test("aaa");
alert(CS_returnvalue);
}
</script>
Ⅶ VB:高分請教webbrowser里的js與程序交互問題
VBA的代碼和vbs、js的代碼雖然在語法有相似之處,但是結構完全不一樣,VBA為編譯代碼,腳本為解釋代碼,兩者不能實現直接交互,只能通過間接交互實現
這里通過VBScript修改瀏覽器標題,觸發VB內部的TitleChange而實現交互
網頁代碼:
<a onclick=VBScript:document.title="VB:test('X','Y','Z')">文字</a>
窗體代碼:
Option Compare Text
Private Function Test(S1, S2, S3)
MsgBox S1 & S2 & S3
End Function
Private Sub Form_Load()
WebBrowser1.Navigate "e:\1.htm"
End Sub
Private Sub WebBrowser1_TitleChange(ByVal Text As String)
If Left(Text, 3) = "VB:" Then
Dim s As String, fncName As String, l As Long, args
s = Mid(Text, 4)
s = Replace(s, "(", " ")
s = Replace(s, ")", " ")
l = InStr(s, " ")
fncName = Trim(Left(s, l - 1))
args = Split(Mid(s, l), ",")
For i = 0 To UBound(args)
args(i) = Trim(args(i))
Next
Select Case fncName
Case "test"
Test args(0), args(1), args(2)
End Select
Else
Caption = Text
End If
End Sub
Ⅷ C#中webbrowser與javascript(js)交互,使程序集COM可見」。這樣做會爆漏程序的安全性嗎
開發環境是Visual Studio 2008 .閱讀此文需要有C#和javascript開發基礎。
1.首先新建一個項目,在默認窗體form1上拖拽一個webbrowser1.
做完這些基本的設置之後就可以讓webbrowser和js交互了,交互方法如下:
C#調用js函數的方法:
首先在js中定義被c#調用的方法:
function Messageaa(message)
{
alert(message);
}
在c#調用js方法Messageaaprivate void button1_Click(object sender, EventArgs e)
{ // 調用JavaScript的messageBox方法,並傳入參數
object[] objects = new object[1];
objects[0] = "c#diao javascript";
webBrowser1.Document.InvokeScript("Messageaa", objects);
}
用JS調用C#函數的方法:
首先在c#中定義被js調用的方法:public void MyMessageBox(string message)
{
MessageBox.Show(message);
}
在js中調用c#方法:<!-- 調用C#方法 -->
<button onclick="window.external.MyMessageBox('javascript訪問C#代碼')" >javascript訪問C#代碼</button>
Ⅸ C# webbrowser 調用JS函數 求教!!
webBrowser1.Navigate("javascript:alert('hello');");
webBrowser1.Document.InvokeScript("Messageaa", objects);