Dan*_*ark 2 jquery tampermonkey
我正在为使用旧版本jQuery的网站制作Tampermonkey脚本。我想在脚本中使用更新的版本。我已经试过了:
var contentIndex = 0;
var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
var jQuery_310 = $.noConflict(true);
Run Code Online (Sandbox Code Playgroud)
但noConflict运行晚了(看来):我正在篡改的网站现在正在与较新的jQuery通讯。
如何避免在现有站点上发生这种冲突?
将此添加到Tampermonkey:
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @grant none
Run Code Online (Sandbox Code Playgroud)
主站点的使用$将引用新的jQuery(310),它可能会破坏“被篡改”站点上的内容。要解决此问题,第一行代码可能是:
window.jQuery310 = $.noConflict(true);
Run Code Online (Sandbox Code Playgroud)
另一个选择: 限制更严格@grant将消除对noConflict生产线的需求。