如何将新的 javascript 附加到当前的 HTML WebBrowser 控件 [英] How to append new javascript to current HTML WebBrowser Control

查看:36
本文介绍了如何将新的 javascript 附加到当前的 HTML WebBrowser 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入我自己的 javascript 以在运行时呈现 HTML(index.html - 存在于隔离存储中).从 javascript 调用 c# webBrowser_ScriptNotify.我将如何实现这一目标,这是我实现这一目标的尝试.

I am trying to insert my own javascript to present HTML(index.html - present in IsolatedStorage) at runtime. To get call from javascript to c# webBrowser_ScriptNotify. How I will achieve this, here is my attempts to achieve this.

string script = " <script type=\"text/javascript\"> function scriptNotify() { window.external.notify(\"runtime\");}</script>";
byte[] param = GetBytes(script);

webBrowser.Navigate(new Uri("/index.html", UriKind.Relative), param,"Content-Type: application/x-www-form-urlencoded");


private void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{            
   if (e.Value.ToString().Equals("runtime"))
   {
       Debug.WriteLine("Call from dynamic javascript");
   }
}   

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

更新

这是我在当前打开的 HTML 页面中注入脚本标签的新尝试.

Here my new try to inject script tag in currently opened HTML page.

var scriptString = "var script=document.createElement('script');"
                                   + "script.type=\"text/javascript\";"
                                   + "script.InnerHTML=\"function callMe(){window.external.notify(\"runtime\")};\""
                                    + "document.getElementsByTagName('head')[0].appendChild(script);";

webBrowser.InvokeScript("eval", scriptString);

由于一些系统错误而失败.

its failed with some system error.

System.SystemException was unhandled
Message: An unhandled exception of type 'System.SystemException' occurred in Microsoft.Phone.Interop.ni.dll
Additional information: An unknown error has occurred. Error: 80020101.

堆栈跟踪:

at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr)
   at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName, String[] args)
   at Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName, String[] args)
   at WebBrowserJSTest.MainPage.webBrowser_LoadCompleted(Object sender, NavigationEventArgs e)

提前致谢.

推荐答案

请注意,当至少有一个

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆