如何将新的 javascript 附加到当前的 HTML WebBrowser 控件 [英] How to append new javascript to current HTML WebBrowser Control
问题描述
我正在尝试插入我自己的 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)
提前致谢.
推荐答案
请注意,当至少有一个 标签时,
eval
可以工作页面.
Note that eval
is known to work when there's already at least one <script>
tag on the page.
以下解决方案使用 webBrowser.InvokeScript("setTimeout", ...)
来执行一些 JavaScript(从而启用 eval
).我用 Windows Store 应用程序测试了这种方法,它在那里工作.我希望它也适用于 Windows Phone,希望你能确认一下.
The following solution uses webBrowser.InvokeScript("setTimeout", ...)
to execute some JavaScript (and thus enable eval
). I tested this approach with a Windows Store app and it works there. I expect it to work with Windows Phone too, hopefully you'd be able to confirm it.
async Task ExecScriptNotify()
{
// load a blank page
var tcsLoad = new TaskCompletionSource<object>();
this.webBrowser.NavigationCompleted += (s, eArgs) =>
tcsLoad.TrySetResult(Type.Missing);
this.webBrowser.NavigateToString("<body></body>");
await tcsLoad.Task;
// first add a script via "setTimeout", JavaScript gets initialized
var tcsInit = new TaskCompletionSource<object>();
this.webBrowser.ScriptNotify += (s, eArgs) =>
{
if (eArgs.Value == "initialized")
tcsInit.TrySetResult(Type.Missing);
};
this.webBrowser.InvokeScript("setTimeout",
new string[] { "window.external.notify('initialized')", "0" });
await tcsInit.Task;
// then use "eval"
this.webBrowser.InvokeScript("eval",
new string[] { "document.body.style.backgroundColor = 'yellow'" });
}
这篇关于如何将新的 javascript 附加到当前的 HTML WebBrowser 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!