jQuery的你userscript runs in a separate environment from the page's jQuery。
您需要截取頁的 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中找到。
試過這兩種方法,不幸的是他們沒有工作。我不確定我要去哪裏錯。你有什麼其他想法可以嘗試嗎? – 2012-03-02 20:06:09
我使用了這兩種方法;他們工作正常。編輯你的問題(或開始一個新的問題)來描述如何**它不適合你和**鏈接到目標頁面**。 Greasemonkey腳本總是高度特定於頁面。 ...如果不適合你的代碼不是**完全**,如上所示,開始一個新的問題,顯示你正在使用的確切代碼,但不要忘記到目標頁面的鏈接。至於其他的想法,攔截AJAx很少是最好的方法,但這就是你問到的。監視目標頁面上的特定節點通常會更好。爲此發佈新Q. – 2012-03-02 21:13:05
好的,我會的,謝謝。我基本上試圖監視一個DIV元素,看看它是否是CSS的變化。我試圖檢查每次發生AJAX事件時,這似乎是沒有計時器的唯一方法。 – 2012-03-02 22:16:29