当前位置:编程学习 > C#/ASP.NET >>

谁能帮我看一下C# WebBrowser如何重写执行网页中的js代码

我想写一段代码,跳过视频播放前的验证码
网站的js脚本 和我写的代码如下,但是却无效,谁能帮我看一下啊

<SCRIPT language=javascript type=text/javascript>
           var startTime = new Date();
           function t() {
               var validWait = 30000;
               var endTime = new Date();
               var validPeriod = endTime - startTime;
               if (validPeriod < validWait) {

               }
               else {
                   alert("没有在" + validWait / 1000 + "秒内回答正确,课程即将退出");
                   top.close();
               }
                tt=setTimeout(t, 1000);
           }
           t();
           function valid() {



               $.get("codeimg.aspx?answer=" + $("#validanswer").val() + "&" + Math.random(), function(data) {
                   if (data == 0) {
                       alert("答案错误");
                       ChangeCodeImg();
                   }
                   if (data == 1) {
                       document.getElementById("playframe").style.display = '';
                       window.clearTimeout(tt);
                       //ChangeCodeImg();
                       closeLayer();
                       document.getElementById("playframe").src = framesrc;
                   }

               });

           }
           function ChangeCodeImg() {
               var a = "CodeImg.aspx?" + rand(10000000);
               $.each($(".ImageCheck"), function() {
                   $(this).attr("src", a);
               });        
           }
           function rand(number) {
               return Math.ceil(rnd() * number);
           }

           rnd.today = new Date();
           rnd.seed = rnd.today.getTime();

           function rnd() {
               rnd.seed = (rnd.seed * 9301 + 49297) % 233280;
               return rnd.seed / (233280.0);
           };
           //弹出层
           function openLayer(objId, conId) {
               var arrayPageSize = getPageSize(); //调用getPageSize()函数
               var arrayPageScroll = getPageScroll(); //调用getPageScroll()函数
               if (!document.getElementById("popupAddr")) {
                   //创建弹出内容层
                   var popupDiv = document.createElement("div");
                   //给这个元素设置属性与样式
                   popupDiv.setAttribute("id", "popupAddr")
                   popupDiv.style.position = "absolute";
                   //popupDiv.style.border = "1px solid #ccc";
                   //popupDiv.style.background = "#FF0000";
                   popupDiv.style.zIndex = 99;
                   //创建弹出背景层
                   var bodyBack = document.createElement("div");
                   bodyBack.setAttribute("id", "bodybg")
                   bodyBack.style.position = "absolute";
                   bodyBack.style.width = "100%";
                   bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
                   bodyBack.style.zIndex = 98;
                   bodyBack.style.top = 0;
                   bodyBack.style.left = 0;
                   bodyBack.style.filter = "alpha(opacity=30)";
                   bodyBack.style.opacity = 0;
                   bodyBack.style.background = "#000000";
                   //实现弹出(插入到目标元素之后)
                   var mybody = document.getElementById(objId);
                   insertAfter(popupDiv, mybody); //执行函数insertAfter()
                   insertAfter(bodyBack, mybody); //执行函数insertAfter()
               }
               //显示背景层
               document.getElementById("bodybg").style.display = "";
               //显示内容层
               var popObj = document.getElementById("popupAddr")
               popObj.innerHTML = document.getElementById(conId).innerHTML;
               popObj.style.display = "";
               //让弹出层在页面中垂直左右居中(统一)
               // popObj.style.width  = "600px";
               // popObj.style.height = "400px";
               // popObj.style.top  = arrayPageScroll[1] + (arrayPageSize[3] - 35 - 400) / 2 + 'px';
               // popObj.style.left = (arrayPageSize[0] - 20 - 600) / 2 + 'px';
               //让弹出层在页面中垂直左右居中(个性)
               var arrayConSize = getConSize(conId)
               popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2 - 50 + 'px';
               popObj.style.left = (arrayPageSize[0] - arrayConSize[0]) / 2 - 50 + 'px';
           }
           //获取内容层内容原始尺寸
           function getConSize(conId) {
               var conObj = document.getElementById(conId)
               conObj.style.position = "absolute";
               conObj.style.left = -1000 + "px";
               conObj.style.display = "";
               var arrayConSize = [conObj.offsetWidth, conObj.offsetHeight]
               conObj.style.display = "none";
               return arrayConSize;
           }
           function insertAfter(newElement, targetElement) {//插入
               var parent = targetElement.parentNode;
               if (parent.lastChild == targetElement) {
                   parent.appendChild(newElement);
               }
               else {
                   parent.insertBefore(newElement, targetElement.nextSibling);
               }
           }
           //获取滚动条的高度
           function getPageScroll() {
               var yScroll;
               if (self.pageYOffset) {
                   yScroll = self.pageYOffset;
               } else if (document.documentElement && document.documentElement.scrollTop) {
                   yScroll = document.documentElement.scrollTop;
               } else if (document.body) {
                   yScroll = document.body.scrollTop;
               }
               arrayPageScroll = new Array('', yScroll)
               return arrayPageScroll;
           }
           //获取页面实际大小
           function getPageSize() {
               var xScroll, yScroll;
               if (window.innerHeight && window.scrollMaxY) {
                   xScroll = document.body.scrollWidth;
                   yScroll = window.innerHeight + window.scrollMaxY;
               } else if (document.body.scrollHeight > document.body.offsetHeight) {
                   xScroll = document.body.scrollWidth;
                   yScroll = document.body.scrollHeight;
               } else {
                   xScroll = document.body.offsetWidth;
                   yScroll = document.body.offsetHeight;
               }
               var windowWidth, windowHeight;
               if (self.innerHeight) {
                   windowWidth = self.innerWidth;
                   windowHeight = self.innerHeight;
               } else if (document.documentElement && document.documentElement.clientHeight) {
                   windowWidth = document.documentElement.clientWidth;
                   windowHeight = document.documentElement.clientHeight;
               } else if (document.body) {
                   windowWidth = document.body.clientWidth;
                   windowHeight = document.body.clientHeight;
               }
               var pageWidth, pageHeight
               if (yScroll < windowHeight) {
                   pageHeight = windowHeight;
               } else {
                   pageHeight = yScroll;
               }
               if (xScroll < windowWidth) {
                   pageWidth = windowWidth;
               } else {
                   pageWidth = xScroll;
               }
               arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
               return arrayPageSize;
           }
           //关闭弹出层
           function closeLayer() {
              
               document.getElementById("popupAddr").style.display = "none";
               document.getElementById("bodybg").style.display = "none";
               return false;
 
           }
           ChangeCodeImg();
           openLayer("maindiv", "poplayer");
           var framesrc = "";
           $(document).ready(function() {
               framesrc = document.getElementById("playframe").src;
               document.getElementById("playframe").src = "";
           });
</SCRIPT>


--------------------编程问答-------------------- 上面是网站弹出验证码的代码,下面是我写的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//下面2个组件需要在vs中添加引用
using mshtml;//添加引用 -> .Net ->Microsoft.mshtml
using SHDocVw;//添加引用 ->com->MicroSoft Internet Controls 
namespace AutoClickConfirm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SHDocVw.WebBrowser wb;
        mshtml.IHTMLDocument2 doc;
        private void btnNavigator_Click(object sender, EventArgs e)
        {
            string url = cmbUrl.Text.Trim().ToLower();
            if (url.StartsWith("http://") || url.StartsWith("https://"))
            {
                webBrowser1.Navigate(url);
                wb = webBrowser1.ActiveXInstance as SHDocVw.WebBrowser;
                wb.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);

            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        void wb_NavigateComplete2(object pDisp, ref object URL)
        {
            doc = wb.Document as mshtml.IHTMLDocument2;
            //执行javascript脚本,覆盖系统的confirm函数,直接return true,这样调用confirm函数都会执行到确认按钮了,同理可以重写系统中的其他函数
            //doc.parentWindow.execScript("function confirm(){return true;}", "javascript");
            //doc.parentWindow.execScript("function alert(){}", "javascript");//设置为alert为空函数体,就不会挂起javascript代码执行了
            doc.parentWindow.execScript("function function(data){return 1;}", "javascript");
            doc.parentWindow.execScript("function valid(){document.getElementById('playframe').style.display = ''; window.clearTimeout(tt); closeLayer(); document.getElementById'playframe').src = framesrc; }", "javascript");
            
           //document.invertscript("(函数名就行)");

            


        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(cmbUrl.Text);
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,