由于页面使用
$.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);


