I am building a GreaseMonkey test script that makes a GM_xmlhttpRequest each time a specific site is visited. GM_xmlhttpRequest should only trigger on the first "document" found (the parent window) and it should ignore iframes and other child windows (I don't want the url for the iframes).
Some thoughts and possible solutions:
1) I tried to insert a break in the GM_xmlhttpRequest onload-callback. Result: The script does not respond. No error message in FireBug. (I guess break only works in loops.)
onload: function(response) {
if (response.status == 200) break;
}
2) Insert an addEventListener before/after the GM_xmlhttpRequest: Result: The script does not respond. No error message in FireBug.
document.addEventListener("load",
// (Insert the GM_xmlhttpRequest code here - see below)
,false);
3) Idea: Can the GM_xmlhttpRequest be "cancelled" after the first successful request? Either in the onload-part, or after the script (like document.removeEventListener cancels document.addEventListener).
4) Idea: Can GreaseMonkey identify the parent window? so the script only runs in the parent window?
Also, I would prefer not to make the script as a synchronous call since it will slow things down.
// ==UserScript==
// @name GreaseMonkey xmlhttpRequest test
// @include http://www.jp.dk/*
// ==/UserScript==
GM_xmlhttpRequest({
method: "POST",
url: "http://localhost/do_stuff",
data: "url=" + document.location.href,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
if (response.responseText)
alert("GreaseMonkey said: " + response.responseText);
}
});