-1

In Greasemonkey, if a userscript adds things to the global namespace by using e.g. @require react-15.1.0.js, what happens if the original page also included React but an older version (say react-15.0.0.js)? Are the global names kept in separate environments somehow, or is there a possibility that a userscript may break a page (and vice versa) by redefining global names?

aom
  • 21
  • 4

1 Answers1

0

No, they cannot clash. I'm not clear on the details of greasemonkey's execution model, but I performed a test. I created this HTML file, which uses lodash when a button is clicked:

<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js'></script>
</head>
<body>
<input type='button' id="btn" value="Invoke lodash">
<script type="text/javascript">
 el = document.getElementById("btn")
 el.addEventListener('click',function (argument) {
  console.log("3 =", _.add(1,2));
 })
</script>
</body>
</html>

I also created a userscript, which removes _:

// ==UserScript==
// @name        Test greasmonkey overriding
// @namespace   gm-overriding
// @version     1
// @grant       none
// ==/UserScript==

_ = null
console.log("here is _:", _);

The userscript does not affect the lodash dependency, and the button click still prints the expected output.

aom
  • 21
  • 4
  • The test is flawed and your conclusion is inaccurate. Scripts interfere with each other and with the page all the time. [Here's one class of examples](http://stackoverflow.com/questions/12146445/jquery-in-greasemonkey-1-0-conflicts-with-websites-using-jquery). ... In your test script use `window._ = null;` and watch what happens. – Brock Adams Jul 25 '16 at 05:57