javascript生成的链接C#-Windows窗体 [英] javascript generated links C# - WIndow Forms

查看:59
本文介绍了javascript生成的链接C#-Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在升级我以前用php和java脚本进行的游戏.它实际上是基于Web Broswer的,在游戏逻辑中,我会问用户一些问题,并给他们三个选择的三个答案,但其中只有一个是正确的.因此,我的页面会以javascript生成的链接的形式生成此问题,并将其放置在页面上,并以图片/图片的形式记录这些链接.

因此,出于某些原因,我将游戏升级到基于桌面的应用程序,我使用Windows IE ActiveX Web浏览器控件从运行Apache的本地服务器上渲染网页(没关系),问题是我可以获取所有链接在页面中被硬编码到页面本身中.使用HTMLCollection数据结构,而不是javascript生成的数据结构.我尝试使用事件处理程序,该事件处理程序在页面完成后立即启动,希望javascript能够完成生成链接并解析到页面中,但仍然没有运气.

如何使用C#代码评估JavaScript生成的链接?我只需要此或某些提示背后的逻辑.不是整个代码.

谢谢

Hi
I''m upgrading a game i did a while back with php and java script. Its actually Web Broswer based, In the game logic I would ask users questions and give them three threee answers to chhose from, but only one of them will be right. So my page would generate this questions as javascript generated links and place them on the page, and note this links where in a form of images / pictures.

So I''m upgrading the game to a desktop based application for some reasons, I used the Windows IE ActiveX Web Browser control to render webpages from a local server running Apache (not that it matters), The problem is i can get all links in the page that are hard-coded into the page itself. using the HTMLCollection Datastructure, but not the javascript generated one. I tried using an event handler that fires up just after the page is done hoping by then the javascript would have finished generating links and parsed into the page but still no luck.

How can i evaluate JavaScript generated links with my C# code? I just need the logic behind this or some hints. Not the whole code.

Thanks

推荐答案

mshauny,

好的,现在我明白了.即使我必须承认这是一个可怕的骇客.
但这有效:

Hi mshauny,

OK now I got it. Even though I must admit it''s a terrible hack.
But it works:

<html>
	<head>
	</head>
	<body onload="javascript:doIt();">
		<div>Here''s a little something</div>
		<div id="test">Nothing to see here!</div>
		<div>Something ...</div>
	</body>
	<script type="text/javascript">
		function doIt()
		{
			alert("Wazzup!?");
			var ele = document.getElementById("test");
			if(ele)
			{
				alert("Right on bro!");
				ele.innerHTML = "<a href=''http://www.google.de''>Google schmoogle!</a>";
				window.status="done setting up links"; // this triggers the event in our forms code
			}
			else
			{
				alert("Epic fail, looser!");
			}
		}
	</script>
</html>





void webBrowser1_StatusTextChanged(object sender, System.EventArgs e)
{
    if (webBrowser1.StatusText.Contains("done setting up links"))
    {
        HtmlElementCollection coll = webBrowser1.Document.GetElementsByTagName("a");
        webBrowser1.Document.Window.StatusBarText = "And now for something completely different"; // this keeps our event from retriggering
    }
}



第一个代码块是我答案的旧版本中的html.动态创建链接后,将修改Windows状态文本.这是由Windows窗体代码文件中的第二个代码块检测到的.我在Visual Studio的属性/事件"对话框中找不到此事件,但是由于MSDN和intellisense,我发现webBrowser存在这样的事件.不需要将事件处理程序中的状态文本设置为完成设置链接"以外的设置,因为状态文本会为页面上发生的某些操作而被覆盖,然后再恢复.
这将使if表达式不只一次为真,但是我们只需要
在链接的javascript设置之后进行一次.

现在剩下要做的就是在From.Design.cs中存在的跟随代码块中,将事件处理程序与事件挂钩:InitializeComponent():



First code block is the html from the former version of my answer. After the links are created dynamically the windows status text is modified. This is detected by the second code block which lives inside the windows forms code file. I couldn''t find this event in the Properties/Events dialog ina Visual Studio but I found that such an event exists for webBrowser thanks to MSDN and intellisense. The setting of status text inside event handler to something other than "done setting up links" is nescessary because status text is overwritten for some occurring actions on the page and later restored.
This would cause the if expression to be true more than once, but we only need
it once after the javascript setup of the links.

All thats left to do now is to hook up the event handler with the event in the followin code block that lives inside From.Design.cs: InitializeComponent():

this.webBrowser1.StatusTextChanged += new System.EventHandler(webBrowser1_StatusTextChanged);



请告诉我您对这种特殊"解决方案的看法.

干杯

Manfred



Please tell me what you think about this "special" solution.

Cheers

Manfred


感谢您的快速回复.但如果我可以发表评论,那么问题不在于html页面本身.与应用程序一起使用.我能得到它吗?如果是的话,如何或最重要的是何时?一切都加载完之后?
Thanks for the quick reply. but If i may comment, the problem is not with the html page itself. Is with the application. can i get it? if so how or most importantly when? after everything has loaded?


哇,您的骇客作品了.抱歉,但是我遇到了最后一个问题.

我看到您在浏览器事件处理程序上应用了hack,但是问题是页面标题是在JavaScript上设置的.并且仅在脚本生成超链接之后才评估该语句.如果标题不变,该怎么办?就像是在页面本身中设置的一样.那么JavaScript将与标题无关.我将如何知道浏览器已完成对脚本的评估,因为它们将是最后一个要输出的脚本?预先感谢
Wow, your hack works. Sorry but I got one last problem though.

I see the hack you applied on the browser event handler, but the problem is that the page title is set on the JavaScript. and that statement is only evaluated after the script has generated the hyper link. what if the title does not change? like it''s set in the page itself. then JavaScript would have nothing todo with the title. how will i know that the browser is done evaluating the script since they will be the last one to be outputted? Thanks in advance


这篇关于javascript生成的链接C#-Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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