我是@require
- 在我的脚本文件中使用此行为我的Greasemonkey脚本创建jQuery:
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
它运作良好;我可以使用$('#id')
来访问DOM。
然而,'real'页面的$
变量被修改(其中$
是jQuery 1.2.xx):
我收到$.include
未定义的错误。
我认为Greasemonkey的沙箱模型会阻止目标页面的变量被覆盖?
如何确保包含javascript库不会影响“真实”网站,只会影响我的Greasemonkey脚本?
答案 0 :(得分:8)
Greasemonkey 1.0, radically changed the way the sandbox works,破坏了数千个脚本。另请参阅jQuery in Greasemonkey 1.0 conflicts with websites using jQuery。
这是一个很大的问题,我希望你能和我一起在the principle bug report for this issue上表达你的意见/经验。
同时,将沙箱恢复到您的脚本,并通过修改 Metadata Block以结束以下行来解决$
冲突:
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change introduced in GM 1.0,
It restores the sandbox.
*/
指定@grant
值(none
除外)会重新激活沙箱。
您可能还会考虑切换到Scriptish,它在过去提供了卓越的功能和性能,并且不会受到这种新的沙箱行为的影响。
答案 1 :(得分:3)
见jQuery.noConflict();
;它解释了很多。
编辑:
找到"Greasemonkey 1.0 + jQuery: Broken, with Workaround"进行搜索。
可能的解决方案:
this.$ = this.jQuery = jQuery.noConflict(true);
这样做,可能会隐含地指定$,jQuery将在脚本的命名空间中可用。