jQuery in Greasemonkey 1.0 conflicts with websites using jQuery
Greasemonkey 1.0, radically changed the way the sandbox works, busting thousands of scripts. This is a huge problem, and I hope you will join me in voicing your opinion/experiences on the principle bug report for this issue.
The Greasemonkey blog claims that you can workaround the issue with the following:
this.$ = this.jQuery = jQuery.noConflict(true);
... Which I'm not sure will work in all cases. And it is the exact wrong approach from a side-effects-avoiding, DRY-principle, atomic-coding philosophy. In my opinion, the best strategy is to restore the sandbox.
Reactivate the sandbox by specifying a @grant
value (other than none
). Edit your Metadata Block to end with the following lines:
// @grant GM_addStyle
// @grant GM.getValue
// ==/UserScript==
/*- The @grant directive is needed to work around a design flaws introduced in GM 1.0
and again in GM 4.0.
It restores the sandbox.
*/
The sandbox will be restored and all conflicts will be resolved.
And the scripts will be compatible with superior engines like Tampermonkey and Violentmonkey.
jQuery.noConflict(); for userscript
I just looked at another userscript and this is the solution!
(function wrapper(window, injectNeeded, undefined) {
// Script injection if needed.
if (injectNeeded) {
var script = document.createElement('script');
script.textContent = '(' + wrapper + ')(window, false)';
document.body.appendChild(script);
document.body.removeChild(script);
return;
}
var $,
jQuery;
$ = jQuery = window.jQuery;
console.log($('a').length + "links on this page.");
})(this.unsafeWindow || window, window.chrome ? true : false);
Source: https://raw.github.com/cletusc/Userscript--Twitch-Chat-Emotes/master/script.user.js#bypass=true
Greasemonkey Jquery interference?
Greasemonkey 1.0, radically changed the way the sandbox works, busting thousands of scripts. See also, jQuery in Greasemonkey 1.0 conflicts with websites using jQuery.
This is a huge problem, and the lead developer of Greasemonkey has expressed a complete unwillingness to resolve it in a sensible way.
To work around it, restore the sandbox to your script, and resolve the $
conflict, by editing your Metadata Block to end with the following lines:
// @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.
*/
Specifying a @grant
value (other than none
) reactivates the sandbox.
You might also consider switching to Scriptish, which has offered superior features and performance in the past, and does not suffer from this new sandbox behavior.
Scriptish, so far, hasn't posted updates that destroy a large portion of its install base, either.
How to edit html with jQuery without conflicts via GreaseMonkey?
Edit: I found a solution here: jQuery in Greasemonkey 1.0 conflicts with websites using jQuery
Change the @grant to:
// @grant GM_log
Then everything works well. Tried and tested as such:
// ==UserScript==
// @name test
// @namespace test
// @description Filter words and disable images if it founded
// @include http://myanimelist.net/*
// @version 3
// @require http://code.jquery.com/jquery-latest.min.js
// @grant GM_log
// ==/UserScript==
//window.addEventListener('load', function ()
document.addEventListener('DOMContentLoaded',
function ()
{
var _$ = this.jQuery = jQuery.noConflict(true);
var txt = _$("body").html();
_$("body").append('<span>SEARCH FOR ME IN SITE!</span>');
console.log('123123123123');
});
@require-ing jQuery overwrites the page's $ variable
Greasemonkey 1.0, radically changed the way the sandbox works, busting thousands of scripts. See also, jQuery in Greasemonkey 1.0 conflicts with websites using jQuery.
This is a huge problem, and I hope you will join me in voicing your opinion/experiences on the principle bug report for this issue.
Meanwhile, restore the sandbox to your script, and resolve the $
conflict, by editing your Metadata Block to end with the following lines:
// @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.
*/
Specifying a @grant
value (other than none
) reactivates the sandbox.
You might also consider switching to Scriptish, which has offered superior features and performance in the past, and does not suffer from this new sandbox behavior.
jQuery .ready not getting called from within Greasemonkey script
The alert doesn't fire for two reasons:
The browser is redirected from
www.ted.com
towww.youtube.com
right away -- long before theready
event can fire onted.com
. The script is not set to fire onyoutube.com
at all.If you want the script to fire on both domains, adjust the
@include
(or@match
) directives and then use checks like:if (location.hostname == "www.ted.com") {
var url = window.location.href;
var talk_name = url.split("/").pop();
talk_name = talk_name.substring(0, talk_name.lastIndexOf('.html'));
var youtube_search_url = 'http://www.youtube.com/user/TEDtalksDirector/search?query=' + talk_name;
window.location.href = youtube_search_url;
}to alter the script's behavior, per domain.
Because
@grant none
is used, the GM script's jQuery conflicts with code on both domains. You will see error messages on Firefox's Error Console (CtrlShiftJ).
The solution for that is to restore the sandbox by changing the@grant
to@grant GM_addStyle
.
Related Topics
Deleting Objects in JavaScript
Convert String to Variable Name in JavaScript
Convert Xml to Json (And Back) Using JavaScript
How to Convert String to Datetime With Format Specification in JavaScript
How to Match Overlapping Strings With Regex
Scroll to the Top of the Page Using JavaScript
Can't Access Object Property, Even Though It Shows Up in a Console Log
How to Access a Dom Element in React? What Is the Equilvalent of Document.Getelementbyid() in React
Is There a Case Insensitive Jquery :Contains Selector
Meaning of "This" in Node.Js Modules and Functions
How to Get the Difference Between Two Dates in JavaScript
Open a File with Default Program in Node-Webkit
Web Worker Settings for Chrome
Node.Js - How to Set Environment Variables in Code
Opera Preventdefault() on Keydown Event