简体   繁体   English

在webbrowser网站上运行Javascript代码

[英]Run Javascript code on webbrowser website

What I have is simple, a form with in that form a webbrowser-controll. 我所拥有的很简单,一种形式的形式是一个webbrowser-controll。

That webbrowser goes to a website. 那个webbrowser去了一个网站。 And that all works. 这一切都有效。

But what I wan't to do when he's on the page is to let him run some javascript code (or jQuery if that's possible) to get all the text thats between the <b></b> tags on the website. 但是当他在页面上时我不想做的就是让他运行一些javascript代码(或jQuery,如果可能的话)来获取网站上<b></b>标签之间的所有文本。

How should I do that? 我该怎么办?

EDIT: Thanks to Birk, the code now looks like this: 编辑:感谢Birk,代码现在看起来像这样:

HtmlElementCollection bigFontTags = webBrowser1.Document.GetElementById("Frame_A").Document.GetElementsByTagName("b");
            string[] textPieces = new string[bigFontTags.Count];
            for (int i = 0; i < bigFontTags.Count; i++) {
                textPieces[i] = bigFontTags[i].InnerText;
            }
            //process text
            string bigText = String.Join(" ", textPieces);
            MessageBox.Show(bigText);

But that doesn't work. 但这不起作用。 That is because the element that i need to get, is deep inside frame's and frameset's.... This is the layout (simple): 那是因为我需要得到的元素是在框架和框架集的内部......这就是布局(简单):

<frameset rows="0,1000" border="0" frameborder="0">

    <frame src="/mail/Login?domain=telfort.nl&amp;style=default&amp;plain=0" name="SContentFrame" id="SContentFrame" scrolling="Auto" noresize="">
    <frameset rows="88,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF">

        <frameset cols="182,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF" onunload="ExitLogout()" onload="LoadFrames('/mail/Navigation?sid=1FBE4F29181F18D9358ABC082C7DEE1B6C67481B&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;style=default','/mail/MessageList?sid=1FBE4F29181F18D9358ABC082C7DEE1B6C67481B&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;chk=1&amp;style=default')">

        <frame src="/supp/blank.htm" name="Frame_A" id="Frame_A" frameborder="0" scrolling="Yes" marginwidth="0" marginheight="0" bordercolor="#FFFFFF" border="0">
        <form name="phoenix" method="post" action="" onsubmit="return clickedButton">
And then are here some tables and the content that i want to get.....

So how can i get that content from there??? 那我怎么能从那里得到那些内容? (in a nice way???) (以一种很好的方式???)

EDIT2: I will post the generated source of the website: (NOTE! i've deleted everything that wassn't relevant... So only the way from the begin to the content) EDIT2:我将发布生成的网站来源:(注意!我删除了所有不相关的内容......所以只有从开头到内容的方式)

<html><head>
<title>Telfort - Webmail</title>
<link rel="SHORTCUT ICON" href="http://www.telfort.nl/images/template/favicon.ico">
<link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
<!-- main.tpl 20001107 -->
<script type="text/javascript" src="/supp/phsec.js"></script>
<script type="text/javascript" src="/supp/client_sniffer.js"></script>

<script type="text/javascript" src="chrome-extension://bfbmjmiodbnnpllbbbfblcplfjjepjdn/js/injected.js"></script></head>
<frameset rows="0,1000" border="0" frameborder="0">
    <frame src="/mail/Login?domain=telfort.nl&amp;style=default&amp;plain=0" name="SContentFrame" id="SContentFrame" scrolling="Auto" noresize="">
        <html><head>
        <title>Telfort - Webmail</title>
        <link rel="SHORTCUT ICON" href="http://www.telfort.nl/images/template/favicon.ico">
        <link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
        <!-- main.tpl 20001107 -->
        <script type="text/javascript" src="/supp/phsec.js"></script>
        <script type="text/javascript" src="/supp/client_sniffer.js"></script>
        </head>
        <frameset rows="0,1000" border="0" frameborder="0">
            <frame src="/mail/Login?domain=telfort.nl&amp;style=default&amp;plain=0" name="SContentFrame" id="SContentFrame" scrolling="Auto" noresize="">
                <html><head>
                <title>Telfort - Webmail</title>
                <link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
                <!-- main_frame.tpl 20060510 -->
                <script src="/supp/phif.js" type="text/javascript"></script>
                <script type="text/javascript">
                <!--
                function LoadFrames(t1,t2) {
                  NavWin(frames["Frame_NAV"],t1);
                  NavWin(frames["Frame_A"],t2);
                }

                function ExitLogout() {
                  window.open(NavURL("/mail/Logout?sid=5D3995C305E2F91F575EDF1E2F11F5D75A7DF6D8&userid=marcmeesters%40telfort.nl&seq=+Q&auth=+A&style=default&popup=1"),"Logout","resizable=yes,scrollbars=yes,status=0,width=10,height=10");
                }
                //-->
                </script>
                </head>
                <frameset rows="88,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF">
                    <frameset cols="182,*" framespacing="0" frameborder="0" border="0" bordercolor="#FFFFFF" onunload="ExitLogout()" onload="LoadFrames('/mail/Navigation?sid=5D3995C305E2F91F575EDF1E2F11F5D75A7DF6D8&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;style=default','/mail/MessageList?sid=5D3995C305E2F91F575EDF1E2F11F5D75A7DF6D8&amp;userid=marcmeesters%40telfort.nl&amp;seq=+Q&amp;auth=+A&amp;srcfolder=INBOX&amp;chk=1&amp;style=default')">
                        <frame src="/supp/blank.htm" name="Frame_A" id="Frame_A" frameborder="0" scrolling="Yes" marginwidth="0" marginheight="0" bordercolor="#FFFFFF" border="0">
                            <html><head>
                            <title>Berichtenlijst</title>
                            <!-- folders_msglist.tpl 20001106 -->

                            <link rel="stylesheet" href="/style/telfort/stylesheet.css" type="text/css" media="screen">
                            </head>
                            <body marginwidth="0" marginheight="0">
                            <form name="phoenix" method="post" action="" onsubmit="return clickedButton">

                            AND HERE THE CONTENT!!!!
                            </form>
                    </frameset>
                </frameset>
        </frameset>
</frameset>
</html>

EDIT 3 : Thanks to Birk, the problem is solved, and this is the answer: 编辑3:感谢Birk,问题解决了,这就是答案:

HtmlWindow SContentFrame = webBrowser1.Document.Window.Frames[1];
            HtmlWindow Frame_A = SContentFrame.Document.Window.Frames[2];
            HtmlElementCollection bigFontTags = Frame_A.Document.GetElementsByTagName("b");
            string[] textPieces = new string[bigFontTags.Count];
            for (int i = 0; i < bigFontTags.Count; i++) {
            textPieces[i] = bigFontTags[i].InnerText;
            }
            string bigText = String.Join(" ", textPieces);
            MessageBox.Show(bigText);

Create a DocumentCompleted-event and process all tags using ((WebBrowser)sender).Document.GetElementsByTagName("b"); 使用((WebBrowser)sender).Document.GetElementsByTagName("b");创建DocumentCompleted事件并处理所有标记((WebBrowser)sender).Document.GetElementsByTagName("b"); .

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlElementCollection bigFontTags = ((WebBrowser)sender).Document.GetElementsByTagName("b");
            string[] textPieces=new string[bigFontTags.Count];
            for (int i = 0; i < bigFontTags.Count; i++)
            {
                textPieces[i] = bigFontTags[i].InnerText;
            }
            //process text
            string bigText = String.Join(" ", textPieces);
            MessageBox.Show(bigText);
        }

That's not JavaScript, but the result is exactly the same. 这不是JavaScript,但结果完全一样。

EDIT #2: For all those nested frames you could use: 编辑#2:对于所有这些嵌套框架,您可以使用:

HtmlElementCollection bigFontTags = webBrowser1.Document.GetElementById("SContentFrame").Document.GetElementById("SContentFrame").Document.GetElementById("Frame_A").Document.GetElementsByTagName("b");

That should work, if I understand your frameset structure correctly. 如果我正确理解你的框架结构,这应该工作。

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

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