Delgan
Delgan

Reputation: 19627

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

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

I know that Chrome restricts the use of jQuery.

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

I tried the solutions but I still can not get my script to run on Google Chrome. I do not really see what's 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 am obliged to leave much since I do not know where is the problem):

// ==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 can be a problem is $.get, is not it?

I tried different solutions, ask loading jQuery before executing my script, I tried with the proposed template but it definitely did not work, and I do not see why.

Upvotes: 4

Views: 5216

Answers (1)

ghost
ghost

Reputation: 769

If you want to use jQuery version that is embedded on this website, you need to refer to it with unsafeWindow. In other words: you need to define $ as unsafeWindow.$ at the beginning of your userscript.

Here's 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++;
}

Upvotes: 6

Related Questions