2

In my script, GM_xmlhttpRequest doesn't work, but regular xmlhttpRequest does. (But I need GM_xmlhttpRequest because the call is cross-domain.)

window.onbeforeunload = function (){
    try{
        details = GM_xmlhttpRequest({
           method: "GET",
           url: "http://www.example.com",
           synchronous: true // this been tried with both possible values
        });
        console.log(details); // Object {abort: function}
    }
    catch(e){ // catch is not being triggered
        console.log(e);   
    }
    return "dontleave";
}

The same code does exactly what it is expected to in Firefox (AJAX first, then continue).

Regular xmlhttpRequest also does what it should in that spot (but it doesn't allow cross domain calls).

What is wrong?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
AlexTR
  • 772
  • 8
  • 14
  • Chrome is much stricter about cross-scope functions between a userscript and page. Try the technique from [this answer](http://stackoverflow.com/a/14255440/331508) as a start, but you may also have to block the page unload since messaging is asynchronous. – Brock Adams May 10 '14 at 22:48

1 Answers1

2

In short, the problem was caused because Tampermonkey, the addon that handles userscripts in Chrome, does not supports GM_xmlhttpRequest fully - it doesn't allows synchronous calls. To make things worse, Chrome itself blocks execution of any asynchronous callbacks during unbeforeunload (delays them until ununload).

We found absolutely no acceptable way around this and had to abandon the script and leave users with limited-functional replacement for Chrome.

AlexTR
  • 772
  • 8
  • 14