Script ok with Greasemonkey, but not working with Tampermonkey. Is it because of jQuery $ .get?

I made a script for Greasemonkey in Firefox, it works fine, but nothing happens in Chrome with Tampermonkey.

I know that Chrome restricts the use of jQuery.

I especially liked this interesting post: How can I use jQuery in Greasemonkey scripts in Google Chrome?

I tried the solutions, but I still cannot run the script in Google Chrome. I really don't see what is wrong with my script because it is really short. What could be the problem?

This is the script I'm trying to run (I shortened it, but I have to leave a lot since I don't know where the problem is):

// ==UserScript== // @name Rainbow DDB // @namespace Rainbow DDB // @description Change la couleur du "!" lorsqu'une DDB est en cours. // @include http://www.jeuxvideo.com/forums/3-* // @include http://www.jeuxvideo.com/forums/1-* // @version 1 // ==/UserScript== dates = document.getElementsByClassName("date"); i=0; function ddb(j) { url = dates[j].getElementsByTagName("a")[0].href; $.get(url, function(data) { if (data.contains("Signalement déjà fait")) { document.getElementsByClassName("date")[j].getElementsByTagName("a")[0].getElementsByTagName("img")[0].src = "http://image.noelshack.com/fichiers/2013/17/1367080939-14agd2.png"; } }); } while (i<dates.length) { ddb(i); i++; } 

The only thing that could be the problem is $ .get, right?

I tried different solutions, asked to download jQuery before executing my script, I tried with the proposed template, but it definitely didn’t work, and I don’t understand why.

+4
source share
1 answer

If you want to use the jQuery version built into this website, you need to access it using unsafeWindow . In other words: you need to define $ as unsafeWindow.$ At the beginning of your custom word.

Here's the fixed code:

 // ==UserScript== // @name Rainbow DDB // @namespace Rainbow DDB // @description Change la couleur du "!" lorsqu'une DDB est en cours. // @include http://www.jeuxvideo.com/forums/3-* // @include http://www.jeuxvideo.com/forums/1-* // @version 1 // ==/UserScript== $ = unsafeWindow.$; dates = document.getElementsByClassName("date"); i=0; function ddb(j) { url = dates[j].getElementsByTagName("a")[0].href; $.get(url, function(data) { if (data.indexOf("Signalement déjà fait") >= 0) { document.getElementsByClassName("date")[j].getElementsByTagName("a")[0].getElementsByTagName("img")[0].src = "http://image.noelshack.com/fichiers/2013/17/1367080939-14agd2.png"; } }); } while (i<dates.length) { ddb(i); i++; } 
+6
source

All Articles