捕捉到的网页有userscript XMLHTTP请求 [英] capture a pages xmlhttp requests with a userscript
问题描述
我有,增加了显著的功能到一个页面,但最近被打破,因为开发商增加了一些AJAX到该页面的用户脚本(镀铬和FF)。我想修改脚本听页面XMLHTTP请求,这样我可以动态地更新我添加的内容的基础上,JSON格式的responseText
该页面正在接收。
I have a user script (for chrome and FF) that adds significant functionality to a page, but has recently been broken because the developers added some AJAX to the page. I would like to modify the script to listen to the pages xmlhttp requests, so that I can update my added content dynamically, based on the JSON formatted responseText
that the page is receiving.
一个搜索已经打开了这应该工作,并在控制台上运行时做的工作很多功能。但是他们什么都不做,从用户脚本的情况下。
A search has turned up many functions that SHOULD work, and do work when run in the console. However they do nothing from the context of a user script.
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("readystatechange", function() {
console.log(this.readyState);
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
从:<一href="http://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhtt$p$pquests-from-a-greasemonkey-script?answertab=active#tab-top">How我可以拦截XMLHtt prequests从的Greasemonkey脚本?
这完全在控制台中,我可以改变 this.readyState
到 this.responseText
和它的伟大工程(虽然在剧本我需要它来打开JSON数据转换成一个对象,然后让我userscript内操纵它。不只是写控制台)。但是如果我把它粘贴到一个userscript没有任何反应。似乎在页面上的xmlhttp请求不通过在userscript事件处理程序来检测。
This works perfectly in the console, I can change this.readyState
to this.responseText
and it works great (though in the script I will need it to turn the JSON data into an object, and then let me manipulate it within the userscript. Not just write to the console). However if I paste it into a userscript nothing happens. The xmlhttp requests on the page do not seem to be detected by the event handler in the userscript.
页面做的是使用jQuery $。获得()函数的请求,如果能有什么关系呢。虽然我不认为它。
The page doing the requesting is using the jquery $.get() function, if that could have anything to do with it. Though I don't think it does.
我无法想象,没有办法,似乎是一个AJAX页面上运行的任何userscript希望这个能力。
I can't imagine that there isn't a way, seems like any userscript running on an AJAX page would want this ability.
推荐答案
由于该页面使用 $。获得()
,它更容易拦截请求。使用 的ajaxSuccess()
。
Since the page uses $.get()
, it's even easier to intercept requests. Use ajaxSuccess()
.
这将工作在一个Greasemonkey的(火狐)脚本:
片段1:
This will work in a Greasemonkey(Firefox) script:
Snippet 1:
unsafeWindow.$('body').ajaxSuccess (
function (event, requestData)
{
console.log (requestData.responseText);
}
);
假设页面以正常方式( $
定义等)使用jQuery的。
Assuming the page uses jQuery in the normal way ($
is defined, etc.).
这应该在Chrome浏览器userscript工作(以及Greasemonkey的):
片段2:
This should work in a Chrome userscript (as well as Greasemonkey):
Snippet 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);
回复:
Re:
怎么会那么做,我得到了数据的脚本?......(这样我就可以)以后使用该脚本中的数据。
"But how then do I get that data to the script? ... (So I can) use the data later in the script."
这工作在Greasemonkey的(火狐);它也可能在工作Chrome的<一个href="https://chrome.google.com/webstore/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo">Tampermonkey:
片段3:
This works in Greasemonkey(Firefox); it might also work in Chrome's Tampermonkey:
Snippet 3:
function myAjaxHandler (requestData) {
console.log ('myAjaxHandler: ', requestData.responseText);
}
unsafeWindow.$('body').ajaxSuccess (
function (event, requestData) {
myAjaxHandler (requestData);
}
);
但是,如果它没有,然后你不能共享一个浏览器userscript和目标页面之间的JS信息(容易) - 设计
But, if it doesn't then you cannot share JS information (easily) between a Chrome userscript and the target page -- by design.
通常情况下,你做的是注入整个userscript,让一切在页面范围内运行。像这样:
摘录4:
Typically what you do is inject your entire userscript, so that everything runs in the page scope. Like so:
Snippet 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);
这篇关于捕捉到的网页有userscript XMLHTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!