栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用用户脚本捕获页面xmlhttp请求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用用户脚本捕获页面xmlhttp请求

由于页面使用

$.get()
,因此拦截请求甚至更加容易。使用
ajaxSuccess()

这将在Greasemonkey(Firefox)脚本中起作用:
片段1:

unsafeWindow.$('body').ajaxSuccess (    function (event, requestData)    {        console.log (requestData.responseText);    });

假设页面以普通方式(

$
定义等)使用jQuery 。

这应该可以在Chrome用户脚本(以及Greasemonkey)中运行:
片段2:

function interceptAjax () {    $('body').ajaxSuccess (        function (event, requestData)        { console.log (requestData.responseText);        }    );}function addJS_Node (text, s_URL, funcToRun) {    var D  = document;    var scriptNode    = D.createElement ('script');    scriptNode.type   = "text/javascript";    if (text)       scriptNode.textContent  = text;    if (s_URL)      scriptNode.src          = s_URL;    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';    var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;    targ.appendChild (scriptNode);}addJS_Node (null, null, interceptAjax);



回覆:

“但是如何将这些数据保存到脚本中呢?…(这样我就可以)稍后在脚本中使用这些数据。”

这适用于Greasemonkey(Firefox);
它也可能在Chrome的Tampermonkey中起作用:
代码段3:

function myAjaxHandler (requestData) {    console.log ('myAjaxHandler: ', requestData.responseText);}unsafeWindow.$('body').ajaxSuccess (    function (event, requestData) {        myAjaxHandler (requestData);    });

但是, 如果不是 这样,则无法通过设计在Chrome用户脚本和目标页面之间轻松地共享JS信息。

通常,您要做的是注入整个用户脚本,以便所有内容都在页面范围内运行。像这样:
片段4:

function scriptWrapper () {    //--- Intercept Ajax    $('body').ajaxSuccess (        function (event, requestData) { doStuffWithAjax (requestData);        }    );    function doStuffWithAjax (requestData) {        console.log ('doStuffWithAjax: ', requestData.responseText);    }    //--- DO YOUR OTHER STUFF HERE.    console.log ('Doing stuff outside Ajax.');}function addJS_Node (text, s_URL, funcToRun) {    var D  = document;    var scriptNode    = D.createElement ('script');    scriptNode.type   = "text/javascript";    if (text)       scriptNode.textContent  = text;    if (s_URL)      scriptNode.src          = s_URL;    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';    var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;    targ.appendChild (scriptNode);}addJS_Node (null, null, scriptWrapper);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401732.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号