@required 脚本是否会与 Greasemonkey 中的其他脚本发生冲突?
Can @required scripts clash with other scripts in Greasemonkey?
在 Greasemonkey 中,如果用户脚本通过使用例如@require react-15.1.0.js
,如果原始页面也包含 React 但版本较旧(比如 react-15.0.0.js
),会发生什么情况?全局名称是否以某种方式保存在单独的环境中,或者用户脚本是否有可能通过重新定义全局名称来破坏页面(反之亦然)?
不,他们不能冲突。我不清楚greasemonkey的执行模型的细节,但我进行了测试。
我创建了这个 HTML 文件,它在单击按钮时使用 lodash:
<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>
我还创建了一个用户脚本,它删除了 _:
// ==UserScript==
// @name Test greasmonkey overriding
// @namespace gm-overriding
// @version 1
// @grant none
// ==/UserScript==
_ = null
console.log("here is _:", _);
用户脚本 不会 影响 lodash 依赖项,单击按钮仍会打印预期的输出。
在 Greasemonkey 中,如果用户脚本通过使用例如@require react-15.1.0.js
,如果原始页面也包含 React 但版本较旧(比如 react-15.0.0.js
),会发生什么情况?全局名称是否以某种方式保存在单独的环境中,或者用户脚本是否有可能通过重新定义全局名称来破坏页面(反之亦然)?
不,他们不能冲突。我不清楚greasemonkey的执行模型的细节,但我进行了测试。 我创建了这个 HTML 文件,它在单击按钮时使用 lodash:
<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>
我还创建了一个用户脚本,它删除了 _:
// ==UserScript==
// @name Test greasmonkey overriding
// @namespace gm-overriding
// @version 1
// @grant none
// ==/UserScript==
_ = null
console.log("here is _:", _);
用户脚本 不会 影响 lodash 依赖项,单击按钮仍会打印预期的输出。