您需要截取页的 AJAX调用,所以你可以使用(A)unsafeWindow或(B)注入脚本。
(A)unsafeWindow样子:
unsafeWindow.$(document).ajaxSuccess(function(e, xhr) {
alert("ajax success hit!");
// I will do my other handling here
});
(B)脚本注入的样子:
function scriptWrapper() {
//--- Intercept Ajax
$('body').ajaxSuccess (
function (event, requestData) {
alert ("ajax success hit!");
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);
注意,在这两种情况下,你必须意识到数据不会从页面范围轻松回到GM范围 - 小心混合两个。
用于在沙箱上传输数据的一种解决方法可在this answer中找到。