greasemonkey / greasemonkey

Greasemonkey is a user script manager for Firefox.
http://www.greasespot.net/
Other
2.34k stars 330 forks source link

DOMException on GM.xmlHttpRequest when using `context` #2701

Open hkyuwon opened 6 years ago

hkyuwon commented 6 years ago

When I try to use [context] on xHR, I meet this error. Script error: DOMException { }

// no problem GM.xmlHttpRequest({method:"GET",url:"/",onload:function(r){console.log(r);}});

// error GM.xmlHttpRequest({method:"GET",url:"/",context:{f:function(r){console.log(r);}}}); // error GM.xmlHttpRequest({method:"GET",url:"/",context:{d:document}});

ccloli commented 6 years ago

I have this problem too, maybe it's related to #2647 . Try adding location.origin before the URL.

Mzyxptlk commented 3 years ago

I am also experiencing this issue. Minimal testcase:

// ==UserScript==
// @name     onload context bug
// @grant    GM.xmlHttpRequest
// @include  https://github.com/greasemonkey/greasemonkey/issues/2701
// ==/UserScript==
var does_not_work = function() {
  console.log('context called!');
};
GM.xmlHttpRequest({
  'url': location.origin,
  'onload': function(req) {
    console.log('onload called');
    if (req.context.call_me) {
      req.context.call_me();
    }
    else {
      console.log('nothing to call');
    }
  },
  'context': {
    'call_me': does_not_work
  }
});
console.log('sent request!');

Loading this page with the above user script installed results in the following error:

Script error in [Greasemonkey script null/onload context bug; version 1]: DataCloneError: The object could not be cloned.

The error does not occur when the context object is empty (but obviously does_not_work is not called in that case either). Unfortunately I've not been able to find a useful backtrace.