[英]javascript windows phone 7.1
我尝试在Windows Phone中执行JavaScript
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream location = new IsolatedStorageFileStream(path,
System.IO.FileMode.Open, file);
HtmlDocument doc = new HtmlDocument();
doc.Load(location);
string newContent = "<script type='text/javascript'>"
+ "function getSelectionHtml()"
+ "{"
+ "var html = '';"
+"if (typeof window.getSelection != 'undefined')"
+"{"
+"var sel = window.getSelection();"
+"if (sel.rangeCount)"
+"{"
+" var container = document.createElement('div');"
+"for (var i = 0, len = sel.rangeCount; i < len; ++i)"
+"{"
+"container.appendChild(sel.getRangeAt(i).cloneContents());"
+"}"
+"html = container.innerHTML;"
+"}"
+"}"
+ "elseif (typeof document.selection != 'undefined')"
+ "{"
+ "if (document.selection.type == 'Text')"
+ "{"
+ "html = document.selection.createRange().htmlText;"
+ "}"
+"}"
+"return Html;"
+ "}"
+ "</script>";
HtmlNode newNode = HtmlNode.CreateNode(newContent);
// Get body node
HtmlNode body = doc.DocumentNode.SelectSingleNode("//head");
// Add new node as first child of body
body.PrependChild(newNode);
savefile(doc);
string path4 = "Cyrlej/Epub/The Motor/42461/@public@vhost@g@gutenberg@html@files@42461@42461-h@42461-h-0.htmcopie.html";
MyWebBrowser.Navigate(new Uri(path4, UriKind.Relative));
public static void savefile(HtmlDocument file)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("Cyrlej/Epub/The Motor/42461/@public@vhost@g@gutenberg@html@files@42461@42461-h@42461-h-0.htmcopie.html", FileMode.Create, isoStore))
{
file.Save(isoStream);
}
}
我已成功将脚本添加到html中,现在当我尝试通过以下方式调用脚本时
MyWebBrowser.InvokeScript("getSelectionHtml");
总是出现错误“错误:80020006”,任何人都有一个主意,谢谢
也许您在页面未加载时试图调用脚本。 尝试:
MyWebBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(Browser_LoadCompleted);
void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
MyWebBrowser.InvokeScript("getSelectionHtml");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.