简体   繁体   English

在Windows Phone 8应用程序中动态注入javascript

[英]Dynamically injecting javascript in windows phone 8 application

I am just trying to add some dynamic javascript to the content of my web browser . 我只是想在我的网络浏览器的内容中添加一些动态javascript。 My scenario is ,I want to display an Ad which is mapped to a zoneID that will be entered by the user from the phone screen. 我的方案是,我想显示一个广告,该广告映射到将由用户从电话屏幕输入的zoneID。 To be more precise I just want to add the following javascript 更确切地说,我只想添加以下javascript

    <script src="http://rq.vserv.mobi/delivery/js.php?zoneid=d03e63db&amp;vr=S-JS-1.0.0"></script>&nbsp;</div>

where zoneid will dynamically vary upon the user input. 其中zoneid将根据用户输入动态变化。

I searched a lot for this as I have never worked with JS before ,and I got this article - 我搜索了很多,因为我之前从未使用过JS,我得到了这篇文章 -

http://matthiasshapiro.com/2012/11/26/windows-phone-html-5-app-basics/ http://matthiasshapiro.com/2012/11/26/windows-phone-html-5-app-basics/

There are two ways I tried to implement this First one is I tried to navigate to the hardcoded HTML content including script- 我试图实现这两种方法首先是我尝试导航到硬编码的HTML内容,包括脚本 -

     webBrowser.NavigateToString(string.Format("<html><head><meta name=\"viewport\" content=\"width=device-width,height=device-height,user-scalable=no\"><script type=\"text/javascript\" src=\"http://rq.vserv.mobi/delivery/js.php?zoneid={0}&amp;vr=S-JS-1.0.0\"></script></head><body></body></html>",p_zoneID));

The Second one is having a local HTML page ,Loading it on the webbrowser's Loaded event,and then inject a script into it - 第二个是有一个本地HTML页面,将它加载到webbrowser的Loaded事件上,然后将一个脚本注入其中 -

     webBrowser.InvokeScript("eval",
                new string[] { "document.getElementById('dynamicTitle').innerHTML = '" + javascript_tag + "';" });

Both the ways are working in the second case when I get the ad,that ad is clickable as I am getting the whole script mapped with that ad .(I checked it by using the webbrowser's SaveToString() method.) 当我获得广告时,这两种方式都适用于第二种情况,因为我将整个脚本映射到该广告时,该广告是可点击的。(我使用webbrowser的SaveToString()方法进行了检查。)

But the problem is that in the first case the ad that I am getting is not clickable as the script related to that is not there .(In the content of HTML page)(Checked this also using the SaveToString();) 但问题是,在第一种情况下,我得到的广告不可点击,因为与之相关的脚本不存在。(在HTML页面的内容中)(也使用SaveToString()检查;);

I have tried a lot but unable to find any clue. 我已经尝试了很多,但无法找到任何线索。 What should be my approach now to know why that ad is not clickable and why I am not getting the script mapped with it . 现在我的方法应该是知道为什么广告不可点击以及为什么我没有将脚本映射到它。

I can also post the HTML content in both the cases if suggested. 如果建议,我也可以在两种情况下发布HTML内容。 Thanks is advance, please help me to go solve this as I am stuck at this point and need clarification of things . 谢谢是提前,请帮我解决这个问题,因为我在这一点上陷入困​​境,需要澄清事情。

You can append scripts to the head using JS and they will run as they should, here is a great answer on StackOverflow showing how it is done with working examples and it uses plain javascript. 您可以使用JS将脚本附加到头部,它们将按预期运行, 这是StackOverflow的一个很好的答案,显示如何使用工作示例完成它并使用普通的javascript。

Hope that helps you. 希望对你有所帮助。

In the first case you are injecting the full html in the web browser control which is having javascript source in it but you are never assigning that to the "dynamicTitle". 在第一种情况下,您将在Web浏览器控件中注入完整的html,其中包含javascript源,但您永远不会将其分配给“dynamicTitle”。 Which you are doing in the second case using eval method of javascript and the string in the variable "javascript_tag" is getting assigned to the innerHTML of the "dynamicTitle". 你在第二种情况下使用javascript的eval方法和变量“javascript_tag”中的字符串进行分配到“dynamicTitle”的innerHTML。

You need to do the same in the first approach as well but there is no way to access the html document directly in windows phone except as a string. 您也需要在第一种方法中执行相同的操作,但除了作为字符串之外,无法直接在Windows Phone中访问html文档。 Below is the code: 以下是代码:

add reference to HtmlAgilityPack. 添加对HtmlAgilityPack的引用。 Follow this link: http://www.tareqateik.com/html-agility-pack%E2%80%93windows-phone-8#.U_eAtPmSySp 请点击此链接: http//www.tareqateik.com/html-agility-pack%E2%80%93windows-phone-8#.U_eAtPmSySp

//fetch the html as string
string htmlAsString = webBrowser.SaveToString();
//create an html document from the string 
HtmlDocument htmlDocument = new HtmlDocument(); 
htmlDocument.LoadHtml(htmlPage);
htmlDocument.GetElementbyId("dynamicTitle").InnerHtml = javascript_tag;

After this you need to load the edited html document in the webBrowser again. 在此之后,您需要再次在webBrowser中加载已编辑的html文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM