doit 2019-03-12 21:38 采纳率: 0%
浏览 1544
已结题

用javascript监听第三方页面上的所有ajax请求

1 1 集成了另外一个web程序,我们把那个web程序放在我们提供的iframe里。那个web程序只有在第一次初始化的时候是一个普通的HTTP请求,之后在页面上的操作,都是通过ajax来操作的。现在我们有个需求,我们需要知道iframe里面做了什么操作。
没法改别人的代码,那我们只能监测里面的事件了,所以总归来说,要找到一种方法来检测到iframe里面的所有ajax请求。

下面是我的请求 发现无法执行 ajaxsend
求改进下 能监听到 子页面得ajax 请求


<iframe id='mainIframe' name='mainIframe' src="https://mobile.peilian.com/promotion_f2?utm_source=szjs&utm_medium=cpa&utm_campaign=time0306&utm_term=&utm_content=" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no"  width="1500" height="900" allowfullscreen="true"  ></iframe>
<!--onload="ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);"-->
<div></div>
<script>

    iframe_add_loadevent();
    function  iframe_add_loadevent() {
        var my_frame =document.getElementById('mainIframe');
        alert(1);
        if(my_frame){
            alert(2);
            if(my_frame.attachEvent){ //ie
                alert(3);
                my_frame.onreadystatechange=function () {
                    if (this.readState == 'complete') {
                        ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);

                    }
                };
            } else{  //other
                alert(4);
                    my_frame.onload = function () {
                        alert(5);
                        ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);
                    };

            }
        }
//        if(my_frame){
//            my_frame.onload = function () {
//                ajaxSend(this.contentWindow.XMLHttpRequest, onAjaxSend);
//            }
//        }

    }
    function ajaxSend(objectOfXMLHttpRequest, callback) {
        alert(10);

        // http://stackoverflow.com/questions/3596583/javascript-detect-an-ajax-event

        if(!callback){

            return;

        }

        var s_ajaxListener = new Object();

        s_ajaxListener.tempOpen = objectOfXMLHttpRequest.prototype.open;

        s_ajaxListener.tempSend = objectOfXMLHttpRequest.prototype.send;

        s_ajaxListener.callback = function () {

            // this.method :the ajax method used

            // this.url :the url of the requested script (including query string, if any) (urlencoded)

            // this.data :the data sent, if any ex: foo=bar&a=b (urlencoded)

            alert(2);
            alert(this.method);
            callback(this.method, this.url, this.data);

        }



        objectOfXMLHttpRequest.prototype.open = function(a,b) {

            if (!a) var a='';

            if (!b) var b='';

            s_ajaxListener.tempOpen.apply(this, arguments);

            s_ajaxListener.method = a;

            s_ajaxListener.url = b;

            if (a.toLowerCase() == 'get') {

                s_ajaxListener.data = b.split('?');

                s_ajaxListener.data = s_ajaxListener.data[1];

            }

        }



        objectOfXMLHttpRequest.prototype.send = function(a,b) {

            if (!a) var a='';

            if (!b) var b='';

            s_ajaxListener.tempSend.apply(this, arguments);

            if(s_ajaxListener.method.toLowerCase() == 'post') {

                s_ajaxListener.data = a;

            }

            s_ajaxListener.callback();

        }

    }

    function onAjaxSend(method, url, data) {
        alert('ajax');
        console.log("method"+method);
        console.log(url);
        console.log(data);

    }

</script>

参考地址:https://blog.csdn.net/yanical/article/details/7213289

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-03-12 23:51
    关注

    纯javascript应该是做不到,浏览器这点安全限制都没有那天下大乱了。
    除非你用activex一类的插件,用本地代码去抓包

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条