2013-04-17 120 views
1

我嘗試在Windows手機執行JavaScript的的JavaScript的Windows Phone 7.1

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/@[email protected]@[email protected]@[email protected]@[email protected]@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/@[email protected]@[email protected]@[email protected]@[email protected]@42461-h-0.htmcopie.html", FileMode.Create, isoStore)) 
      { 

       file.Save(isoStream); 
      } 
     } 

我成功添加腳本到我的HTML,現在當我嘗試通過

MyWebBrowser.InvokeScript("getSelectionHtml");

調用腳本總是我得到錯誤「錯誤:80020006」 任何人有一個想法 謝謝你提前

回答

0

也許你在頁面未加載時試圖調用腳本。試試:

MyWebBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(Browser_LoadCompleted); 

void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) 
{ 
    MyWebBrowser.InvokeScript("getSelectionHtml");    
}